what the difference between these loops?

D

doolittle

My code was not working in an expected way, so i replaced the line

while ((my $key, my $value) = each %boardhash) {
....
}

with the lines

for (keys(%boardhash)){
my $value = $boardhash{$_};
....
}

and it worked differently (more better).
 
J

John Bokma

doolittle said:
My code was not working in an expected way, so i replaced the line

while ((my $key, my $value) = each %boardhash) {
...
}

with the lines

for (keys(%boardhash)){
my $value = $boardhash{$_};
...
}

and it worked differently (more better).

In what way?


perldoc -f each
perldoc -f keys
 
J

John W. Krahn

doolittle said:
My code was not working in an expected way, so i replaced the line

while ((my $key, my $value) = each %boardhash) {
...
}

with the lines

for (keys(%boardhash)){
my $value = $boardhash{$_};
...
}

and it worked differently (more better).

They of course do work differently (as documented.) As to why one is better
than the other depends on the rest of your code.


John
 
P

paul

while ( my ($key, $value) = each %boardhash)

use grep if you filter data from the list...

my @datastore = grep { /filtersomething/ } values (%boardhash);

Paul :)
 
D

doolittle

John said:
In what way?

Depending on how the key was defined, the version using 'each' didn't
seem to go through each hash pair. Maybe its because i would sometimes
break out of the each loop before it ended, then the next time into
that loop, it started from the point i left off.

Even so, i only noticed this when i changed how the key was defined.
When i had -

$key = join('',$character,$number);

it appeared to 'work' (ie go through all the pairs every time) But
when I changed that line to

$key = join(':',$character,$number);

it stopped going through every pair every time.
 
T

Tad McClellan

In what way what?

Please retain enought context for folks to be able to tell what you
are talking about.

I think it was something about in what way did your code
using "each" fail...

Depending on how the key was defined, the version using 'each' didn't
seem to go through each hash pair. Maybe its because i would sometimes
break out of the each loop before it ended, then the next time into
that loop, it started from the point i left off.


That's what "perldoc -f each" says it is supposed to do.

It also says how to avoid it.

You should read the documentation for the functions that you use.

(most especially when you are having trouble with that function!)
 
D

doolittle

Tad said:
In what way what?

Please retain enought context for folks to be able to tell what you
are talking about.

Sometimes its difficult to find a good compromise between too little,
and too much context. I have seen far more people criticized (sp?) for
including all previous posts and replies in a reply, than for not
including enough.

I think its reasonable to assume that if someone only reads the last
post in a thread, they won't get the whole story.

Yes, the perl docs are great. So is perl, because it allows people to
write useful stuff at a level of knowledge that they are comfortable
with.

I still don't understand why the 'each' loop worked when the keys were
defined one way, but not another.
 
J

Jim Gibson

I still don't understand why the 'each' loop worked when the keys were
defined one way, but not another.

If you really want to understand, then you should post a short program
that demonstrates the concept with which you are having difficulty.
There will be many here who can explain why your program behaves the
way it does.
 
D

doolittle

Jim said:
If you really want to understand, then you should post a short program
that demonstrates the concept with which you are having difficulty.
There will be many here who can explain why your program behaves the
way it does.

After trying and failing to create such a program, i think i have found
the source of the error.

Basically i think putting information into the key of a hash ( with
$key = join( ',' , $foo , $bar) ) with the idea of later extracting
that information using split (or substr), is dodgy. Especially if you
use join( '' , $foo , $bar), and later try to extract the information
with substr.

Really i need a more complex data structure which contains the hash
value, $foo and $bar.

Thanks for all the advice.
 
J

John Bokma

doolittle said:
Tad McClellan wrote:


Sometimes its difficult to find a good compromise between too little,
and too much context. I have seen far more people criticized (sp?) for
including all previous posts and replies in a reply, than for not
including enough.

There is a simple test: reread what before you post and wonder: does this
make sense when I read *only* this message after a month or 3 from now on?
I think its reasonable to assume that if someone only reads the last
post in a thread, they won't get the whole story.

But it's not reasonable to assume that they have to read the *previous*
message in order to understand the current one.
I still don't understand why the 'each' loop worked when the keys were
defined one way, but not another.

Without a smallest piece of code that shows this behavior we can't even
guess.
 
J

John Bokma

doolittle said:
After trying and failing to create such a program, i think i have found
the source of the error.

Basically i think putting information into the key of a hash ( with
$key = join( ',' , $foo , $bar) ) with the idea of later extracting
that information using split (or substr), is dodgy. Especially if you
use join( '' , $foo , $bar), and later try to extract the information
with substr.

All depends on what's in $foo and $bar. If you use substr you have to make
sure that foo always has the same length.

If you use split, you must make sure that the character you split on can
only occur exactly once in your key (since you want foo and bar).
Really i need a more complex data structure which contains the hash
value, $foo and $bar.

Depends on what you're doing. I have used composite keys in the past, and
I am sure others have done so.

Again, without a minimal example we can only guess.
 
U

Uri Guttman

JB> Depends on what you're doing. I have used composite keys in the
JB> past, and I am sure others have done so.

and that is supported in perl (even perl4!) as pseudo multilevel
hashes. look for $; in perlvar for more.

but to the OP, a multilevel hash is the way to go. merging keys can be a
useful technique as john says but it isn't that common.

as for the OP's original issue, it seems he didn't rtfm properly about
each and how it works and is reset.

uri
 
J

John Bokma

Uri Guttman said:
JB> Depends on what you're doing. I have used composite keys in the
JB> past, and I am sure others have done so.

and that is supported in perl (even perl4!) as pseudo multilevel
hashes. look for $; in perlvar for more.

but to the OP, a multilevel hash is the way to go. merging keys can be a
useful technique as john says but it isn't that common.

Recently used it: http://johnbokma.com/perl/google-search-cloud.html

$stats{ "$path:$status" }{ sum }++;
:
my ( $path, $status ) = $ps =~ /(.*):(\d+)/;

But yeah, I rarely use this.
as for the OP's original issue, it seems he didn't rtfm properly about
each and how it works and is reset.

:-D What else is new.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,051
Members
47,656
Latest member
rickwatson

Latest Threads

Top