Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Looping through an array

by Anonymous Monk
on Sep 27, 2021 at 19:49 UTC ( [id://11137065]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks!

I get this data where I need to get the values, when I loop through it, the values are coming duplicated, cant see where the issue is anymore:
My test data:
my $DATA = { "required": [ { "docs": [ { "sec": "123A", "number": "1" } ], "name": "Mary Lou" }, { "docs": [ { "sec": "4567", "number": "1" } ], "name": "John De" }, { "docs": [ { "sec": "8763", "number": "1" } ], "name": "Smith Doe" }, { "docs": [ { "sec": "123B", "number": "1" } ], "name": "Joe De" } ], }; ... for my $dta ( @{ $DATA->{ required } || [] } ){ # if the docs is true if( scalar @{ $dta->{ docs } } ){ for my $doc_data ( @{ $dta->{ docs } || [] } ){ if( $doc_data->{'sec'} ) { # Get sec - number and name } } }else { warn "NO: $doc_data->{'sec'} | $doc_data->{'number'} \n"; # Get sec - numnber and name } } ...

I am trying to get this:
"sec": "123A", "number": "1" "name": "Mary Lou" "sec": "4567", "number": "1" "name": "John De" "sec": "8763", "number": "1" "name": "Smith Doe" "sec": "123B", "number": "1" "name": "Joe De"


Im getting data, but its duplicating:
"sec": "123A", "number": "1" "name": "Mary Lou" "sec": "4567", "number": "1" "name": "John De" "sec": "8763", "number": "1" "name": "Smith Doe" "sec": "123B", "number": "1" "name": "Joe De" "sec": "123A", "number": "1" "name": "Mary Lou" "sec": "4567", "number": "1" "name": "John De" "sec": "8763", "number": "1" "name": "Smith Doe" "sec": "123B", "number": "1" "name": "Joe De"

Thanks for looking!

Replies are listed 'Best First'.
Re: Looping through an array
by choroba (Cardinal) on Sep 27, 2021 at 20:21 UTC
    The contents of $DATA in not Perl, it looks like a JSON. There are modules to turn JSON into a Perl structure (e.g. Cpanel::JSON::XS).

    Your code doesn't compile under strict. That's because $doc_data is declared only in the for loop, but you use it outside of it in the else part. Without strict, it runs, but doesn't output anything.

    Please, try to post the actual code. Help us to help you.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Looping through an array
by tybalt89 (Monsignor) on Sep 27, 2021 at 21:05 UTC

    After converting your JSON to perl...

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11137065 use warnings; my $DATA = { required => [ { docs => [{ number => 1, sec => "123A" }], name => "Mary Lou" }, { docs => [{ number => 1, sec => 4567 }], name => "John De" }, { docs => [{ number => 1, sec => 8763 }], name => "Smith Doe" }, { docs => [{ number => 1, sec => "123B" }], name => "Joe De" }, ], }; for my $dta ( @{ $DATA->{required} } ) { my $sec = $dta->{docs}[0]{sec}; my $number = $dta->{docs}[0]{number}; my $name = $dta->{name}; print <<END; "sec": "$sec", "number": "$number" "name": "$name" END }

    Outputs

    "sec": "123A", "number": "1" "name": "Mary Lou" "sec": "4567", "number": "1" "name": "John De" "sec": "8763", "number": "1" "name": "Smith Doe" "sec": "123B", "number": "1" "name": "Joe De"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found