Hashes, flattening, evaluation

Discussion in 'Perl Misc' started by Gregory Toomey, Mar 16, 2005.

  1. 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
     
    Gregory Toomey, Mar 16, 2005
    #1
    1. Advertising

  2. Gregory Toomey wrote:
    > 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.


    <snip>

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


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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 16, 2005
    #2
    1. Advertising

  3. Gregory Toomey <> wrote:
    > 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;
    }


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas part time wanker
     
    Tad McClellan, Mar 16, 2005
    #3
  4. Tad McClellan wrote:

    > Gregory Toomey <> wrote:
    >> 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;
    > }


    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
     
    Gregory Toomey, Mar 16, 2005
    #4
  5. A. Sinan Unur, Mar 16, 2005
    #5
  6. A. Sinan Unur wrote:

    > Gregory Toomey <> wrote in
    > news::
    >
    >> Theres a section in the cookbook on using closures as objects
    >> http://www.india-seo.com/perl/cookbook/ch11_08.htm

    >
    > 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?"
     
    Chris Mattern, Mar 16, 2005
    #6
  7. A. Sinan Unur wrote:

    > Gregory Toomey <> wrote in
    > news::
    >
    >> Theres a section in the cookbook on using closures as objects
    >> http://www.india-seo.com/perl/cookbook/ch11_08.htm

    >
    > 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
     
    Gregory Toomey, Mar 17, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. delgados129
    Replies:
    2
    Views:
    799
    delgados129
    Apr 25, 2005
  2. David Gersic

    Flattening out an XML document

    David Gersic, May 24, 2005, in forum: XML
    Replies:
    0
    Views:
    401
    David Gersic
    May 24, 2005
  3. gangesmaster

    a flattening operator?

    gangesmaster, Apr 18, 2006, in forum: Python
    Replies:
    2
    Views:
    297
    Michael Tobis
    Apr 22, 2006
  4. Replies:
    3
    Views:
    452
    Eddie Corns
    Mar 22, 2007
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    216
Loading...

Share This Page