building anonymous arrays and adding reference to main array

Discussion in 'Perl Misc' started by wana, Oct 12, 2004.

  1. wana

    wana Guest

    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
     
    wana, Oct 12, 2004
    #1
    1. Advertising

  2. wana

    Anno Siegel Guest

    wana <> wrote in comp.lang.perl.misc:
    > 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
     
    Anno Siegel, Oct 12, 2004
    #2
    1. Advertising

  3. wana wrote:

    > 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.
     
    Brian McCauley, Oct 12, 2004
    #3
  4. wana

    wana Guest

    > 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
     
    wana, Oct 13, 2004
    #4
  5. wana

    Eric Bohlman Guest

    (wana) wrote in
    news::

    > 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."
     
    Eric Bohlman, Oct 17, 2004
    #5
    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. John M
    Replies:
    1
    Views:
    1,239
    Kumar Reddi
    May 29, 2005
  2. Reporter
    Replies:
    3
    Views:
    485
    Mike Schilling
    May 12, 2007
  3. ravi
    Replies:
    28
    Views:
    997
    Richard Heathfield
    Sep 26, 2007
  4. Philipp
    Replies:
    21
    Views:
    1,156
    Philipp
    Jan 20, 2009
  5. Replies:
    1
    Views:
    227
Loading...

Share This Page