- 论坛徽章:
- 1
|
- join EXPR,LIST
- Joins the separate strings of LIST into a single string with
- fields separated by the value of EXPR, and returns that new
- string. Example:
- $rec = join(':', $login,$passwd,$uid,$gid,$gcos,$home,$shell);
- Beware that unlike "split", "join" doesn't take a pattern as its
- first argument. Compare "split".
复制代码
备忘:join 将 LIST 里面的每个元素用 EXPR 为分隔符连接成一个字符串,并且返回这个字符串
- map BLOCK LIST
- map EXPR,LIST
- Evaluates the BLOCK or EXPR for each element of LIST (locally
- setting $_ to each element) and returns the list value composed
- of the results of each such evaluation. In scalar context,
- returns the total number of elements so generated. Evaluates
- BLOCK or EXPR in list context, so each element of LIST may
- produce zero, one, or more elements in the returned value.
- @chars = map(chr, @nums);
- translates a list of numbers to the corresponding characters.
- And
- %hash = map { getkey($_) => $_ } @array;
- is just a funny way to write
- %hash = ();
- foreach $_ (@array) {
- $hash{getkey($_)} = $_;
- }
- Note that $_ is an alias to the list value, so it can be used to
- modify the elements of the LIST. While this is useful and
- supported, it can cause bizarre results if the elements of LIST
- are not variables. Using a regular "foreach" loop for this
- purpose would be clearer in most cases. See also "grep" for an
- array composed of those items of the original list for which the
- BLOCK or EXPR evaluates to true.
- "{" starts both hash references and blocks, so "map { ..." could
- be either the start of map BLOCK LIST or map EXPR, LIST. Because
- perl doesn't look ahead for the closing "}" it has to take a
- guess at which its dealing with based what it finds just after
- the "{". Usually it gets it right, but if it doesn't it won't
- realize something is wrong until it gets to the "}" and
- encounters the missing (or unexpected) comma. The syntax error
- will be reported close to the "}" but you'll need to change
- something near the "{" such as using a unary "+" to give perl
- some help:
- %hash = map { "\L$_", 1 } @array # perl guesses EXPR. wrong
- %hash = map { +"\L$_", 1 } @array # perl guesses BLOCK. right
- %hash = map { ("\L$_", 1) } @array # this also works
- %hash = map { lc($_), 1 } @array # as does this.
- %hash = map +( lc($_), 1 ), @array # this is EXPR and works!
- %hash = map ( lc($_), 1 ), @array # evaluates to (1, @array)
- or to force an anon hash constructor use "+{"
- @hashes = map +{ lc($_), 1 }, @array # EXPR, so needs , at end
- and you get list of anonymous hashes each with only 1 entry.
复制代码
备忘:map 将 LIST 中的每个元素用 EXPR 或者 BLOCK 指定的代码处理一遍,并且返回处理后的列表。
- rand EXPR
- rand Returns a random fractional number greater than or equal to 0
- and less than the value of EXPR. (EXPR should be positive.) If
- EXPR is omitted, the value 1 is used. Currently EXPR with the
- value 0 is also special-cased as 1 - this has not been
- documented before perl 5.8.0 and is subject to change in future
- versions of perl. Automatically calls "srand" unless "srand" has
- already been called. See also "srand".
- Apply "int()" to the value returned by "rand()" if you want
- random integers instead of random fractional numbers. For
- example,
- int(rand(10))
- returns a random integer between 0 and 9, inclusive.
- (Note: If your rand function consistently returns numbers that
- are too large or too small, then your version of Perl was
- probably compiled with the wrong number of RANDBITS.)
复制代码
备忘:rand 返回一个介于 0~EXPR 之间的随机的小数。
- int EXPR
- int Returns the integer portion of EXPR. If EXPR is omitted, uses
- $_. You should not use this function for rounding: one because
- it truncates towards 0, and two because machine representations
- of floating point numbers can sometimes produce counterintuitive
- results. For example, "int(-6.725/0.025)" produces -268 rather
- than the correct -269; that's because it's really more like
- -268.99999999999994315658 instead. Usually, the "sprintf",
- "printf", or the "POSIX::floor" and "POSIX::ceil" functions will
- serve you better than will int().
复制代码
备忘:int 可以舍弃一个小数的小数部分,只留下整数部分。
合起来就是:
- 得到一个由 $maxLength 个随机字符组成的字符串。该字符串中的每个字符都取自于 @a 这个字符数组。
复制代码 |
|