Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^6: JSON::Path and node iteration (updated)

by frazap (Monk)
on Mar 22, 2019 at 15:22 UTC ( [id://1231573]=note: print w/replies, xml ) Need Help??


in reply to Re^5: JSON::Path and node iteration (updated)
in thread JSON::Path and node iteration

Nice !

my $cr = REST::Client::CrossRef->new( mailto => 'd...h', spit_raw_data => 0, add_end_flag => 1, json_path => [ ['$..author[*].[family,given,affiliation]'], ], json_path_callback => { '$..author[*].[family,given,affiliation]' => \&unfoldauthors +4 }, ); sub unfoldauthors4 { my ($authors) = @_; my @all = @{$authors}; print Dumper @all; my $authors_limit = 50; my $last = $authors_limit - 1; my $size = @all / 3; if ( $size > $authors_limit ) { @all = @all[ 0 .. $last * 3 ]; $all[ $last * 3 - 1 ] .= " ..."; } my @res; my $i; for ( $i = 0; $i <= $#all; $i += 3 ) { my $line = $all[$i] . ", " . $all[ $i + 1 ] . "; " . join( " ", map { $_->{name} } @{ $all[ $i + 2 ] } ); push @res, $line; } return \@res; }

works

Thanks

Update the above loop is not elegant and perlish; This is easier to read (without the truncation to 50 authors:
sub unfoldauthors4 { my ($authors) = @_; my @all = @{$authors}; my @res; while (@all) { push @res, shift( @all) . ", ". shift( @all) . "; " . join(" ", +map {$_->{name}} @{ shift( @all )}); } return \@res; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-18 00:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found