building anonymous arrays and adding reference to main array

W

wana

suppose you have this:

@array1 = ();
@array2 = ();
@array3 = ();
$count = 0;
foreach(@array1)
{
if (count % 100 == 0)
{
#the code in question:
push @array3, \@array2;
@array2 = ();
}
push @array2, $_;
count++;
}

I don't think this will work as expected because what I really want is
to create an anonymous array from @array2 and push a reference to that
onto @array3.

Or at least I think that's the way to do it but I'm not sure how to
create an anonymous array from the contents of another array.

Thanks!

wana
 
A

Anno Siegel

wana said:
suppose you have this:

@array1 = ();
@array2 = ();
@array3 = ();
$count = 0;
foreach(@array1)
{
if (count % 100 == 0)
{
#the code in question:
push @array3, \@array2;
@array2 = ();
}
push @array2, $_;
count++;
}

I don't think this will work as expected because what I really want is

Oh man... So what do you expect? How are we supposed to know when
all you present is code that *doesn't* do what you want?
to create an anonymous array from @array2 and push a reference to that
onto @array3.

Or at least I think that's the way to do it but I'm not sure how to
create an anonymous array from the contents of another array.

Maybe you need "[@array2]" instead of "\@array2". There's no way
of telling.

Anno
 
B

Brian McCauley

wana said:
suppose you have this:

@array1 = ();
@array2 = ();
@array3 = ();
$count = 0;
foreach(@array1)
{
if (count % 100 == 0)
{
#the code in question:
push @array3, \@array2;
@array2 = ();
}
push @array2, $_;
count++;
}

Since @array1 is known empty your whole problem optomizes away completely.

I shall assume instead that @array1 should contain several hundred elements.
I don't think this will work as expected because what I really want is
to create an anonymous array from @array2 and push a reference to that
onto @array3.

Or at least I think that's the way to do it but I'm not sure how to
create an anonymous array from the contents of another array.

I'm curious how you come to know the term "anonymous array" and yet
don't know how to create one. Where did you come upon this term?

There are three ways to create an anonymous array:

1) create a nonymous one and let its name drop out of scope
2) use the explicit anonymous array constructor [ LIST ]
3) use an undefined lvalue in an arrayref context (autovivification)

Anyhow your orginial problem is much more simply done...

my @array3;
{
my @array2 = @array1;
push @array3, [ splice @array2, 0, 100 ] while @array2;
}

Of course if you don't need to preserve @array1 then you can consume it
directly and do away with @array2.

If you look at previous threads asking substancially the same question
you'll see many other solutions using slices and map() but I think the
one using splice and while is the most elegant.
 
W

wana

Anyhow your orginial problem is much more simply done...
my @array3;
{
my @array2 = @array1;
push @array3, [ splice @array2, 0, 100 ] while @array2;
}

Of course if you don't need to preserve @array1 then you can consume it
directly and do away with @array2.

If you look at previous threads asking substancially the same question
you'll see many other solutions using slices and map() but I think the
one using splice and while is the most elegant.

I am sorry to have asked a question that is answered so clearly in
Programming Perl. I did a little reading after posting it and wished
I could retract my easily answered question.

On the other hand, I am glad that I did now because the splice thing
is really cool. I have my book open now to p. 793 (3rd ed.
revised,updated). I really have to read this book at least once and
probably 2,3 times before asking basic questions. I need a time warp
to go into so I will have a few weeks to read and no real time will
have passed.

Thanks!

wana
 
E

Eric Bohlman

(e-mail address removed) (wana) wrote in
On the other hand, I am glad that I did now because the splice thing
is really cool. I have my book open now to p. 793 (3rd ed.
revised,updated). I really have to read this book at least once and
probably 2,3 times before asking basic questions. I need a time warp
to go into so I will have a few weeks to read and no real time will
have passed.

If you can easily deal with explanations that are a bit more terse than
those found in the Camel book, you might try:

perldoc perlreftut
perldoc perlref
perldoc perldsc

which should get you up to speed in much less time than "a few weeks."
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top