I tried your code on my machine, with a few additions/modifications to get it running, all of them marked with "# **" below. I didn't see any problem with the output. The only really significant modifications were to take the "my" away from the "$c" in the while loop, since it was re-initializing $c with each pass through the loop, and to add binmode(IN), which actually doesn't make any difference with the data I'm using.

Try this modified code on your machine and see if you get appropriate output. It only processes the first two numbers, so it should be easy to see what's going on and play with it.

use warnings; # ** use strict; # ** my $data; # ** my $c = 0; # ** my $outfile = 'tempo.pk'; # ** my $infile = shift; # ** open(IN, " < $infile" ); binmode(IN); # ** prevents newline translation my $si=2; # the number of entries is known my %matrix; while($c<$si) { # ** removed my read(IN, my $bin, 4); $data = unpack('N', $bin); my $d = Unshuffle($c); print "$c -> $d ($data)\n"; # ** $matrix{$d} += $data/3; # ** $c++; } close( IN ); open(OUT, " >$outfile" ); foreach my $d (sort {$a <=> $b} (keys %matrix)) { print "$outfile $d ($matrix{$d})\n"; # ** print (OUT pack('V', $matrix{$d})); } close( OUT ); open(IN, " <$outfile" ); # ** while( read(IN, my $bin, 4) ) { # ** $data = unpack('V', $bin); # ** print "$data:"; # ** } # ** sub Unshuffle { # ** interchange the first two positions my $val = shift; return 1 if ($val == 0); return 0 if ($val == 1); return $val; } # output was # 0 -> 1 (2140) # 1 -> 0 (1544028160) # tempo.pk 0 (514676053.333333) # tempo.pk 1 (713.333333333333) # 514676053:713:

In reply to Re^2: Data munging - in, out and something in between by rodion
in thread Data munging - in, out and something in between by Anonymous Monk

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.