Inserting the same thing multi times into array.

Discussion in 'Perl Misc' started by Richard S Beckett, Nov 19, 2003.

  1. Guys,

    To insert 7 'nothings' into an array, I did this:

    splice @array, 2, 0, ("", "", "", "", "", "", "");

    This leaves me with ($array[0], $array[1], "", "", "", "", "", "", "",
    $array[2], $array[3]...)

    Is there a way that I can do something like?:

    splice @array, 2, 0, 7*(""); # i.e. 7 lots of ""

    Thanks.
    --
    R.
    GPLRank +79.699
    Richard S Beckett, Nov 19, 2003
    #1
    1. Advertising

  2. Richard S Beckett

    Ben Morrow Guest

    "Richard S Beckett" <> wrote:
    > To insert 7 'nothings' into an array, I did this:


    ITY know the difference between "" and undef (which is a lot closer to
    a 'nothing' :).

    > splice @array, 2, 0, ("", "", "", "", "", "", "");
    >
    > This leaves me with ($array[0], $array[1], "", "", "", "", "", "", "",
    > $array[2], $array[3]...)
    >
    > Is there a way that I can do something like?:
    >
    > splice @array, 2, 0, 7*(""); # i.e. 7 lots of ""


    splice @array, 2, 0, map "", 1..7; #untested

    Ben

    --
    Like all men in Babylon I have been a proconsul; like all, a slave ... During
    one lunar year, I have been declared invisible; I shrieked and was not heard,
    I stole my bread and was not decapitated.
    ~ ~ Jorge Luis Borges, 'The Babylon Lottery'
    Ben Morrow, Nov 19, 2003
    #2
    1. Advertising

  3. Richard S Beckett wrote:
    > To insert 7 'nothings' into an array, I did this:
    >
    > splice @array, 2, 0, ("", "", "", "", "", "", "");
    >
    > This leaves me with ($array[0], $array[1], "", "", "", "",
    > "", "", "", $array[2], $array[3]...)
    >
    > Is there a way that I can do something like?:
    >
    > splice @array, 2, 0, 7*(""); # i.e. 7 lots of ""


    splice @array, 2, 0, ("")x7;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 19, 2003
    #3
  4. Richard S Beckett <> wrote:
    > Guys,
    >
    > To insert 7 'nothings' into an array, I did this:
    >
    > splice @array, 2, 0, ("", "", "", "", "", "", "");
    >
    > This leaves me with ($array[0], $array[1], "", "", "", "", "", "", "",
    > $array[2], $array[3]...)
    >
    > Is there a way that I can do something like?:
    >
    > splice @array, 2, 0, 7*(""); # i.e. 7 lots of ""


    Yes, use the x operator...

    Pls do a perldoc perlop and search for 'repeat'

    >
    > Thanks.



    --
    FB
    Ferine Boncer, Nov 19, 2003
    #4
  5. A very nice man said:
    > > splice @array, 2, 0, 7*(""); # i.e. 7 lots of ""

    > splice @array, 2, 0, ("")x7;


    Oooh! So close! :) Thanks.

    Someone else said:
    > Pls do a perldoc perlop and search for 'repeat'


    Well, I tried perldoc, but without asking this question, I wouldn't have
    thought of trying the word 'repeat'. Similarly, I searched google, before
    asking. As both were fruitless I asked. That _is_ what this newsgroup is
    for, right?
    --
    R.
    GPLRank +79.699
    Richard S Beckett, Nov 19, 2003
    #5
  6. Richard S Beckett <> wrote:
    > A very nice man said:
    > > > splice @array, 2, 0, 7*(""); # i.e. 7 lots of ""

    > > splice @array, 2, 0, ("")x7;

    >
    > Oooh! So close! :) Thanks.
    >
    > Someone else said:
    > > Pls do a perldoc perlop and search for 'repeat'

    >
    > Well, I tried perldoc, but without asking this question, I wouldn't have
    > thought of trying the word 'repeat'. Similarly, I searched google, before
    > asking. As both were fruitless I asked. That _is_ what this newsgroup is
    > for, right?


    I also mentioned that the answer is to use the operator 'x' and *then*
    requested you to read the perldoc and also directed you to search for
    the appropriate word so that you know all the ins-and-outs of the
    operator.

    I wasn't trying to be unhelpful...

    :)


    --
    FB
    Ferine Boncer, Nov 19, 2003
    #6
  7. > I also mentioned that the answer is to use the operator 'x' and *then*
    > requested you to read the perldoc and also directed you to search for
    > the appropriate word so that you know all the ins-and-outs of the
    > operator.
    > I wasn't trying to be unhelpful...
    > :)


    Sorry.
    --
    R.
    GPLRank +79.699
    Richard S Beckett, Nov 19, 2003
    #7
  8. Ben Morrow <> writes:

    > "Richard S Beckett" <> wrote:


    > > splice @array, 2, 0, ("", "", "", "", "", "", "");


    > > Is there a way that I can do something like?:
    > >
    > > splice @array, 2, 0, 7*(""); # i.e. 7 lots of ""

    >
    > splice @array, 2, 0, map "", 1..7; #untested


    As others have pointed out the x operator is more appropriate than map()
    in this case.

    It is, however, worth pointing out that if you were inserting
    reference to anonymous things rather than strings then you probably
    would want map().

    splice @array, 2, 0, map [], 1..7; # DWIM - refs to 7 empty arrays
    splice @array, 2, 0, ([]) x 7; # !DWIM - 7 refs to one empty array

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley, Nov 19, 2003
    #8
  9. > >> splice @array, 2, 0, map [], 1..7; # DWIM - refs to 7 empty arrays
    > >> splice @array, 2, 0, ([]) x 7; # !DWIM - 7 refs to one empty
    > >> array


    > The first creates a new array reference seven times, then sticks those
    > seven distinct array references into @array.
    >
    > The second creates a new array reference, duplicates that ONE array
    > reference seven times, and sticks those copies into @array.
    >
    > In the first case, you'll have seven independent values. In the second
    > case, you'll have seven references to the SAME array ref -- if you assign
    > to one of them, you're assigning to *all* of them.
    >
    > Similarly,
    >
    > @r = map rand, 1..7;
    >
    > will insert seven different random numbers into @r, while
    >
    > @r = (rand) x 7;
    >
    > will insert one random number into @r seven times.


    Ah! Thanks Eric. That's well worth knowing.
    --
    R.
    GPLRank +79.699
    Richard S Beckett, Nov 20, 2003
    #9
    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. David Hubbard
    Replies:
    2
    Views:
    817
    David Hubbard
    Jan 12, 2006
  2. qu0ll
    Replies:
    8
    Views:
    7,550
    Patricia Shanahan
    Oct 22, 2006
  3. Zam
    Replies:
    1
    Views:
    219
    Mark Schupp
    Mar 14, 2005
  4. Thomas Greenwood
    Replies:
    7
    Views:
    158
    David Jacobs
    May 15, 2011
  5. ste-m
    Replies:
    1
    Views:
    298
Loading...

Share This Page