Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Couple of array/reference type questions

Reply
Thread Tools

Couple of array/reference type questions

 
 
Arvin Portlock
Guest
Posts: n/a
 
      07-13-2003
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!



 
Reply With Quote
 
 
 
 
Arvin Portlock
Guest
Posts: n/a
 
      07-13-2003
Now I remember the other one:

How do I find the last subscript of a reference to an
array without first dereferencing it? You know, with the
$# syntax.

Thank you again,

 
Reply With Quote
 
 
 
 
Arvin Portlock
Guest
Posts: n/a
 
      07-13-2003
Bart Lateur wrote:

> Arvin Portlock wrote:
>
>
> >How do I find the last subscript of a reference to an
> >array without first dereferencing it? You know, with the
> >$# syntax.

>
>
> $#{$ref}
> or
> $#$ref


Thanks! I won't even ATTEMPT to explain why I never thought
to try that.

 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      07-13-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Arvin Portlock <> wrote in news:besdb4$1a02$1
@agate.berkeley.edu:



> 1. Grepping an array of references possible?

....
>
> 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;
> }
> }


@mybooks = grep $_->{title} =~ /Gone/, @books;

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


$num = grep $_->{title} =~ /Gone/, @books;

> How about if I only wanted to know whether
> the array contained ANY book whose title contained that word,


if (grep $_->{title} =~ /Gone/, @books) { ...}

But you're right, a foreach loop is not a bad way to do that, especially
if the list is large, and you use 'last' once you have found a match.

> 2. Replacing elements of an array of references.

....
>
> 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 ()?


No; a foreach loop is your best bet here:

foreach my $book (@$books)
{
$book = $new_edition if $book->{title} eq '....';
}

- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPxHLkmPeouIeTNHoEQIYRACeN8hFSv1+6HlLNrHju1jqd4 lHO3AAoPn2
hYnPru0bZbhNYPFSlmAk8oBw
=+f9C
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      07-14-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Arvin Portlock <> wrote in news:besj3q$1dti$1
@agate.berkeley.edu:

> I'm printing this out and pinning it to my computer! Thanks you so
> much Eric.
>
> BTW, yes, the foreach is cleaner than my for. I forgot they're
> references after all and I needn't go through all that subscript
> nonsense.


Just remember: An array reference is just like an array, except that you
have to go through one extra step to dereference it when you use it.

- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPxJ8T2PeouIeTNHoEQLy8QCeO/mpBm8N2/el4ow12FtqeqgExagAniql
qgvs3x3r8HyKpYqe/e7LTzjn
=OXbq
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A couple of questions regarding IOS software Andrew Hodgson Cisco 4 02-06-2006 04:49 PM
A couple of T-Bird questions Dale Brisket Firefox 2 10-02-2005 10:25 PM
Newbie questions - Couple of VC++ questions regarding dlls and VB6 Ali Syed C Programming 3 10-13-2004 10:15 PM
1721 ADLS - a couple of questions ? Thomas Cisco 0 01-30-2004 04:42 PM
Couple of Reflection Type Questions Paul Joel Java 5 09-24-2003 05:30 PM



Advertisments