Permalink
Browse files

Merge pull request #1 from spooon/master

some improvements
  • Loading branch information...
2 parents d95c543 + 168671e commit a39cc1f18df819b8ba78f6701b49da9ad8cc9328 @tokuhirom tokuhirom committed May 23, 2011
Showing with 26 additions and 3 deletions.
  1. +8 −0 docs/arrays/array_merge.txt
  2. +17 −2 docs/arrays/array_rand.txt
  3. +1 −1 docs/arrays/array_sum.txt
@@ -7,8 +7,16 @@ array array_merge ( array $array1 [, array $array2 [, array $... ]] )
*Perl
>|perl|
+# for concatenating arrays
my @result = ( @array1, @array2 , @array3 ... );
push @result, @array1;
push @result, @array2;
+
+# for merging hashes
+my %result = ( %hash1, %hash2 ... );
+
+# alternately:
+my %result = %hash1;
+@result { keys %hash2 } = values %hash2;
||<
View
@@ -9,6 +9,21 @@ mixed array_rand ( array $input [, int $num_req = 1 ] )
**Perl
>|Perl|
-use List::Util qw(shuffle);
-@rand = shuffle @array;
+# FOR ARRAYS
+# when $num_req == 1:
+my $result = int rand @array;
+
+# otherwise:
+use Data::Random qw/ rand_set /;
+my @results = rand_set ( set => [0 .. $#array], size => $num_req );
+
+# FOR HASHES
+# when $num_req == 1:
+my @keys = keys %hash;
+my $result = $keys [ int rand @keys ];
+
+# otherwise:
+use Data::Random qw/ rand_set /;
+my @keys = keys %hash;
+my @results = @keys [ rand_set ( set => [0 .. $#array], size => $num_req ) ];
||<
@@ -20,5 +20,5 @@ $sum += $_ for @array;
# List::Util を使って
use List::Util qw/ sum /;
-$sum = sum @array;
+$sum = sum 0, @array; # the 0 is to allow it to work with empty arrays too
||<

0 comments on commit a39cc1f

Please sign in to comment.