Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Parsing Data Help!

by Anonymous Monk
on Mar 15, 2013 at 15:13 UTC ( #1023717=note: print w/ replies, xml ) Need Help??


in reply to Re: Parsing Data Help!
in thread Parsing Data Help!

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"; };


Comment on Re^2: Parsing Data Help!
Download Code
Re^3: Parsing Data Help!
by hdb (Parson) on Mar 15, 2013 at 15:20 UTC
    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.
Re^3: Parsing Data Help!
by Anonymous Monk on Mar 15, 2013 at 15:21 UTC
Re^3: Parsing Data Help!
by choroba (Abbot) on Mar 15, 2013 at 15:23 UTC
    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}; } } } }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-08-23 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (171 votes), past polls