I think the problem stems from the fact that you're using $last_value{ $id } as a value and as a hash reference. You should only use it as a value. I see you commented out use strict; which would have pointed you to that problem immediately:

Can't use string ("00 00 00 00 00 00 00 00") as a HASH ref while "stri +ct refs" in use at q:\tmp.pl line 24, <DATA> line 1.

If you change the code to always use hash references, it works for me:

#$last_value{ $id } = $payload; $last_value{ $id } ||= {}; if(defined $last_value{ $id } ) { $last_value{ $id }{COUNT} += 1; # If we last saw something from this device, note the +time difference if( $last_value{ $id }{TIME} ) { $time_diff = $now_milli - $last_value{ $id }{TIME}; } else { # Otherwise, print 0 $time_diff= 0; };

When printing out, you print $last_value{ $id }, which will not be a useful value. Change it to print $last_value{ $id }{PAYLOAD}.

Maybe now, %last_value should be renamed to %last_response in your code...

The following code works for me:

#!/usr/bin/perl -w use strict; use Time::HiRes qw/gettimeofday/; my $PCAN="/dev/pcan32"; my $now_milli = 1000 * gettimeofday(); my %last_value; my $time_diff; #open my $can_bus, "receivetest -f=$PCAN | sed -e \'s/receivetest: m s + 0x//g\' |" # or die "Couldn't read from CAN bus: $!"; my $can_bus= *DATA; while (<$can_bus>) { if( /^(........\.\d{1,3}) (........) (.*)$/ ) { # system('clear'); #clear screen my ($something,$id, $payload) = ($1,$2,$3); $last_value{ $id } ||= {}; if(defined $last_value{ $id } ) { $last_value{ $id }{COUNT} += 1; if( $last_value{ $id }{TIME} ) { $time_diff = $now_milli - $last_value{ $id }{TIME}; } else { $time_diff= 0; }; $last_value{ $id }{TIME} = $now_milli; $last_value{ $id }{DIFF} = $time_diff; $last_value{ $id }{PAYLOAD} = $payload; } else { #$last_value{ $id }{COUNT} = 1; $last_value{ $id }{TIME} = $now_milli; $last_value{ $id }{DIFF} = "NA"; $last_value{ $id }{PAYLOAD} = $payload; } #Print Table for my $id (sort keys %last_value) { print "$id\t $last_value{ $id }{PAYLOAD}\t $last_value{ $i +d }{COUNT}\t $last_value{ $id }{DIFF}\n"; } } else { warn "ignore unknown line: "; warn $_; } } #/^(........\.\d{1,3}) (........) (.*) __DATA__ 12345678.123 0CFFE000 00 00 00 00 00 00 00 00 87654321.321 18FEF501 00 FF 32 C4 FF 00 00 00 43215678.132 0C11FF00 FF FF FF FF FF FF FF FF 12348765.312 18FFE100 23 E0 C1 0E FF 00 AC 00 43218765.112 00000000 01 FF FF FF FF FF FF FF 12345678.123 0CFFE000 01 00 00 00 00 00 00 00 87654321.321 18FEF501 00 00 00 00 00 00 00 00 43215678.132 0C11FF00 00 00 00 00 00 00 00 00 12348765.312 18FFE100 00 00 00 00 00 00 00 00 43218765.112 00000000 00 00 00 00 00 00 00 00 12345678.123 0CFFE000 00 00 00 00 00 00 00 00

In reply to Re^5: Perl and Can Bus by Corion
in thread Perl and Can Bus by Jalcock501

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.