creating an array of $n elements

Discussion in 'Perl Misc' started by samwyse, Nov 5, 2008.

  1. samwyse

    samwyse Guest

    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.
     
    samwyse, Nov 5, 2008
    #1
    1. Advertising

  2. In article
    <>,
    samwyse <> wrote:

    > 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?

    --
    DeeDee, don't press that button! DeeDee! NO! Dee...
    [I filter all Goggle Groups posts, so any reply may be automatically by ignored]
     
    Michael Vilain, Nov 5, 2008
    #2
    1. Advertising

  3. samwyse <> wrote:
    > 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ürgen Exner, Nov 5, 2008
    #3
  4. Michael Vilain <> wrote:
    >In article
    ><>,
    > samwyse <> wrote:
    >
    >> 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ürgen Exner, Nov 5, 2008
    #4
  5. samwyse wrote:
    > 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
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Nov 5, 2008
    #5
  6. >>>>> "s" == samwyse <> writes:

    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


    --
    Charlton Wilbur
     
    Charlton Wilbur, Nov 5, 2008
    #6
  7. samwyse

    samwyse Guest

    On Nov 4, 9:31 pm, samwyse <> wrote:
    > 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!
     
    samwyse, Nov 5, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. diffused
    Replies:
    9
    Views:
    738
    Oscar kind
    Aug 1, 2004
  2. Shalini
    Replies:
    2
    Views:
    508
    Brian Genisio
    Jan 9, 2004
  3. P
    Replies:
    1
    Views:
    1,188
    Joe Kesselman
    Jul 7, 2006
  4. Replies:
    10
    Views:
    686
  5. John Harvey
    Replies:
    0
    Views:
    172
    John Harvey
    Jan 9, 2006
Loading...

Share This Page