Hashes, flattening, evaluation

G

Gregory Toomey

I've simplified a problem shown in the fragment below. I'm defining a hash
in terms of a function of one of its components.

I would have hoped to get
param 1 is carrie

as part of the output but dont.
This has left me confused about Perl evaluation. I dont think Perl handles
lazy evaluation, and gives me a result I did not expect.

Any comments on what I should be doing to get the semantics I want?

gtoomey

--------------
Fragment:

sub printhash {
my %h= (@_);
for (keys(%h)) {print "$_ = $h{$_}\n"}

}

sub myfunc {
print "param 1 is $_[0]\n";
return ('a'=>'b','c'=>'d')
}

my %hash;
%hash= (
"cat"=>'tony',
"dog"=>'carrie',
myfunc($hash{dog})
);

printhash(%hash);

----------------
Output:
param 1 is
cat = tony
c = d
a = b
dog = carrie
 
G

Gunnar Hjalmarsson

Gregory said:
I've simplified a problem shown in the fragment below. I'm defining a hash
in terms of a function of one of its components.

my %hash;
%hash= (
"cat"=>'tony',
"dog"=>'carrie',
myfunc($hash{dog})
);

my %hash = ( cat => 'tony', dog => 'carrie' );
%hash = ( %hash, myfunc($hash{dog}) );
 
T

Tad McClellan

Gregory Toomey said:
I've simplified a problem shown in the fragment below. I'm defining a hash
in terms of a function of one of its components.

I would have hoped to get
param 1 is carrie

as part of the output but dont.
This has left me confused about Perl evaluation. I dont think Perl handles
lazy evaluation, and gives me a result I did not expect.


The RHS of a list assignment is evaluated completely before the
assigning happens.

This allows swapping without a temp var as in: ($x,$y) = ($y,$x);

Any comments on what I should be doing to get the semantics I want?

my %hash;
%hash= (
"cat"=>'tony',
"dog"=>'carrie',
myfunc($hash{dog})
);


Does this do it for you?

my %hash = (
"cat"=>'tony',
"dog"=>'carrie',
);

{ my %h = myfunc($hash{dog}); # commence homliness
$hash{$_} = $h{$_} for keys %h;
}
 
G

Gregory Toomey

Tad said:
The RHS of a list assignment is evaluated completely before the
assigning happens.

This allows swapping without a temp var as in: ($x,$y) = ($y,$x);




Does this do it for you?

my %hash = (
"cat"=>'tony',
"dog"=>'carrie',
);

{ my %h = myfunc($hash{dog}); # commence homliness
$hash{$_} = $h{$_} for keys %h;
}

That does and Gunnar has a nice solution too.

The basic problem is data inheritance. I'm looking at defining a hash as a
base case, and defining other hashes as variants of this, as a hierarchy.

Doing this using OO may be overkill. Theres a section in the cookbook on
using closures as objects
http://www.india-seo.com/perl/cookbook/ch11_08.htm

I just want a solution thats simple to set up & maintain for about 50
hashes.

gtoomey
 
C

Chris Mattern

A. Sinan Unur said:
You know that is an illegal copy, right? Have you informed O'Reilley about
the site?
Not to mention out of date; it's the first edition.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
G

Gregory Toomey

A. Sinan Unur said:
You know that is an illegal copy, right? Have you informed O'Reilley about
the site?

Sinan

I just found it with google. I didnt know it was illegal.

gtoomey
 

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
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top