unpack - strange behaviour

I

ifiaz

The following program run:

($field1, $field2, $field3, $field4, $field5) = unpack("A4 A1 A3 A1
A*", "this is v strange");

print "|$field1|\n";
print "|$field2|\n";
print "|$field3|\n";
print "|$field4|\n";
print "|$field5|\n";

and the result is this:

|this|
||
|is|
|v|
| strange|

But, I expect a space for $field2 i.e. | |

Why $field4 can print with a single character and
$field2 can't. Am I missing something?
 
A

Anno Siegel

ifiaz said:
The following program run:

($field1, $field2, $field3, $field4, $field5) = unpack("A4 A1 A3 A1
A*", "this is v strange");

print "|$field1|\n";
print "|$field2|\n";
print "|$field3|\n";
print "|$field4|\n";
print "|$field5|\n";

and the result is this:

|this|
||
|is|
|v|
| strange|

But, I expect a space for $field2 i.e. | |

Why $field4 can print with a single character and
$field2 can't. Am I missing something?

Yes. Perldoc -f pack:

as necessary. When unpacking, "A" strips
trailing spaces and nulls, "Z" strips

Use "a" instead.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top