Home: Perl Programming Help: Intermediate:
Unpack - bad result (from file)



waltz
Novice

Oct 5, 2011, 5:53 AM


Views: 1485
Unpack - bad result (from file)

Hi,

what is wrong on code bellow ? ...on output i except three number - no only one :( can youi help me ?


Code
open I, ">d:\\a.bin"; 
binmode I;
print I pack('i3', 444, 555, 666); # three integers
close I;

# read
open O, "d:\\a.bin";
binmode I;

while (read(O, $b, 12) != 0) { # one integer has 4 bytes (4*3 as one block of three integers)
print unpack('i3', $b) . "\n"; # ???
}

close O;


Thank you


BillKSmith
Veteran

Oct 5, 2011, 8:39 AM


Views: 1463
Re: [waltz] Unpack - bad result (from file)

The concatenation operator "." in your print statement forces unpack into scalar context. In scalar context, unpack returns only the first item. Change the "." to "," to fix this problem.

Use split or $, to separate the print fields.
Good Luck,
Bill