Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Read datastructure

by Anonymous Monk
on Feb 05, 2021 at 23:33 UTC ( [id://11127956]=perlquestion: print w/replies, xml ) Need Help??

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

Hello

What is this data structure I get with Dumper?

$VAR1 = [ { 'sourceL' => 'spacecraft', 'source' => 'MT', 'targetL' => 'vaisseau spatial' } ];

Why can I not read it with the following?

foreach (@data){ print my $target= ucfirst($_->{targetL}); }

Replies are listed 'Best First'.
Re: Read datastructure
by hippo (Bishop) on Feb 05, 2021 at 23:46 UTC

    It's a reference to an array of hash refs, so you must de-reference it first.

    #!/usr/bin/perl use strict; use warnings; my $VAR1 = [ { 'sourceL' => 'spacecraft', 'source' => 'MT', 'targetL' => 'vaisseau spatial' } ]; for (@$VAR1) { print ucfirst $_->{targetL} ; }

    See perldsc for lots more.


    🦛

      thank you
Re: Read datastructure
by AnomalousMonk (Archbishop) on Feb 05, 2021 at 23:52 UTC

    Alternatively:

    Win8 Strawberry 5.8.9.5 (32) Fri 02/05/2021 18:49:03 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings -MData::Dumper my @data = ( { 'sourceL' => 'spacecraft', 'source' => 'MT', 'targetL' => 'vaisseau spatial' } ); print Dumper \@data; foreach (@data){ print my $target= ucfirst($_->{targetL}); } ^Z $VAR1 = [ { 'source' => 'MT', 'sourceL' => 'spacecraft', 'targetL' => 'vaisseau spatial' } ]; Vaisseau spatial
    How and from what are you producing the Dumper output (which is what the output looks like)?


    Give a man a fish:  <%-{-{-{-<

Re: Read datastructure
by Fletch (Bishop) on Feb 06, 2021 at 03:23 UTC

    For more information see the data structures cookbook: perldsc

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found