rovf has asked for the wisdom of the Perl Monks concerning the following question:
I would like to write a function makelist($x,$n), which returns a list of $n elements, each element being the scalar $x. A pretty naive implementation looks like this:
However I wonder if there is a more concise way of doing this, maybe one where I don't need a temporary @list. If it would be a string of $n occurances of $x, I would simply write $x x $n. Can I do something similarily simple for lists?sub makelist { my ($elem,$count)=@_; my @list; $list[$count-1]=0; map { $elem } @list }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: generating a list of identical elements
by jethro (Monsignor) on Jan 19, 2011 at 11:33 UTC | |
by AnomalousMonk (Archbishop) on Jan 19, 2011 at 11:41 UTC | |
by rovf (Priest) on Jan 19, 2011 at 11:48 UTC | |
by raybies (Chaplain) on Jan 19, 2011 at 13:45 UTC | |
|
Re: generating a list of identical elements
by JavaFan (Canon) on Jan 19, 2011 at 11:41 UTC | |
|
Re: generating a list of identical elements
by tilly (Archbishop) on Jan 19, 2011 at 16:53 UTC | |
|
Re: generating a list of identical elements
by fisher (Priest) on Jan 19, 2011 at 11:29 UTC |