|
Posted by Klaus on May 8, 2008, 3:52 pm
Please log in for more thread options
According to "perldoc perlpacktut", I could use unpack to extract
fixed length fields:
from "perldoc perlpacktut":
+++++++++++++++++++++++++++++++++++++
++ [...] we can see that the date column stretches from
++ column 1 to column 10 - ten characters wide. The
++ pack-ese for "character" is A, and ten of them are
++ A10. So if we just wanted to extract the dates, we
++ could say this:
++
++ my($date) = unpack("A10", $_);
+++++++++++++++++++++++++++++++++++++
So I have created a test case with 3 A's then 7 spaces, followed by 3
B's:
use strict;
use warnings;
$_ = 'AAA BBB';
my $result = unpack("A10", $_);
print "result = '$result'\n";
This is the output:
result = 'AAA'
It basically works, but unpack seems to remove trailing spaces.
To my understanding of "perldoc perlpacktut", the output should show
as result a field of 10 characters (3 A's followed by 7 spaces), but
actually it only shows 3 A's.
Where have the 7 spaces gone ?
I can easily fill up the missing spaces with an sprintf("%-10s"), but
can somebody point me to the documentation where it says that
unpack("A10") removes trailing spaces ?
btw, I am using perl 5.10 under Win XP
--
Klaus
|
|
Posted by Uri Guttman on May 8, 2008, 4:22 pm
Please log in for more thread options
K> According to "perldoc perlpacktut", I could use unpack to extract
K> fixed length fields:
K> from "perldoc perlpacktut":
K> +++++++++++++++++++++++++++++++++++++
K> ++ [...] we can see that the date column stretches from
K> ++ column 1 to column 10 - ten characters wide. The
K> ++ pack-ese for "character" is A, and ten of them are
K> ++ A10. So if we just wanted to extract the dates, we
K> ++ could say this:
K> ++
K> ++ my($date) = unpack("A10", $_);
K> +++++++++++++++++++++++++++++++++++++
K> So I have created a test case with 3 A's then 7 spaces, followed by 3
K> B's:
K> use strict;
K> use warnings;
K> $_ = 'AAA BBB';
K> my $result = unpack("A10", $_);
K> print "result = '$result'\n";
K> This is the output:
K> result = 'AAA'
K> It basically works, but unpack seems to remove trailing spaces.
from perldoc -f pack:
The "a", "A", and "Z" types gobble just one value, but pack it
as a string of length count, padding with nulls or spaces as
necessary. When unpacking, "A" strips trailing spaces and
nulls, "Z" strips everything after the first null, and "a"
returns data verbatim. When packing, "a", and "Z" are
equivalent.
K> Where have the 7 spaces gone ?
where the docs said they would go. use the 'a' format to keep the spaces.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
|
| Similar Threads | Posted | | remove CR + trailing spaces | December 19, 2007, 2:08 pm |
| Trailing Spaces -- Format Perl | September 29, 2004, 2:13 pm |
| How can I search and replace a string while preserving (not removing) trailing spaces? | May 12, 2007, 11:43 pm |
| script help - stripping trailing spaces in exisitng script | June 5, 2005, 7:59 pm |
| unpack "B*", 15 | November 14, 2008, 7:48 am |
| pack and unpack ? | January 9, 2005, 11:37 am |
| Extraction of bits using unpack | August 17, 2005, 10:08 am |
| pack /unpack issue | November 10, 2005, 7:48 pm |
| pack, unpack and 64-bit values | February 9, 2006, 12:55 pm |
| replacement of slow unpack | June 28, 2006, 9:34 am |
|