Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > trailing spaces in unpack("A10")

Reply
Thread Tools

trailing spaces in unpack("A10")

 
 
Klaus
Guest
Posts: n/a
 
      05-08-2008
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
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      05-08-2008
>>>>> "K" == Klaus <(E-Mail Removed)> writes:

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 ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- 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 ---------
 
Reply With Quote
 
 
 
 
Klaus
Guest
Posts: n/a
 
      05-08-2008
On May 8, 11:16 pm, Sherman Pendley <(E-Mail Removed)> wrote:
> Klaus <(E-Mail Removed)> writes:
> > 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.

>
> Have a look at 'perldoc -f pack' - A capital 'A' indicates that the field
> will be space-padded. It seems that unpack is doing the opposite, removing
> any trailing spaces.
>
> A lowercase 'a' indicates a null-padded string - what happens when you use
> that instead?


That's it, lowercase 'a' works fine (i.e. it preserves trailing
spaces). Thanks to you, and to uri, who pointed me to the relevant
part of the documentation.

--
Klaus
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSD to allow A-Z 0-9 and spaces, but not leading/trailing spaces johkar XML 2 12-10-2009 09:24 AM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM
Removing Trailing spaces from Excel using asp.net Ben ASP .Net 5 01-16-2005 06:56 PM
add ability to strip off leading/trailing spaces from email regularExpressionValidator? Les Caudle ASP .Net 0 03-07-2004 12:20 PM



Advertisments