LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: KornLee

有关Perl问题的讨论请到这里

[复制链接]
发表于 2003-10-25 11:16:46 | 显示全部楼层
对不起,那个脚本严重不行,今天再试多几次,发现漏了一次检验了。发表了这样的脚本,向大家道歉,现在我改了,此脚本只适用于正整数的转换。不知道那里还有漏洞的,请大家指出。

#!/usr/bin/perl
#author:devel
print "input a 2 format number :\n";
chomp($a=<STDIN>);
if ( $a >=10) {
    while ( $a >= 10)
{
             $A=&yu ($a);
             $result += 2**$A;
             $a -=10**$A;
         }
    $result += $a % 10 ;
    print "$result\n";
}
else {
    print "$a\n";
}
sub yu {     #这个子函数的作用是计算出有多少位的十进制正整数。
$k=$a,$i=0;
while ( $k >= 10 )
{
$e= $k % 10;
exit if ( $k !~ /[01]/ );
$k -= $e;
$k/=10;
++$i;
}
exit if ($k !~ /[01]/ );    #加了这里
return "$i";          #返回值就是位数了。
}
发表于 2003-10-25 19:08:06 | 显示全部楼层
请问new()这个函数有什么用:ask
发表于 2003-11-6 16:22:47 | 显示全部楼层
想写一个类似于web爬虫一类的程序,定期检查站点url的可连接性,但是对于那些要求登陆后才可见的web页,我怎样才能让它自动登陆,进行检查呢?
 楼主| 发表于 2003-11-7 11:39:12 | 显示全部楼层
7楼和8楼的问题,我看还是让咱们的perl高手来解决吧~~ :sorry
发表于 2003-11-7 21:00:11 | 显示全部楼层

我不是高手...

请问new()这个函数有什么用

29.2.100. new   

new CLASSNAME LIST
new CLASSNAME

There is no built-in new function. It is merely an ordinary constructor method (that is, a user-defined subroutine) that is defined or inherited by the CLASSNAME class (that is, package) to let you construct objects of type CLASSNAME. Many constructors are named "new", but only by convention, just to trick C++ programmers into thinking they know what's going on. Always read the documentation of the class in question so you know how to call its constructors; for example, the constructor that creates a list box in the Tk widget set is just called Listbox().

另:Perl提供内置的制式转换函数
一个二进制转为十进制的脚本

29.2.103. oct     

oct EXPR
oct
This function interprets EXPR as an octal string and returns the equivalent decimal value. If EXPR happens to start with "0x", it is interpreted as a hexadecimal string instead. If EXPR starts off with "0b", it is interpreted as a string of binary digits. The following will properly convert to numbers any input strings in decimal, binary, octal, and hex bases written in standard C or C++ notation:
$val = oct $val if $val =~ /^0/;
To perform the inverse function, use sprintf with an appropriate format:


$perms = (stat("filename"))[2] & 07777;
$oct_perms = sprintf "%lo", $perms;
The oct function is commonly used when a data string such as "644" needs to be converted into a file mode, for example. Although Perl will automatically convert strings into numbers as needed, this automatic conversion assumes base 10.

想写一个类似于web爬虫一类的程序,定期检查站点url的可连接性,但是对于那些要求登陆后才可见的web页,我怎样才能让它自动登陆,进行检查呢?

你可以看看perl-CGI类参考书中有关HTTP协议的章节。
发表于 2003-11-8 20:54:19 | 显示全部楼层
我想通过perl来控制串口,从交换机中读取信息,但是怎样从串口中读入信息呢?有没有资料,或者图书什么的?
发表于 2003-11-9 10:51:12 | 显示全部楼层
如果你有C语言进行这类操作的经验,应该知道基本的思路,用Perl的思路是一样的,可以查阅有关系统IO调用的Perl文档,还可以去CPAN参考一下IO::Stty、Device:arallelPort 、Device::SerialPort 等文档。
发表于 2003-11-11 16:40:29 | 显示全部楼层
一个从正整数的10进制转到正整数的8进制的perl脚本,有什么漏洞请大家指出


#!/usr/bin/perl
$i=0;
print "input a 10 format num: \n";
chomp($n=<>);
if ( $n > 7 )
{
$c=0;
while ( $n >= 8 )
{
$i=$n % 8;
$r += (10 ** $c) * $i;
$c++;
$n=( $n - $n % 8 ) /8 ;
}
$r += (10 ** $c) * $n;
print "0$r\n";
}
else
{
print "0$n\n";
}

改一下,也可以变成从正整数的10进制转为2进制的,或者3.4.5.6.7,8,9进制都行但计算机并不需要这些。呵呵~~~~


#!/usr/bin/perl
print "input a 10 format num: \n";
chomp($n=<>);
if ( $n > 1 )
{
$c=0;
while ( $n >= 2 )
    {
       $i=$n % 2;
       $r += (10 ** $c) * $i;
       $c++;
       $n=( $n - $n % 2 ) /2 ;
     }
    $r += (10 ** $c) * $n;
    print "$r\n";
}
else
   {
    print "$n\n";
   }
 楼主| 发表于 2003-11-12 16:43:39 | 显示全部楼层
devle一露面就出手不凡
 楼主| 发表于 2003-11-12 23:33:49 | 显示全部楼层
最初由 home 发表
who is devle ?;)

大水缸兄,我推荐你作版主,

,支持~~~,我9号给他发了短消息~~,希望他能来~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表