in reply to Proper use of split

First let me say thank you for the wealth of good information. I have been doing a lot of reading in areas of various parts of perl that have been called out in each of your replies. Terms like JSON, etc. Again, I am new and its a learning process. Using a personal project as a referance point for me makes it a little bit fun. I think the first response on using the map and undef helped me the most for now in understanding how I can carve up my data, but for some reason in my original post I think my example got butchered and I ran into another problem. It looks the editor field on this website does some strange wrapping of the line I am trying to work with. The end of the data in tstatcollect has a subsection that seems to break the original splits.

{"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 +_p ost":0}
My working code now looks like
#! /usr/bin/perl open (FILE, '/home/julian/tstatcollect'); while (<FILE>) { chomp; (undef,$temp, undef,$tmode, undef,$fmode, undef,$override, undef,$hold +, undef,$tcool, undef,$tstate, undef,$fstate, undef,$time, undef,$day +, undef,$hour, undef,$minute, undef,$t_type_post,) = map split (":"), + split (","), ; print "Temperature: $temp\n"; print "Tmode: $tmode\n"; print "Fmode: $fmode\n"; print "Override: $override\n"; print "Hold: $hold\n"; print "tcool: $tcool\n"; print "tstate: $tstate\n"; print "fstate: $fstate\n"; print "day: $day\n"; print "hour: $hour\n"; print "minute: $minute\n"; print "t_type_post $t_type_post\n"; print "________\n"; } close (FILE); exit;
With an output now of
Temperature: 75.50 Tmode: 2 Fmode: 0 Override: 0 Hold: 0 tcool: 75.00 tstate: 0 fstate: 0 day: "hour" hour: "minute" minute: "t_type_post" t_type_post "
It seems as though when the regular format changes to something it doesnt expect the continuing parsing of the data goes to trash? My thought being is do I need do break the data into two forms. One with the first portion and another with the date time nested portion? I also liked the statement made earlier about collecting the data regardless of where its at with regards to the delimiter? Interesting. I apprecaite all the help and its giving me a lot of reading and researching to do for this weekend. I know there is a ton of resources and books written on the subject but without a referance point to start it made it hard to know what I was looking for. This gives me more to work with. Not looking for answers to the problem as much as I am looking for places to start learning. Thanks again. Julian

Replies are listed 'Best First'.
Re^2: Proper use of split
by kennethk (Abbot) on Jun 01, 2012 at 19:25 UTC
    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.

      This is excellent, kennethk!