Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

retrieve list values...

by danidin (Novice)
on Jul 18, 2005 at 13:25 UTC ( #475749=perlquestion: print w/replies, xml ) Need Help??

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

Hi All,
I'm new here and with perl so your help is more than needed.. I have a varibale which when I print it I get :

$VAR1 = [ { 'ID' => '111087327557000.111087327557001', 'TEXT' => 'AIAA_AMERICAN_INSTITUTE_OF_AERONAUTICS_A-getFul +lTxt', 'VALUE' => '111087327557001' }, { 'ID' => '110974885571586.110974885571587', 'TEXT' => 'AIP_SCITATION-getFullTxt', 'VALUE' => '110974885571587' },....

and so on.

My question is how I seperate the trioes (ID, TEXT, VALUE)? I want all the trioes to set into array and every trio to be in a different cell in the array.
How can I do that ?!?!


20050718 Janitored by Corion: Fixed formatting

Replies are listed 'Best First'.
Re: retrieve list values...
by jdporter (Chancellor) on Jul 18, 2005 at 13:50 UTC
    So you've got the array in $VAR1 (an array reference)... you want to iterate over all the records. Note that each record exists in the array as a hash reference. So:
    for my $rec_hr ( @$VAR1 ) { ... }
    Next is to turn each record hashref into an array of items in the order (ID, TEXT, VALUE). You can use a hash slice to do this.
    for my $rec_hr ( @$VAR1 ) { my @rec = @{$rec_hr}{'ID','TEXT','VALUE'}; }
    From there, you can do whatever you want. You could, for example, push these array-type records onto a new array, or print them:
    for my $rec_hr ( @$VAR1 ) { my @rec = @{$rec_hr}{'ID','TEXT','VALUE'}; push @array_records, \@rec; print "(@rec)\n"; }
      the variable that I'm printing is defined like this : my @tss = $tool->{param}->{'TARGET_SERVICES_SINGLE'}; when I print it I get array reference. the code that you suggested didn't work :-( what am I doing wrong !?

        If you did anything wrong, it was to neglect to say that you've got a plain array that you're printing out the variable using Data::Dumper. :-)

        To account for this, simply take the code I posted before and replace @$VAR1 with @tss: :

        for my $rec_hr ( @tss ) { my @rec = @{$rec_hr}{'ID','TEXT','VALUE'}; push @array_records, \@rec; print "(@rec)\n"; }
        I might also suggest that you read up on references, e.g. in perlreftut.
Re: retrieve list values...
by socketdave (Curate) on Jul 18, 2005 at 13:32 UTC
    I'm not clear on what you're looking for here... You have an anonymous array of hashes already. Do you want the ID, TEXT and VALUE data to be flattened into a string and have them stored in an array? Do you want to give the anonymous array a name?
      I can't go over all the trioes. I have tried any possible way. how can I go over these trioes ? what should be the code for having them trio by trio ? Thanks.
        use strict; use warnings; my $arrayref = [ { 'ID' => '111087327557000.111087327557001', 'TEXT' => 'AIAA_AMERICAN_INSTITUTE_OF_AERONAUTICS_A-getFullTxt +', 'VALUE' => '111087327557001' }, { 'ID' => '110974885571586.110974885571587', 'TEXT' => 'AIP_SCITATION-getFullTxt', 'VALUE' => '110974885571587' }, ]; my $i; for my $trio ( @{$arrayref} ) { $i++; print "Trio Number $i contains:\n", " id: ", $trio->{ID}, "\n", " text: ", $trio->{TEXT}, "\n", " value: ", $trio->{VALUE}, "\n\n"; }

        holli, /regexed monk/

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-11-29 01:42 GMT
Find Nodes?
    Voting Booth?