Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Problem printing JSON type output

by pme (Prior)
on Jul 14, 2015 at 11:44 UTC ( #1134693=note: print w/replies, xml ) Need Help??


in reply to Problem printing JSON type output

Hi jonathan.vanderwatt

Welcome to the monastery!

This little piece of code might help.

while (my $row = <$fh>) { chomp($row); next if $row =~ /^cd|^#|^$/; # skip lines $row =~ /xruntime\/([a-z0-9]+)_/; # regexp pattern matc +hing with memory () print "\t{ \"{#BOBINSTANCE}\":\"$1\"}\n"; # print memory regist +er $1 }

Replies are listed 'Best First'.
Re^2: Problem printing JSON type output
by jonathan.vanderwatt (Initiate) on Jul 14, 2015 at 12:00 UTC

    Close. Your code gives me the following:

    { "data": [ { "{#BOBINSTANCE}":"bob311"} { "{#BOBINSTANCE}":"bob411"} { "{#BOBINSTANCE}":"bob312"} { "{#BOBINSTANCE}":"bob412"} { "{#BOBINSTANCE}":"bob313"} { "{#BOBINSTANCE}":"bob413"} { "{#BOBINSTANCE}":"bob314"} { "{#BOBINSTANCE}":"bob414"} { "{#BOBINSTANCE}":"bob315"} { "{#BOBINSTANCE}":"bob415"} { "{#BOBINSTANCE}":"bob331"} { "{#BOBINSTANCE}":"bob332"} { "{#BOBINSTANCE}":"bob431"} { "{#BOBINSTANCE}":"bob432"} { "{#BOBINSTANCE}":"bob333"} { "{#BOBINSTANCE}":"bob334"} { "{#BOBINSTANCE}":"bob433"} { "{#BOBINSTANCE}":"bob434"} { "{#BOBINSTANCE}":"bob341"} { "{#BOBINSTANCE}":"bob342"} { "{#BOBINSTANCE}":"bob343"} { "{#BOBINSTANCE}":"bob351"} { "{#BOBINSTANCE}":"bob352"} { "{#BOBINSTANCE}":"bob353"} { "{#BOBINSTANCE}":"bob361"} { "{#BOBINSTANCE}":"bob362"} { "{#BOBINSTANCE}":"bob363"} { "{#BOBINSTANCE}":"bob371"} { "{#BOBINSTANCE}":"bob372"} { "{#BOBINSTANCE}":"bob373"} { "{#BOBINSTANCE}":"bob441"} { "{#BOBINSTANCE}":"bob442"} { "{#BOBINSTANCE}":"bob443"} { "{#BOBINSTANCE}":"bob451"} { "{#BOBINSTANCE}":"bob452"} { "{#BOBINSTANCE}":"bob453"} { "{#BOBINSTANCE}":"bob461"} { "{#BOBINSTANCE}":"bob462"} { "{#BOBINSTANCE}":"bob463"} { "{#BOBINSTANCE}":"bob471"} { "{#BOBINSTANCE}":"bob472"} { "{#BOBINSTANCE}":"bob473"} ] }
    It needs to be:
    { "data": [ { "{#BOBINSTANCE}":"bob311"}, { "{#BOBINSTANCE}":"bob411"}, { "{#BOBINSTANCE}":"bob312"}, { "{#BOBINSTANCE}":"bob412"}, { "{#BOBINSTANCE}":"bob313"}, { "{#BOBINSTANCE}":"bob413"}, { "{#BOBINSTANCE}":"bob314"}, { "{#BOBINSTANCE}":"bob414"}, { "{#BOBINSTANCE}":"bob315"}, { "{#BOBINSTANCE}":"bob415"}, { "{#BOBINSTANCE}":"bob331"}, { "{#BOBINSTANCE}":"bob332"}, { "{#BOBINSTANCE}":"bob431"}, { "{#BOBINSTANCE}":"bob432"}, { "{#BOBINSTANCE}":"bob333"}, { "{#BOBINSTANCE}":"bob334"}, { "{#BOBINSTANCE}":"bob433"}, { "{#BOBINSTANCE}":"bob434"}, { "{#BOBINSTANCE}":"bob341"}, { "{#BOBINSTANCE}":"bob342"}, { "{#BOBINSTANCE}":"bob343"}, { "{#BOBINSTANCE}":"bob351"}, { "{#BOBINSTANCE}":"bob352"}, { "{#BOBINSTANCE}":"bob353"}, { "{#BOBINSTANCE}":"bob361"}, { "{#BOBINSTANCE}":"bob362"}, { "{#BOBINSTANCE}":"bob363"}, { "{#BOBINSTANCE}":"bob371"}, { "{#BOBINSTANCE}":"bob372"}, { "{#BOBINSTANCE}":"bob373"}, { "{#BOBINSTANCE}":"bob441"}, { "{#BOBINSTANCE}":"bob442"}, { "{#BOBINSTANCE}":"bob443"}, { "{#BOBINSTANCE}":"bob451"}, { "{#BOBINSTANCE}":"bob452"}, { "{#BOBINSTANCE}":"bob453"}, { "{#BOBINSTANCE}":"bob461"}, { "{#BOBINSTANCE}":"bob462"}, { "{#BOBINSTANCE}":"bob463"}, { "{#BOBINSTANCE}":"bob471"}, { "{#BOBINSTANCE}":"bob472"}, { "{#BOBINSTANCE}":"bob473"} ] }

      This seems to have solved my problem:

      #!/usr/bin/perl $first=1; print "{"; print " \"data\":\n\t[\n"; my $filename = '/opt/scripts/bob_monitor.sh'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; while (my $row = <$fh>) { chomp($row); for($row) { next if $row =~ /^cd|^#|^$|^,/; # skip lines ($bobinstance) = m/(bob(\d+))/; print ",\n" if not $first; $first = 0; $return = "\t{ \"{#BOBINSTANCE}\":\"$bobinstance\"}" u +nless /(^\s*$|^\,$)/; print $return; } } print "\t]\n"; print "}\n";
      Thanks for the help.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2020-01-24 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?