Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Tidying up some dereferencing code

by Basilides (Friar)
on Aug 13, 2002 at 14:04 UTC ( [id://189793]=perlquestion: print w/replies, xml ) Need Help??

Basilides has asked for the wisdom of the Perl Monks concerning the following question:

I have some code which dereferences a couple of pointers, and it works, but, as you'll be able to see, it looks pretty clunky--using a temporary variable etc. In part I think this is because Perl doesn't mind dereferencing a simple scalar like this, @$myarray, but when OO comes into play, it doesn't like @$self->{_myarray}. Would somebody tell me if there is a neat way of doing what I'm trying to do.

Please note that I am trying to tidy up a project for college. If you consider this to be homework trolling, don't read any further, but don't flame me either :-)

$self->{_words} is a pointer to an array of objects. Calling the pass_matches method of any of those objects will return a pointer to another array. I'd like to run a nested loop through the two arrays. (Actually, with the outer array, I only want to loop till the penultimate element, hence no foreach.) Here's what I'm doing now:

my $ref = $self->{_words}; for (my $i = 0; $i < @$ref - 1; $i++) { my $temp = @$ref[$i]->pass_matches; my @curr_word = @$temp; foreach my $curr (@curr_word) { #blah blah blah } }
Any ideas?

Cheers
Dennis

Replies are listed 'Best First'.
Re: Tidying up some dereferencing code
by Abigail-II (Bishop) on Aug 13, 2002 at 14:08 UTC
    The syntax in Perl is simple. Where ever you have @name you may replace name with a block whose result is a reference of the appropriate type.

    So, in your case: @{$self -> {_words}}.

    @$myarray is just a short hand notation for @{$myarray}.

    Abigail

(jeffa) Re: Tidying up some dereferencing code
by jeffa (Bishop) on Aug 13, 2002 at 16:34 UTC
Re: Tidying up some dereferencing code
by uwevoelker (Pilgrim) on Aug 13, 2002 at 14:15 UTC
    foreach (@{$self->{_words}}) { foreach my $curr (@{$_->pass_matches}) { # ... } }
    Better use foreach instead of for, when you iterate over an array.

      "Better use foreach instead of for, when you iterate over an array."

      Why, they do the exact same thing:
      print $_ for qw(foo bar baz); print $_ foreach qw(foo bar baz); $ perl -MO=Deparse foo.pl foreach $_ ('foo', 'bar', 'baz') { print $_; } foreach $_ ('foo', 'bar', 'baz') { print $_; } foo.pl syntax OK
      I will admit that i tend to use foreach when i iterate over a 'collection', but never hardly ever (never say never...) when iterating over arrays. As a matter of fact, i have really started to favor for over foreach because foreach is just too wordy. But, consitency is alway king. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://189793]
Approved by aufrank
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found