Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Data structure / hash reference question

by stevieb (Canon)
on Apr 21, 2012 at 14:36 UTC ( [id://966366]=note: print w/replies, xml ) Need Help??


in reply to Re: Data structure / hash reference question
in thread Data structure / hash reference question

You were close :)

my $class = 'regex1'; my $color = ( first { $class =~ /$_->{'re'}/ } @rules )->{ color }; + print "$color\n";

Replies are listed 'Best First'.
Re^3: Data structure / hash reference question
by roboticus (Chancellor) on Apr 21, 2012 at 18:07 UTC

    stevieb:

    You'd think that as long as I've been toying around with perl, I'd've figured it out or stumbled across it by now.

    I tried several "obvious" things, none of which worked:

    $color = ${ ... }->{color}; $color = $${ ... }->{color}; $color = ${%{ ... }}{color};

    The parenthesis are a surprise to me, but I'm glad it's possible to avoid the two-step!

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I love learning new things :)

      If I recall correctly, I found the basis for this trick years ago after figuring there must be a simpler way to do this:

      # want fourth word my $str = "one two three four five"; my @array = split / /, $str; my $word = $array[3];

      I monkeyed about and accidentally came up with this:

      my $str = "one two three four five"; my $word = ( split / /, $str )[3];

      Combined with my current knowledge of refs and anonymous data and where they are handy, I revisited the parens and came to the conclusion that the value after the expression is evaluated within the parens can be operated on directly.

      You were close with the first one - you just didn't need the dereferencing arrow ->. This should work:

      $color = ${ ... }{color};

      Why? Because if ... is a hashref, then %{...} is a plain old hash.

      If our hash was called %foo then we'd access a value in it as $foo{color} - that is, change the sigil from % to $, and add the key in curlies. Our hash is called %{...} but we can still use the same technique.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        I was curious as to how this could work, so I got playing around.

        I forgot that you could substitute the -> deref operator for ${}

        perl -E '$h={a=>1,b=>2}; say $h->{a}'

        ...is the same as:

        perl -E '$h={a=>1,b=>2}; say ${$h}{a}'

        In roboticus' attempt that you pointed out, you'd still need to use the parens inside of the deref block, or else perl will try to use the bareword 'first' as the name of the hash instead of evaluating the expression to its final value (I only found this after testing it):

        my $color = ${( first { $class =~ /$_->{'re'}/ } @rules )}{ color };
Re^3: Data structure / hash reference question
by hperange (Beadle) on Apr 21, 2012 at 15:27 UTC
    Thanks, this works :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-24 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found