Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: elegant way of handling hash?

by dorward (Curate)
on Jan 19, 2006 at 09:38 UTC ( [id://524167]=note: print w/replies, xml ) Need Help??


in reply to elegant way of handling hash?

It mostly depends on how you get the data from the API. Its quite likely that a foreach loop (or a while loop) will get the job done, but not easy to say without knowing exactly how you are getting the data in the first place!

Replies are listed 'Best First'.
Re^2: elegant way of handling hash?
by Anonymous Monk on Jan 19, 2006 at 10:01 UTC
    hi doward, i will try to be more clear .
    my %replacehash; $replacehash{TEMP1} = $flow->getExeName(); $replacehash{TEMP2} = $flow->getTempName(); $replacehash{TEMP3} = $flow->getData()->getNameType(); ....
    All the API's are different .

      You can do the first two quite easily

      @replacehash{ map{ "TEMP$_" } 1 .. 2 } = map{ $flow->$_ } qw[getExeName getTempName];

      Doing the third one, with the double level of indirection, would probably require a string eval.

      Whether you should do this is another matter entirely. It would have to be a pretty long list to warrent it, and even then you would probably be better having the $flow class provide an API that returned the hash. That way the construction need only be done once and the user of the $flow object can just do

      my %replacehash = $flow->getAll();

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        It wouldn't need a string eval, but it does start to become convoluted. Something like this should work:
        @replacehash{ map{ "TEMP$_" } 1 .. 2 } = map{ use List::Util 'reduce'; reduce {$a->$b} ($flow, split /->/, $_); } qw[getExeName getTempName getData->getNameType];

        Caution: Contents may have been coded under pressure.

      You could try

      my %replacehash = ( TEMP1 => $flow->getExeName(), TEMP2 => $flow->getTempName(), TEMP3 => $flow->getData()->getNameType(), )
      (If you call a method with no arguments, like here, you could also omit the empty pair of parentheses, but that's not the point here.)

      Also

      my %replacehash; @replacehash{map { "TEMP$_" } 1 .. 3} = ( $flow->getExeName(), $flow->getTempName(), $flow->getData()->getNameType(), )

Log In?
Username:
Password:

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

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

    No recent polls found