Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > help with references

Reply
Thread Tools

help with references

 
 
Nene
Guest
Posts: n/a
 
      11-21-2012
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


 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      11-21-2012
Nene <(E-Mail Removed)> writes:
> 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:umper 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(0x605d4
`----

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.

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      11-21-2012
Nene <(E-Mail Removed)> wrote:
>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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      11-21-2012
Henry Law <(E-Mail Removed)> wrote:
>On 21/11/12 12:48, Nene wrote:
>> 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);

>
>As a devotee of Data:umper 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Snake references just as ok as Monty Python jokes/references in python community? :) seberino@spawar.navy.mil Python 8 12-12-2006 11:21 PM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
how to understand references to variables and references to constants are distinguished? baumann.Pan@gmail.com C++ 3 11-10-2004 04:16 AM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments