Retrieving then deleting elements of a list (references)

A

Arvin Portlock

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?
 
J

John W. Krahn

Arvin said:
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
 

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,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top