Skip to content

Commit a39cc1f

Browse files
committed
Merge pull request #1 from spooon/master
some improvements
2 parents d95c543 + 168671e commit a39cc1f

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

docs/arrays/array_merge.txt

+8
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ array array_merge ( array $array1 [, array $array2 [, array $... ]] )
77
*Perl
88

99
>|perl|
10+
# for concatenating arrays
1011
my @result = ( @array1, @array2 , @array3 ... );
1112

1213
push @result, @array1;
1314
push @result, @array2;
15+
16+
# for merging hashes
17+
my %result = ( %hash1, %hash2 ... );
18+
19+
# alternately:
20+
my %result = %hash1;
21+
@result { keys %hash2 } = values %hash2;
1422
||<

docs/arrays/array_rand.txt

+17-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ mixed array_rand ( array $input [, int $num_req = 1 ] )
99
**Perl
1010

1111
>|Perl|
12-
use List::Util qw(shuffle);
13-
@rand = shuffle @array;
12+
# FOR ARRAYS
13+
# when $num_req == 1:
14+
my $result = int rand @array;
15+
16+
# otherwise:
17+
use Data::Random qw/ rand_set /;
18+
my @results = rand_set ( set => [0 .. $#array], size => $num_req );
19+
20+
# FOR HASHES
21+
# when $num_req == 1:
22+
my @keys = keys %hash;
23+
my $result = $keys [ int rand @keys ];
24+
25+
# otherwise:
26+
use Data::Random qw/ rand_set /;
27+
my @keys = keys %hash;
28+
my @results = @keys [ rand_set ( set => [0 .. $#array], size => $num_req ) ];
1429
||<

docs/arrays/array_sum.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ $sum += $_ for @array;
2020

2121
# List::Util を使って
2222
use List::Util qw/ sum /;
23-
$sum = sum @array;
23+
$sum = sum 0, @array; # the 0 is to allow it to work with empty arrays too
2424
||<

0 commit comments

Comments
 (0)