in reply to Parsing Data Help!

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;
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Parsing Data Help!
by Anonymous Monk on Mar 15, 2013 at 15:13 UTC
    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
        You are right, its not flat,I need a module to do this or flat the data somehow.