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) ; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Generating an array of n identical elements
by blakem (Monsignor) on Sep 16, 2002 at 08:50 UTC | |
by bronto (Priest) on Sep 16, 2002 at 09:50 UTC | |
by BrowserUk (Patriarch) on Sep 16, 2002 at 10:14 UTC | |
by bronto (Priest) on Sep 16, 2002 at 10:49 UTC | |
by Aristotle (Chancellor) on Sep 16, 2002 at 11:46 UTC | |
| |
by Aristotle (Chancellor) on Sep 16, 2002 at 10:54 UTC | |
Re: Generating an array of n identical elements
by Aristotle (Chancellor) on Sep 16, 2002 at 09:23 UTC |
Back to
Cool Uses for Perl