help with references

N

Nene

Hi

I have this variable in my script that produced this data:

{
'reason_for_account' => 'tempaccounts',
'username' => 'narf',
'useruuid' => 'blahblah',
'created' => '2012-11-18 15:57:35',
'password' => '1232132',
'tempaccount' => '947A60EA-31C2-11E2-8C75-A6A7C6D72CC8',
'context' => '{ FTPStuff }',
'id' => '12',
'type' => 'Cabbage'
}

I was able to produce the data above by this:

my @data = \$ret;
print Dumper(@data);

But I need to grab the value to the key 'tempaccount' and put it in a variable.

How do I do that?

usaims
 
R

Rainer Weikusat

Nene said:
I have this variable in my script that produced this data:

{
'reason_for_account' => 'tempaccounts',
'username' => 'narf',
'useruuid' => 'blahblah',
'created' => '2012-11-18 15:57:35',
'password' => '1232132',
'tempaccount' => '947A60EA-31C2-11E2-8C75-A6A7C6D72CC8',
'context' => '{ FTPStuff }',
'id' => '12',
'type' => 'Cabbage'
}

I was able to produce the data above by this:

my @data = \$ret;
print Dumper(@data);

But I need to grab the value to the key 'tempaccount' and put it in
a variable.

It is not entirely clear if your script produces this text or of this
is something Data::Dumper generated based on the input data. In any
case, this thing above is what the perlref manpage calls 'an anonymous
hash composer', cf

,----
| [rw@sapphire]~ $perl -e '$h = { a => b }; print $h, "\n";'
| HASH(0x605d48)
`----

And for the simple case[*], the elements are accessed just like hash
elements except that the reference needs to be dereferenced. This
looks like this:

,----
| [rw@sapphire]~ $perl -e '$h = { a => b }; print $h->{a}, "\n";'
| b
`----

In your case, that would be $ret->{tempaccount}.

[*] Considering the gory details, as described in the 'Using
References' section of the perlref manpage, this description is quite
of an understatement and (as seems very likely) if you'll never need
to write code for a perl old enough that it doesn't know about ->,
consider yourself "blissfully ignorant" of stuff no one really wanted
to learn :->. [**]

[**] Sufficiently new perls (since 5.14, IIRC), have
thankfully started to eliminate @{${$_[0]}} and friends from the more
common use cases but that's just an outlook into a possibly nicer
future at the moment.
 
J

Jürgen Exner

Nene said:
I have this variable in my script that produced this data:

Variables contain data, they don't produce it.

It would have been of great help if you would have included the
definition of this variable, such that we can actually run it ourselves.
I am assuming it is probably something like

my $ret = {
'reason_for_account' => 'tempaccounts',
'username' => 'narf',
'useruuid' => 'blahblah',
'created' => '2012-11-18 15:57:35',
'password' => '1232132',
'tempaccount' => '947A60EA-31C2-11E2-8C75-A6A7C6D72CC8',
'context' => '{ FTPStuff }',
'id' => '12',
'type' => 'Cabbage'
}

which would make $ret a reference to an anonymous hash.
I was able to produce the data above by this:

my @data = \$ret;
print Dumper(@data);

But I need to grab the value to the key 'tempaccount' and put it in a variable.

Just dereference (see "perldoc perlreftut" and "perldoc perlref") the
reference and select the element from the hash:

$thisaccount = $ret->{tempaccount};

jue
 
J

Jürgen Exner

Henry Law said:
As a devotee of Data::Dumper I can say categorically that, whatever
you've dumped out above, it's not an array called @data, or
@anythingelse either; it's a hash.

....which in the old days used to be called an associative array.
Could we see some real code, preferably something that runs, so we can
help you better?

That of course is always a very good idea.

jue
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top