GrandFather has asked for the wisdom of the Perl Monks concerning the following question:
I wanted to generate a sequence of binary numbers so I tried code like:
print unpack ('B16', $_)."\n" for (0..10);
which generated:
00110000 00110001 00110010 00110011 00110100 00110101 00110110 00110111 00111000 00111001 0011000100110000
which is not what I expected! Where I expected that the for loop variable would be stored as a number, it seems to be being stored as a numeric string: '0'..'10'. Why is that? Is it possible to coerce Perl into storing a number as an integer in that sort of context?
Note that I have solved my initial problem using an sprintf, which may well be what I should have used in the first place.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: unpack in incrementing variables
by davido (Cardinal) on Oct 14, 2005 at 02:32 UTC | |
|
Re: unpack in incrementing variables
by demerphq (Chancellor) on Oct 14, 2005 at 07:54 UTC | |
by GrandFather (Saint) on Oct 14, 2005 at 09:38 UTC | |
by demerphq (Chancellor) on Oct 14, 2005 at 09:44 UTC | |
by GrandFather (Saint) on Oct 14, 2005 at 10:31 UTC | |
by Roy Johnson (Monsignor) on Oct 14, 2005 at 17:59 UTC | |
|
Re: unpack in incrementing variables
by ikegami (Patriarch) on Oct 14, 2005 at 01:58 UTC | |
by GrandFather (Saint) on Oct 14, 2005 at 02:14 UTC | |
by GrandFather (Saint) on Oct 14, 2005 at 02:21 UTC | |
by ikegami (Patriarch) on Oct 14, 2005 at 02:32 UTC | |
|
Re: unpack in incrementing variables
by pg (Canon) on Oct 14, 2005 at 02:00 UTC | |
|
Re: unpack in incrementing variables
by EvanCarroll (Chaplain) on Oct 14, 2005 at 02:18 UTC |