Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Inserting the same thing multi times into array.

Reply
Thread Tools

Inserting the same thing multi times into array.

 
 
Richard S Beckett
Guest
Posts: n/a
 
      11-19-2003
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


 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      11-19-2003
"Richard S Beckett" <(E-Mail Removed)> 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.
~ http://www.velocityreviews.com/forums/(E-Mail Removed) ~ Jorge Luis Borges, 'The Babylon Lottery'
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-19-2003
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

 
Reply With Quote
 
Ferine Boncer
Guest
Posts: n/a
 
      11-19-2003
Richard S Beckett <(E-Mail Removed)> 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
 
Reply With Quote
 
Richard S Beckett
Guest
Posts: n/a
 
      11-19-2003
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


 
Reply With Quote
 
Ferine Boncer
Guest
Posts: n/a
 
      11-19-2003
Richard S Beckett <(E-Mail Removed)> 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
 
Reply With Quote
 
Richard S Beckett
Guest
Posts: n/a
 
      11-19-2003
> 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


 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      11-19-2003
Ben Morrow <(E-Mail Removed)> writes:

> "Richard S Beckett" <(E-Mail Removed)> 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\\
 
Reply With Quote
 
Richard S Beckett
Guest
Posts: n/a
 
      11-20-2003
> >> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question on multi-thread execute the same method of same classinstance QQ Java 6 03-28-2008 02:56 PM
Inserting Page Load Times into HTML Source as a HTML Comment ste-m Javascript 1 11-01-2006 03:18 AM
how to use same user control x number of times on same asp page David Hubbard ASP .Net 2 01-12-2006 03:56 PM
one thing solved, but other terrible thing occur... Zam ASP General 1 03-14-2005 06:09 PM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM



Advertisments