creating an array of $n elements

S

samwyse

In Python, I can say "7 * (1,)" to create a list of 7 items. If I need
a list in Perl with all elements initialized to the same value, is
there a one-liner? The best I can come up with is "$a[$n] = 0; pop
@a;" which will give me an array of $n undefs, but it seems a bit
inefficient. Any ideas? Thanks.
 
M

Michael Vilain

samwyse said:
In Python, I can say "7 * (1,)" to create a list of 7 items. If I need
a list in Perl with all elements initialized to the same value, is
there a one-liner? The best I can come up with is "$a[$n] = 0; pop
@a;" which will give me an array of $n undefs, but it seems a bit
inefficient. Any ideas? Thanks.

what's wrong with a simple declaration with initialization?

@a = (0, 0, 0, 0, 0, 0, 0);

Or have I missed something?
 
J

Jürgen Exner

samwyse said:
If I need
a list in Perl with all elements initialized to the same value, is
there a one-liner?

perldoc perlop ==> Multiplicative Operators ==> x

@ones = (1) x 80; # a list of 80 1's

jue
 
J

Jürgen Exner

Michael Vilain said:
samwyse said:
In Python, I can say "7 * (1,)" to create a list of 7 items. If I need
a list in Perl with all elements initialized to the same value, is
there a one-liner? The best I can come up with is "$a[$n] = 0; pop
@a;" which will give me an array of $n undefs, but it seems a bit
inefficient. Any ideas? Thanks.

what's wrong with a simple declaration with initialization?

@a = (0, 0, 0, 0, 0, 0, 0);

Or have I missed something?

@a = (0) x 7;

seems to be easier on the eyes.

jue
 
J

John W. Krahn

samwyse said:
In Python, I can say "7 * (1,)" to create a list of 7 items. If I need
a list in Perl with all elements initialized to the same value, is
there a one-liner? The best I can come up with is "$a[$n] = 0; pop
@a;" which will give me an array of $n undefs, but it seems a bit
inefficient. Any ideas? Thanks.

$ perl -le'
use Data::Dumper;
my $n = 7;
my @a = ( undef ) x $n;
print Dumper \@a;
'
$VAR1 = [
undef,
undef,
undef,
undef,
undef,
undef,
undef
];


John
 
C

Charlton Wilbur

s> If I need a list in Perl with all elements initialized to the
s> same value, is there a one-liner?

Look at the x operator in perldoc perlop.

Charlton
 
S

samwyse

In Python, I can say "7 * (1,)" to create a list of 7 items. If I need
a list in Perl with all elements initialized to the same value, is
there a one-liner?  The best I can come up with is "$a[$n] = 0; pop
@a;" which will give me an array of $n undefs, but it seems a bit
inefficient.  Any ideas?  Thanks.

Thanks to everyone. For some reason, I was thinking that the 'x'
operator only applied to strings. D'oh!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top