Retrieving then deleting elements of a list (references)

Discussion in 'Perl Misc' started by Arvin Portlock, Dec 9, 2003.

  1. Hello!

    I have a list of data structures. Each struct has a number of
    fields. I need to retrieve certain elements of the list based
    on the value of one of its fields, then delete those elements.
    The reason I need to do this is that after I treat these first
    special cases, I need to go through and process all of the
    remaining elements in a generic way. I don't want to process
    those special elements twice, so I need to remove them the first
    time I process them. Is that clear?

    I imagine I could do something like popping unprocessed elements
    to a new array but my attempts at this have been awkward and
    I'm looking for something very efficient and elegant. Since it's
    the type of thing I need to do frequently I figure it should be
    something I can add to my bag of perl tools.

    So I first want to process those elements that have a 'type'
    field containing 'SERIES', i.e., $elt->{type} =~ /SERIES/:

    my @series = grep ($_->{type} =~ /SERIES/, @{$data->{elements}});
    foreach my $ser (@series) {
    etc...
    }
    my @items = grep ($_->{type} =~ /ITEM/, @{$data->{elements}});
    foreach my $item (@items) {
    etc...
    }
    ## Now process everything that's left over
    foreach my $elt (@{$data->{elements}}) {
    etc...
    }

    Note, not every element has a 'type' field.

    Except you can see in the above scenario, that last foreach
    processes everything in $data->{elements}, not just anything
    "left over."

    Any elegant suggestions? Is this something map could do?
    Arvin Portlock, Dec 9, 2003
    #1
    1. Advertising

  2. Arvin Portlock wrote:
    >
    > I have a list of data structures. Each struct has a number of
    > fields. I need to retrieve certain elements of the list based
    > on the value of one of its fields, then delete those elements.
    > The reason I need to do this is that after I treat these first
    > special cases, I need to go through and process all of the
    > remaining elements in a generic way. I don't want to process
    > those special elements twice, so I need to remove them the first
    > time I process them. Is that clear?
    >
    > I imagine I could do something like popping unprocessed elements
    > to a new array but my attempts at this have been awkward and
    > I'm looking for something very efficient and elegant. Since it's
    > the type of thing I need to do frequently I figure it should be
    > something I can add to my bag of perl tools.
    >
    > So I first want to process those elements that have a 'type'
    > field containing 'SERIES', i.e., $elt->{type} =~ /SERIES/:
    >
    > my @series = grep ($_->{type} =~ /SERIES/, @{$data->{elements}});
    > foreach my $ser (@series) {
    > etc...
    > }
    > my @items = grep ($_->{type} =~ /ITEM/, @{$data->{elements}});
    > foreach my $item (@items) {
    > etc...
    > }
    > ## Now process everything that's left over
    > foreach my $elt (@{$data->{elements}}) {
    > etc...
    > }
    >
    > Note, not every element has a 'type' field.
    >
    > Except you can see in the above scenario, that last foreach
    > processes everything in $data->{elements}, not just anything
    > "left over."
    >
    > Any elegant suggestions? Is this something map could do?



    You could store the left-overs in another array.


    my ( @series, @left_overs );
    push @{ $_->{type} =~ /SERIES/ ? \@series : \@left_overs }, $_ for @{$data->{elements}};
    foreach my $ser ( @series ) {
    etc...
    }
    my @items
    push @{ $_->{type} =~ /ITEM/ ? \@items : \@left_overs }, $_ for splice @left_overs;
    foreach my $item ( @items ) {
    etc...
    }
    ## Now process everything that's left over
    foreach my $elt ( @left_overs ) {
    etc...
    }


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Dec 10, 2003
    #2
    1. Advertising

  3. Oh, that is REALLY excellent! Thank you very much!


    > You could store the left-overs in another array.
    >
    >
    > my ( @series, @left_overs );
    > push @{ $_->{type} =~ /SERIES/ ? \@series : \@left_overs }, $_ for
    > @{$data->{elements}};
    > foreach my $ser ( @series ) {
    > etc...
    > }
    > my @items
    > push @{ $_->{type} =~ /ITEM/ ? \@items : \@left_overs }, $_ for splice
    > @left_overs;
    > foreach my $item ( @items ) {
    > etc...
    > }
    > ## Now process everything that's left over
    > foreach my $elt ( @left_overs ) {
    > etc...
    > }
    >
    >
    > John
    Arvin Portlock, Dec 10, 2003
    #3
    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. flupke
    Replies:
    5
    Views:
    359
    Larry Bates
    Oct 29, 2004
  2. Adam Hartshorne
    Replies:
    2
    Views:
    373
    Nitin Motgi
    Jan 27, 2006
  3. Harry Barker
    Replies:
    2
    Views:
    514
    Alf P. Steinbach
    Apr 19, 2006
  4. Replies:
    0
    Views:
    277
  5. Just in

    Deleting blank elements from a list

    Just in, Jul 2, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    131
    Gunnar Hjalmarsson
    Jul 3, 2004
Loading...

Share This Page