A
Arvin Portlock
There are several array type things I find myself doing all
the time and I'd like to know, once and for all, the best way
to do them. For the moment I'm most interested in core language
commands rather than specialized modules, but I'm interested to
hear about modules as well if they're really useful.
1. Grepping an array of references possible?
If I have an array of hash references, is there a way to use
grep() to find those elements whose values match a given value?
I.e., is there a grep() way to do this? I just like grep () and
I'm confortable with it and want to use it as much as I can.
my $ref = {};
$ref->{title} = 'Gone with the wind';
$ref->{date} = '1952';
push @books, $ref;
my @mybooks;
foreach my $book (@books) {
if ($book->{title} =~ /Gone/) {
push @mybooks, $book;
}
}
How would I use grep () in a scalar context if I just wanted
to know how many books contained "Gone" and didn't want to
create a new array? How about if I only wanted to know whether
the array contained ANY book whose title contained that word,
without the overhead of counting them all? For that last I'm
guessing a foreach loop is as good as it gets, but since I do
it all the time I just want to make sure I'm not missing any-
thing magical.
2. Replacing elements of an array of references.
Suppose I'm a book seller who just got in a new shipment of
"Gone with the wind." I want to replace the old edition in
my array with the new edition. This is the best I can come up
with (preserving order is important):
my $new_edition = {};
$new_edition->{title} = 'Gone with the wind';
$new_edition->{date} = '2003';
for (my $i = 0; $i++; $i <= $#books) {
$books->[$i] = $new_edition if $books->[$i]->{title}
eq 'Gone with the wind';
}
Any way to do this with grep ()?
Those are the only two I can think of at the moment. Thanks for
any advice!
the time and I'd like to know, once and for all, the best way
to do them. For the moment I'm most interested in core language
commands rather than specialized modules, but I'm interested to
hear about modules as well if they're really useful.
1. Grepping an array of references possible?
If I have an array of hash references, is there a way to use
grep() to find those elements whose values match a given value?
I.e., is there a grep() way to do this? I just like grep () and
I'm confortable with it and want to use it as much as I can.
my $ref = {};
$ref->{title} = 'Gone with the wind';
$ref->{date} = '1952';
push @books, $ref;
my @mybooks;
foreach my $book (@books) {
if ($book->{title} =~ /Gone/) {
push @mybooks, $book;
}
}
How would I use grep () in a scalar context if I just wanted
to know how many books contained "Gone" and didn't want to
create a new array? How about if I only wanted to know whether
the array contained ANY book whose title contained that word,
without the overhead of counting them all? For that last I'm
guessing a foreach loop is as good as it gets, but since I do
it all the time I just want to make sure I'm not missing any-
thing magical.
2. Replacing elements of an array of references.
Suppose I'm a book seller who just got in a new shipment of
"Gone with the wind." I want to replace the old edition in
my array with the new edition. This is the best I can come up
with (preserving order is important):
my $new_edition = {};
$new_edition->{title} = 'Gone with the wind';
$new_edition->{date} = '2003';
for (my $i = 0; $i++; $i <= $#books) {
$books->[$i] = $new_edition if $books->[$i]->{title}
eq 'Gone with the wind';
}
Any way to do this with grep ()?
Those are the only two I can think of at the moment. Thanks for
any advice!