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 (Archbishop) 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.