So, the reason that the split approach fails is that it is very sensitive to changes in input structure. If we examine the corrected input stream, you'll note that it is now valid JSON, so the script
use strict; use warnings; use JSON; use Data::Dumper; $_ = '{"temp":75.50,"tmode":2,"fmode":0,"override":0,"hold":0,"t_cool" +:75.00,"tstate":0,"fstate":0,"time":{"day":4,"hour":13,"minute":49}," +t_type_post":0}'; print Dumper decode_json ($_);
outputs
$VAR1 = { 'fstate' => 0, 't_cool' => '75', 'time' => { 'hour' => 13, 'minute' => 49, 'day' => 4 }, 'fmode' => 0, 'tmode' => 2, 'temp' => '75.5', 'hold' => 0, 'override' => 0, 'tstate' => 0, 't_type_post' => 0 };

Rather than having to fight writing a parser, the module does the heavy lifting, and your script could be rewritten perhaps like:

use strict; use warnings; use JSON; open (my $fh, '<', '/home/julian/tstatcollect') or die "Open failed:$! +"; while (<$fh>) { chomp; my $obj = decode_json($_); print "Temperature: $obj->{temp}\n"; print "Tmode: $obj->{tmode}\n"; print "Fmode: $obj->{fmode}\n"; print "Override: $obj->{override}\n"; print "Hold: $obj->{hold}\n"; print "tcool: $obj->{t_cool}\n"; print "tstate: $obj->{tstate}\n"; print "fstate: $obj->{fstate}\n"; print "day: $obj->{time}{day}\n"; print "hour: $obj->{time}{hour}\n"; print "minute: $obj->{time}{minute}\n"; print "t_type_post $obj->{t_type_post}\n"; print "________\n"; }
which outputs
Temperature: 75.5 Tmode: 2 Fmode: 0 Override: 0 Hold: 0 tcool: 75 tstate: 0 fstate: 0 day: 4 hour: 13 minute: 49 t_type_post 0 ________

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re^2: Proper use of split by kennethk
in thread Proper use of split by th3j4ckl3

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.