in reply to Re^3: How do I read a 24 bit integer?
in thread How do I read a 24 bit integer?

Finally understanding this a bit, but it seems like I have one more problem.

First, my code:

read FILE, $myinput, 12; my($var1, $var2, $var3, $var4) = unpack('A3A3A3A3', $myinput); $var1 = unpack ('N', "\x00" . $var1); $var2 = unpack ('N', "\x00" . $var2); $var3 = unpack ('N', "\x00" . $var3); $var4 = unpack ('N', "\x00" . $var4); print "$var1\n"; print "$var2\n"; print "$var3\n"; print "$var4\n";
The problem is that some of these values are legitimately zero, and when I try to print them, I get:
Use of uninitialized value in concatenation (.) or string at myscript. +pl line 20.
What did I miss to allow zeroes?

Replies are listed 'Best First'.
Re^5: How do I read a 24 bit integer?
by JavaFan (Canon) on Jan 02, 2010 at 11:28 UTC
    Are you sure you actually read 12 bytes? Because it seems one of your variables wasn't set by the unpack (probably the $var4).
      I have absolutely no idea. :( But this code:
      #!/usr/bin/perl -w use strict; my $myinput="\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06"; my($var1, $var2, $var3, $var4) = unpack('A3A3A3A3', $myinput); $var1 = unpack ('N', "\x00" . $var1); $var2 = unpack ('N', "\x00" . $var2); $var3 = unpack ('N', "\x00" . $var3); $var4 = unpack ('N', "\x00" . $var4); print "$var1\n"; print "$var2\n"; print "$var3\n"; print "$var4\n";
      produces:
      Use of uninitialized value in concatenation (.) or string at ./test2.p +l line 10. Use of uninitialized value in concatenation (.) or string at ./test2.p +l line 11. 66051 263430
      when I would expect to see:
      0 0 66051 264430

        Use a template of '(a3)*' ('a' not 'A'), to fix the problem.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.