Inserting the same thing multi times into array.

R

Richard S Beckett

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.
 
B

Ben Morrow

Richard S Beckett said:
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
 
G

Gunnar Hjalmarsson

Richard said:
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;
 
F

Ferine Boncer

Richard S Beckett said:
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'
 
R

Richard S Beckett

A very nice man said:
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?
 
F

Ferine Boncer

Richard S Beckett said:
A very nice man said:

Oooh! So close! :) Thanks.

Someone else said:

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

:)
 
R

Richard S Beckett

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.
 
B

Brian McCauley

Ben Morrow said:
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\\
 
R

Richard S Beckett

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.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top