Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

How can I acces the "member" stepxxx of the structure given below?

by adrya407 (Novice)
on Feb 05, 2016 at 12:35 UTC ( [id://1154480]=perlquestion: print w/replies, xml ) Need Help??

adrya407 has asked for the wisdom of the Perl Monks concerning the following question:

$VAR1 = { 'opt' => { 'step820' => '0', 'step190' => '0', 'step124' => '0', 'step127' => '0', 'step410' => '0', 'step015' => '2', 'step130' => '1', 'step013' => '0', 'step715' => '0', 'step540' => '0', 'step800' => '5', 'step135' => '0', 'step003' => '0', 'step455' => '0', + } };
How can I acces the "stepxxx"? I need to acces it's name and verify if it equals a variable $x like this:
if $stepxxx eq $x{ #do something; }

Replies are listed 'Best First'.
Re: How can I acces the "member" stepxxx of the structure given below?
by Discipulus (Canon) on Feb 05, 2016 at 12:41 UTC
    hello adrya407 and welcome to the monastery!

    you can find usefull a read of perldsc - hashes of hashes

    You need to learn the syntax to access the value of an hash key that is itself the value of another key of the (possibly anonymous) outer hash.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: How can I acces the "member" stepxxx of the structure given below?
by dorko (Prior) on Feb 05, 2016 at 14:23 UTC
    Without any other context, this will give you something to work with:

    use strict; use warnings; use 5.016; use Data::Dumper; my @array_of_hashes = { 'opt' => { 'step820' => '0', 'step190' => '0', 'step124' => '0', 'step127' => '0', 'step410' => '0', 'step015' => '2', 'step130' => '1', 'step013' => '0', 'step715' => '0', 'step540' => '0', 'step800' => '5', 'step135' => '0', 'step003' => '0', 'step455' => '0', } }; say Dumper @array_of_hashes; foreach my $step_number ('step001' .. 'step820') { next if (not defined $array_of_hashes[0]{opt}{$step_number}); say $step_number . " = " . $array_of_hashes[0]{opt}{$step_number}; + }

    Interesting output:

    step003 = 0 step013 = 0 step015 = 2 step124 = 0 step127 = 0 step130 = 1 step135 = 0 step190 = 0 step410 = 0 step455 = 0 step540 = 0 step715 = 0 step800 = 5 step820 = 0

    Cheers,

    Brent

    -- Yeah, I'm a Delt.
Re: How can I acces the "member" stepxxx of the structure given below?
by GotToBTru (Prior) on Feb 05, 2016 at 12:54 UTC

    Another useful read on the subject of references.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: How can I acces the "member" stepxxx of the structure given below?
by swampyankee (Parson) on Feb 05, 2016 at 18:55 UTC

    I think it would be helpful if you gave us a little context -- checking the value of it's contents seems like just one piece of a multi-step process.

    I also tend to get suspicious when I see lots of things with names like "stepxxx" where "xxx" seems to be counting. When one has something like that, I, for one, wonder why isn't an array being used: Why doesn't your structure look like

    $hash{opt}{step} = 001, 002, .....999;

    This way, you could easily pull out element xxx and test its value.


    Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

Re: How can I acces the "member" stepxxx of the structure given below?
by Laurent_R (Canon) on Feb 05, 2016 at 17:54 UTC
    It seems you have a hash of hashes. Assuming the name of your top level hash is %hash (it hopefully has a better name), you could do something like this:
    for my $key (keys %{$hash{opt}}) { do_something($key) if $hash{opt}{$key} == $x; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-19 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found