You are an old Matlab(TM) user, aren't you? Now your boss asked you to learn perl and you really miss the dear old ones function. You were used to create n-lenght arrays -er... vectors!- of identical elements by simply doing
fives = 5*ones(1:20) ;and now in perl you don't find any ones.
Stop crying and use map!
# An array of twenty fives: my @fives = map 5,(1..20) ; # An array of 3 Xes # (You must be at least 18 to watch this!) my @porn = map 'X',(1..3) ; my @whatever = replicate($x,$n) ; sub replicate { my ($x,$n) = @_ ; return () if $n < 1 ; return map $x,(1..$n) ; }
Back to
Cool Uses for Perl