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