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

Parsing Data Help!

by Anonymous Monk
on Mar 15, 2013 at 13:48 UTC ( #1023696=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 need to parse this data structure and my doubts are on the inner []s inside the main block of []. MY goal is to get the values like the value of the “cd” keys. Can anyone point me on the right direction on how I would parse this structure if possible? Thanks for hte help!
[ {"account":"123456","name":"Comps"}, {"account":"123456","name":"MIT"}, {"account":"123456","name":"Engine"}, {"account":"123456","name":"Cars"}, {"account":"123456","name":"The Company Inc."}, {"account":"123456","name":"XTCo. Inc."}, {"ZIP":"02111","CITY":"New York, NY","STREET_NAME_TWO":"","STREET_NA +ME_ONE":"1 SAMWE SQUARE"}, [ {"order":"542","suggested":"250"} ], [ {"cd":"Joe Doe"}, {"cd":"Mary Lou"}, {"cd":"Jim D. Mark"}, {"cd":"Charles Dmond"}, {"cd":"Justin Dwo"} ] ]

Replies are listed 'Best First'.
Re: Parsing Data Help!
by choroba (Chancellor) on Mar 15, 2013 at 13:52 UTC
    The structure resembles JSON. The corresponding CPAN module can handle it easily:
    #!/usr/bin/perl use warnings; use strict; use JSON; use Data::Dumper; my $j = << '__STRUCT__'; [ {"account":"123456","name":"Comps"}, {"account":"123456","name":"MIT"}, {"account":"123456","name":"Engine"}, {"account":"123456","name":"Cars"}, {"account":"123456","name":"The Company Inc."}, {"account":"123456","name":"XTCo. Inc."}, {"ZIP":"02111","CITY":"New York, NY","STREET_NAME_TWO":"","STREET_NA +ME_ONE":"1 SAMWE SQUARE"}, [ {"order":"542","suggested":"250"} ], [ {"cd":"Joe Doe"}, {"cd":"Mary Lou"}, {"cd":"Jim D. Mark"}, {"cd":"Charles Dmond"}, {"cd":"Justin Dwo"} ] ] __STRUCT__ my $p = from_json($j); print Dumper $p;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      The issue is how to interact with this structure, trying but getting an error with:
      for my $items( @{$p} ){ print $item->{account} . "\n"; print $item->{ZIP} . "\n"; print $item->{CITY} . "\n"; print $item->{order} . "\n"; print $item->{suggested} . "\n"; print $item->{cd} . "\n"; };
        The structure is not flat. You have to follow it deeper if needed:
        for my $item (@{$p}) { if ('HASH' eq ref $item) { for my $key (qw(account ZIP CITY)) { print $item->{$key} , "\n" if exists $item->{$key}; } } elsif ('ARRAY' eq ref $item) { for my $inner (@$item) { for my $key (qw(order suggested cd)) { print "\t", $inner->{$key} , "\n" if exists $inner->{$ +key}; } } } }
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Here is one way:
        sub findcd { my $someref = shift; my $type = ref($someref); if( $type eq "" ) { # do nothing for non-references } elsif( $type eq "ARRAY" ) { foreach my $elem (@{$someref}) { findcd( $elem ); } } elsif ($type eq "HASH" ) { foreach my $elem (keys %{$someref}) { print "cd: $$someref{$elem}\n" if $elem eq "cd"; findcd( $$someref{$elem} ); } } else { warn "Cannot deal with $type.\n"; } } findcd( $p );
        but I am sure there is a module doing it simpler.
        For every complex problem there is an answer that is clear, simple, and wrong. H. L. Mencken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2016-09-30 11:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:







    Results (563 votes). Check out past polls.