免费注册 查看新帖 |

Chinaunix

广告
  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1599 | 回复: 3
打印 上一主题 下一主题

请帮助解释一下,不太明白! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-05 10:08 |只看该作者 |倒序浏览
内容如下:
#!/usr/bin/perl

$maxLenth=100;
@a = (0..9,'$','%','a'..'z','A'..'Z','-','+','_');
$password = join '', map { $a[int rand @a] } 0..($maxLenth-1);
print "$password\n";


主要是第五行!说高手指点

论坛徽章:
0
2 [报告]
发表于 2006-07-05 10:18 |只看该作者

回复 1楼 cheng_ge 的帖子

http://blog.chinaunix.net/u/10363/showart.php?id=53084  

哈哈,5.产生随机密码!

谢,仙子姐姐??!!

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2006-07-05 10:30 |只看该作者
  1. join EXPR,LIST
  2.         Joins the separate strings of LIST into a single string with
  3.         fields separated by the value of EXPR, and returns that new
  4.         string. Example:

  5.             $rec = join(':', $login,$passwd,$uid,$gid,$gcos,$home,$shell);

  6.         Beware that unlike "split", "join" doesn't take a pattern as its
  7.         first argument. Compare "split".
复制代码

备忘:join 将 LIST 里面的每个元素用 EXPR 为分隔符连接成一个字符串,并且返回这个字符串
  1. map BLOCK LIST
  2. map EXPR,LIST
  3.         Evaluates the BLOCK or EXPR for each element of LIST (locally
  4.         setting $_ to each element) and returns the list value composed
  5.         of the results of each such evaluation. In scalar context,
  6.         returns the total number of elements so generated. Evaluates
  7.         BLOCK or EXPR in list context, so each element of LIST may
  8.         produce zero, one, or more elements in the returned value.

  9.             @chars = map(chr, @nums);

  10.         translates a list of numbers to the corresponding characters.
  11.         And

  12.             %hash = map { getkey($_) => $_ } @array;

  13.         is just a funny way to write

  14.             %hash = ();
  15.             foreach $_ (@array) {
  16.                 $hash{getkey($_)} = $_;
  17.             }

  18.         Note that $_ is an alias to the list value, so it can be used to
  19.         modify the elements of the LIST. While this is useful and
  20.         supported, it can cause bizarre results if the elements of LIST
  21.         are not variables. Using a regular "foreach" loop for this
  22.         purpose would be clearer in most cases. See also "grep" for an
  23.         array composed of those items of the original list for which the
  24.         BLOCK or EXPR evaluates to true.

  25.         "{" starts both hash references and blocks, so "map { ..." could
  26.         be either the start of map BLOCK LIST or map EXPR, LIST. Because
  27.         perl doesn't look ahead for the closing "}" it has to take a
  28.         guess at which its dealing with based what it finds just after
  29.         the "{". Usually it gets it right, but if it doesn't it won't
  30.         realize something is wrong until it gets to the "}" and
  31.         encounters the missing (or unexpected) comma. The syntax error
  32.         will be reported close to the "}" but you'll need to change
  33.         something near the "{" such as using a unary "+" to give perl
  34.         some help:

  35.             %hash = map {  "\L$_", 1  } @array  # perl guesses EXPR.  wrong
  36.             %hash = map { +"\L$_", 1  } @array  # perl guesses BLOCK. right
  37.             %hash = map { ("\L$_", 1) } @array  # this also works
  38.             %hash = map {  lc($_), 1  } @array  # as does this.
  39.             %hash = map +( lc($_), 1 ), @array  # this is EXPR and works!

  40.             %hash = map  ( lc($_), 1 ), @array  # evaluates to (1, @array)

  41.         or to force an anon hash constructor use "+{"

  42.            @hashes = map +{ lc($_), 1 }, @array # EXPR, so needs , at end

  43.         and you get list of anonymous hashes each with only 1 entry.
复制代码

备忘:map 将 LIST 中的每个元素用 EXPR 或者 BLOCK 指定的代码处理一遍,并且返回处理后的列表。

  1. rand EXPR
  2. rand    Returns a random fractional number greater than or equal to 0
  3.         and less than the value of EXPR. (EXPR should be positive.) If
  4.         EXPR is omitted, the value 1 is used. Currently EXPR with the
  5.         value 0 is also special-cased as 1 - this has not been
  6.         documented before perl 5.8.0 and is subject to change in future
  7.         versions of perl. Automatically calls "srand" unless "srand" has
  8.         already been called. See also "srand".

  9.         Apply "int()" to the value returned by "rand()" if you want
  10.         random integers instead of random fractional numbers. For
  11.         example,

  12.             int(rand(10))

  13.         returns a random integer between 0 and 9, inclusive.

  14.         (Note: If your rand function consistently returns numbers that
  15.         are too large or too small, then your version of Perl was
  16.         probably compiled with the wrong number of RANDBITS.)
复制代码

备忘:rand 返回一个介于 0~EXPR 之间的随机的小数。

  1. int EXPR
  2. int     Returns the integer portion of EXPR. If EXPR is omitted, uses
  3.         $_. You should not use this function for rounding: one because
  4.         it truncates towards 0, and two because machine representations
  5.         of floating point numbers can sometimes produce counterintuitive
  6.         results. For example, "int(-6.725/0.025)" produces -268 rather
  7.         than the correct -269; that's because it's really more like
  8.         -268.99999999999994315658 instead. Usually, the "sprintf",
  9.         "printf", or the "POSIX::floor" and "POSIX::ceil" functions will
  10.         serve you better than will int().
复制代码

备忘:int 可以舍弃一个小数的小数部分,只留下整数部分。

合起来就是:
  1. 得到一个由 $maxLength 个随机字符组成的字符串。该字符串中的每个字符都取自于 @a 这个字符数组。
复制代码

论坛徽章:
0
4 [报告]
发表于 2006-07-06 10:51 |只看该作者

回复 3楼 flw 的帖子

真详细! 非常感谢!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP