CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Dereferencing an array element inside the foreach loop

 



Jean
User


Feb 24, 2002, 3:39 AM

Post #1 of 7 (1201 views)
Dereferencing an array element inside the foreach loop Can't Post

I have a question for the people more experienced in Perl than myself: (usually that is the case)

In the following example, I'm looping through the array of hash refs and would like to dereference the array elements right in the foreach loop, e.g. I want to do in one line what I'm doing here in two:

[perl] foreach my $Header (@Headers) {

%Header = %{$Header}; # Dereference the array element

...

}

[/perl]


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


mhx
Enthusiast / Moderator

Feb 24, 2002, 4:12 AM

Post #2 of 7 (1199 views)
Re: [Jean] Dereferencing an array element inside the foreach loop [In reply to] Can't Post

Why? Doesn't make very much sense to me... (Maybe that's why I'm sure it's not possible.)

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Paul
Enthusiast

Feb 24, 2002, 4:22 AM

Post #3 of 7 (1197 views)
Re: [Jean] Dereferencing an array element inside the foreach loop [In reply to] Can't Post

>>I want to do in one line what I'm doing here in two: <<

for (@Headers) { %Header = %{$_} }

Thats one line Laugh

I hate capital letters in perl :)


(This post was edited by RedRum on Feb 24, 2002, 4:22 AM)


Jean
User


Feb 24, 2002, 4:57 AM

Post #4 of 7 (1194 views)
Re: [RedRum] Dereferencing an array element inside the foreach loop [In reply to] Can't Post

Thanks Paul,

Although it's not really one line (that's because I've got additional lines in the loop block) your solution has saved me the need for $Header variable which was used only for dereferencing it in the next line ;-)

And due to being raised on Pascal some 9 years ago I still prefer MixedCase variables and I can live with it as long as I use strict and use warnings because of the above Pascal ;-)



To Mhx: You're correct - there isn't much use to the idea - previously I was using the reference in the rest of the code, but for some obscure reason decided to change the code to work with the hash instead of the reference to it (doesn't make much sense except readability for future maintenance).


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


mhx
Enthusiast / Moderator

Feb 24, 2002, 5:52 AM

Post #5 of 7 (1190 views)
Re: [Jean] Dereferencing an array element inside the foreach loop [In reply to] Can't Post

Not only does it make little sense, it can also slow down your code tremendously:

[perl]
#!/usr/bin/perl -w
use strict;
use Benchmark;

my @aoh = map {{ map {( $_ => rand 100 )} 'aa'..'zz' }} 1 .. 100;
my $sum = 0;

timethese( 500, {
'Refs' => sub {
for my $val ( @aoh ) {
$sum += $val->{aa} + $val->{cu} + $val->{yy};
}
},
'Hash' => sub {
for my $val ( @aoh ) {
my %val = %$val;
$sum += $val{aa} + $val{cu} + $val{yy};
}
}
});
[/perl]


Code
mhx@r2d2:~/src/perl/test > perl bench.pl 
Benchmark: timing 500 iterations of Hash, Refs...
Hash: 55 wallclock secs (48.15 usr + 1.80 sys = 49.95 CPU) @ 10.01/s (n=500)
Refs: 1 wallclock secs ( 0.08 usr + 0.00 sys = 0.08 CPU) @ 6250.00/s (n=500)


As you can see, there's a slight difference in speed for both versions. This is because in "your" version, perl is copying the whole contents of each hash in every iteration of the for loop.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Jean
User


Feb 24, 2002, 6:52 AM

Post #6 of 7 (1188 views)
Re: [mhx] Dereferencing an array element inside the foreach loop [In reply to] Can't Post

Actually in this case I was aware of the consequences, but the script I'm talking about is going to work with an average array size of 3 elements each including one email header...

So speed is of no consequence (at least right now) Wink. Moreover, most chances that the one who's going to maintain the code is not very familiar with Perl and he prefers readability to anything else Unsure.

Thanks for the explanation anyway - chances are that it'll help somebody in the future...


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


Jean
User


Feb 24, 2002, 6:58 AM

Post #7 of 7 (1183 views)
Re: [mhx] Dereferencing an array element inside the foreach loop [In reply to] Can't Post

Oh, and I remembered why I wanted to dereference in the first place...

I'm making changes to the hash elements and I'd like to preserve the original values (currently I don't have use for this, but it's possible I'll need them soon enough - a least that was my original thought)


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives