Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: json data: dereferencing arrays

by hdb (Prior)
on May 14, 2013 at 11:01 UTC ( #1033447=note: print w/replies, xml ) Need Help??

in reply to json data: dereferencing arrays

You got the levels wrong. Change $s->{BLOCKS} to $p->{BLOCKS} and $b->{PDB_ID} to $p->{PDB_ID} and $blocks[$p][$b]{PSTART} to $blocks[$b]{PSTART}.

UPDATE: I got them wrong as well. Here is code that works:

my $position = 1200; for my $s (@$data) { next unless $s->{PROT_NAME} eq 'ALK'; foreach my $p (0 .. $#{$s->{PDBS}}) { foreach my $b (0 .. $#{$s->{PDBS}[$p]->{BLOCKS}}) { my @blocks = @{ $s->{PDBS}[$p]->{BLOCKS}}; if ($position >= $blocks[$b]{PSTART} && $position <= $ +blocks[$b]{PEND} ){ print ($s->{PDBS}[$p]->{PDB_ID},"\n");} } } }

Replies are listed 'Best First'.
Re^2: json data: dereferencing arrays
by hulketa (Initiate) on May 14, 2013 at 12:46 UTC
    uuoouu thank you so much!!!! I can see my errors now! you save my life!!! THANKS THANKS!!

      No reason to get excited. Here is how I would usually do it by iterating directly over the structure.

      my $position = 1200; for my $s (@$data) { next unless $s->{PROT_NAME} eq 'ALK'; foreach my $p ( @{ $s->{PDBS} } ) { foreach my $b ( @{ $p->{BLOCKS} } ) { if ($position >= $b->{PSTART} && $position <= $b->{PEND} ) +{ print ($p->{PDB_ID},"\n"); } } } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1033447]
[stevieb]: for the first time ever, I will have 100% of the documentation and unit tests written for a new distribution before writing even a single character of the C or Perl code required :)
[hippo]: Congrats. I expect the resulting code to be rock solid. :)

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2017-01-20 16:46 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (176 votes). Check out past polls.