[30207] in Perl-Users-Digest
Perl-Users Digest, Issue: 1450 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Apr 15 16:30:53 2008
Date: Tue, 15 Apr 2008 13:30:41 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 15 Apr 2008 Volume: 11 Number: 1450
Today's topics:
Unpack - getting values directly into the correct varia PugetSoundSylvia@gmail.com
Re: Unpack - getting values directly into the correct v xhoster@gmail.com
Re: Unpack - getting values directly into the correct v <someone@example.com>
Re: Unpack - getting values directly into the correct v <1usa@llenroc.ude.invalid>
Re: Unpack - getting values directly into the correct v <someone@example.com>
Re: Unpack - getting values directly into the correct v <1usa@llenroc.ude.invalid>
Re: Unpack - getting values directly into the correct v <nospam-abuse@ilyaz.org>
Re: Unpack - getting values directly into the correct v <someone@example.com>
Re: Unpack - getting values directly into the correct v <hjp-usenet2@hjp.at>
Re: Unpack - getting values directly into the correct v <nobull67@gmail.com>
use modules OS dependent sledz@dresearch.de
Re: use modules OS dependent <noreply@gunnar.cc>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 10 Apr 2008 08:52:55 -0700 (PDT)
From: PugetSoundSylvia@gmail.com
Subject: Unpack - getting values directly into the correct variables
Message-Id: <a9c1aa0a-7f6f-4d7d-94e9-dc474b1ba039@t12g2000prg.googlegroups.com>
Hello all,
I'm just starting to get into perl, so please forgive me if I'm asking
something obvious.
I'm using the unpack function to parse through a fixed length file.
It's working well, but there's lots and lots of fields, and I'd like
to make the code more readable.
What I have now is this:
$LOGFILE = "text.log";
open(LOGFILE) or die("Could not open log file.");
# Define the record format for unpack function
$BaseBSPFileTemplate =
"A3" # RecordType Field 0
."A8" # SequenceNumber Field 1
."A2" # RecordTypeSuffix Field 2
."A6" # CreateDate Field 3
."A6" # TransactionNumber Field 4
."A15" # DataNumber Field 5
."A98" # UpdateDate Field 6
;
while (<LOGFILE>) {
@fields = unpack( $BaseBSPFileTemplate, $_ );
$RecordType = $fields[0];
$SequenceNumber = $fields[1];
$RecordTypeSuffix = $fields[2];
... and so forth ...
Is there a better way to do this - one where the unpack function
itself would automatically split it into the actual variables
($RecordType, $SequenceNumber, $RecordTypeSuffix, etc) - instead of me
having to have the section that has a bunch of rows like this:
$RecordType = $fields[0];
Thanks much for any advice!!
Sylvia
------------------------------
Date: 10 Apr 2008 16:02:53 GMT
From: xhoster@gmail.com
Subject: Re: Unpack - getting values directly into the correct variables
Message-Id: <20080410120254.889$3p@newsreader.com>
PugetSoundSylvia@gmail.com wrote:
>
> while (<LOGFILE>) {
>
> @fields = unpack( $BaseBSPFileTemplate, $_ );
>
> $RecordType = $fields[0];
> $SequenceNumber = $fields[1];
> $RecordTypeSuffix = $fields[2];
>
> ... and so forth ...
>
> Is there a better way to do this - one where the unpack function
> itself would automatically split it into the actual variables
> ($RecordType, $SequenceNumber, $RecordTypeSuffix, etc) - instead of me
> having to have the section that has a bunch of rows like this:
You can assign directly to a list of variables:
my ( $RecordType,
$SequenceNumber,
$RecordTypeSuffix,
# ....
) = unpack( $BaseBSPFileTemplate, $_ );
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Thu, 10 Apr 2008 17:48:46 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Unpack - getting values directly into the correct variables
Message-Id: <2qsLj.27618$pb5.11173@edtnps89>
PugetSoundSylvia@gmail.com wrote:
>
> I'm just starting to get into perl, so please forgive me if I'm asking
> something obvious.
>
> I'm using the unpack function to parse through a fixed length file.
> It's working well, but there's lots and lots of fields, and I'd like
> to make the code more readable.
I see that Xho has answered your unpack question but ...
> What I have now is this:
You should really have these two lines at the beginning of your program:
use warnings;
use strict;
> $LOGFILE = "text.log";
Which means that you have to declare this variable:
my $LOGFILE = 'text.log';
> open(LOGFILE) or die("Could not open log file.");
And you should really be using the three argument form of open(). As
well, you should include the $! variable in the error message so you
know why it failed:
open LOGFILE, '<', $LOGFILE or die "Could not open '$LOGFILE' $!";
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
------------------------------
Date: Thu, 10 Apr 2008 17:53:05 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Unpack - getting values directly into the correct variables
Message-Id: <Xns9A7C8D3B1C134asu1cornelledu@127.0.0.1>
PugetSoundSylvia@gmail.com wrote in
news:a9c1aa0a-7f6f-4d7d-94e9-dc474b1ba039
@t12g2000prg.googlegroups.co
m:
> Hello all,
>
> I'm just starting to get into perl, so please forgive me if I'm
> asking something obvious.
>
> I'm using the unpack function to parse through a fixed length
> file. It's working well, but there's lots and lots of fields, and
> I'd like to make the code more readable.
>
> What I have now is this:
>
> $LOGFILE = "text.log";
> open(LOGFILE) or die("Could not open log file.");
>
> # Define the record format for unpack function
> $BaseBSPFileTemplate =
> "A3" # RecordType Field 0
> ."A8" # SequenceNumber Field 1
> ."A2" # RecordTypeSuffix Field 2
> ."A6" # CreateDate Field 3
> ."A6" # TransactionNumber Field 4
> ."A15" # DataNumber Field 5
> ."A98" # UpdateDate Field 6
> ;
>
> while (<LOGFILE>) {
>
> @fields = unpack( $BaseBSPFileTemplate, $_ );
>
> $RecordType = $fields[0];
> $SequenceNumber = $fields[1];
> $RecordTypeSuffix = $fields[2];
#!/usr/bin/perl
use strict;
use warnings;
my @fields = qw( RecordType SequenceNumber RecordTypeSuffix
CreateDate TransactionNumber DataNumber UpdateDate );
my %formats;
@formats{ @fields } = qw( A3 A8 A2 A6 A6 A15 A98 );
my $unpack_tmpl = join( '', @formats{ @fields } );
while( <DATA> ) {
last if /^\s+$/;
my %obs;
@obs{ @fields } = unpack $unpack_tmpl;
print "$_\t$obs{$_}\n" for @fields;
}
__END__
00011111111223333334444445555555555555556666666666666666666666666666
66666666666666666666666666666666666666666666666666666666666666666666
66
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Thu, 10 Apr 2008 18:47:44 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Unpack - getting values directly into the correct variables
Message-Id: <khtLj.27964$pb5.25660@edtnps89>
A. Sinan Unur wrote:
> PugetSoundSylvia@gmail.com wrote in
> news:a9c1aa0a-7f6f-4d7d-94e9-dc474b1ba039
> @t12g2000prg.googlegroups.co
> m:
>
>> Hello all,
>>
>> I'm just starting to get into perl, so please forgive me if I'm
>> asking something obvious.
>>
>> I'm using the unpack function to parse through a fixed length
>> file. It's working well, but there's lots and lots of fields, and
>> I'd like to make the code more readable.
>>
>> What I have now is this:
>>
>> $LOGFILE = "text.log";
>> open(LOGFILE) or die("Could not open log file.");
>>
>> # Define the record format for unpack function
>> $BaseBSPFileTemplate =
>> "A3" # RecordType Field 0
>> ."A8" # SequenceNumber Field 1
>> ."A2" # RecordTypeSuffix Field 2
>> ."A6" # CreateDate Field 3
>> ."A6" # TransactionNumber Field 4
>> ."A15" # DataNumber Field 5
>> ."A98" # UpdateDate Field 6
>> ;
>>
>> while (<LOGFILE>) {
>>
>> @fields = unpack( $BaseBSPFileTemplate, $_ );
>>
>> $RecordType = $fields[0];
>> $SequenceNumber = $fields[1];
>> $RecordTypeSuffix = $fields[2];
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my @fields = qw( RecordType SequenceNumber RecordTypeSuffix
> CreateDate TransactionNumber DataNumber UpdateDate );
>
> my %formats;
> @formats{ @fields } = qw( A3 A8 A2 A6 A6 A15 A98 );
>
> my $unpack_tmpl = join( '', @formats{ @fields } );
>
> while( <DATA> ) {
> last if /^\s+$/;
> my %obs;
> @obs{ @fields } = unpack $unpack_tmpl;
Don't forget the variable that you want to unpack:
@obs{ @fields } = unpack $unpack_tmpl, $_;
> print "$_\t$obs{$_}\n" for @fields;
>
> }
>
> __END__
> 00011111111223333334444445555555555555556666666666666666666666666666
> 66666666666666666666666666666666666666666666666666666666666666666666
> 66
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
------------------------------
Date: Thu, 10 Apr 2008 23:27:00 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Unpack - getting values directly into the correct variables
Message-Id: <Xns9A7CC5D7D7751asu1cornelledu@127.0.0.1>
"John W. Krahn" <someone@example.com> wrote in news:khtLj.27964
$pb5.25660@edtnps89:
> A. Sinan Unur wrote:
...
>> @obs{ @fields } = unpack $unpack_tmpl;
>
> Don't forget the variable that you want to unpack:
>
> @obs{ @fields } = unpack $unpack_tmpl, $_;
I didn't ;-) From perldoc -f unpack:
unpack TEMPLATE,EXPR
unpack TEMPLATE
...
If EXPR is omitted, unpacks the $_ string.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Thu, 10 Apr 2008 23:50:31 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: Unpack - getting values directly into the correct variables
Message-Id: <ftm947$1q6b$1@agate.berkeley.edu>
[A complimentary Cc of this posting was sent to
<PugetSoundSylvia@gmail.com>], who wrote in article <a9c1aa0a-7f6f-4d7d-94e9-dc474b1ba039@t12g2000prg.googlegroups.com>:
> $BaseBSPFileTemplate =
> "A3" # RecordType Field 0
> ."A8" # SequenceNumber Field 1
> ."A2" # RecordTypeSuffix Field 2
> ."A6" # CreateDate Field 3
> ."A6" # TransactionNumber Field 4
> ."A15" # DataNumber Field 5
> ."A98" # UpdateDate Field 6
> ;
>
> while (<LOGFILE>) {
>
> @fields = unpack( $BaseBSPFileTemplate, $_ );
>
> $RecordType = $fields[0];
> $SequenceNumber = $fields[1];
> $RecordTypeSuffix = $fields[2];
What a horror... Here is an example (Audio::FindChunks):
my $wav_header = <<EOH;
a4 # header: 'RIFF'
V # size: Size of what follows
a4 # type: 'WAVE'
a4 # type1: 'fmt ' subchunk
V # size1: Size of the rest of subchunk
v # format: 1 for pcm
v # channels: 2 stereo 1 mono
V # frequency
V # bytes_per_sec
v # bytes_per_sample
v # bits_per_sample_channel
a4 # type2: 'data' subchunk
V # sizedata: Size of the rest of subchunk
EOH
my @wav_fields = ($wav_header =~ /^\s*\w+\s*#\s*(\w+)/mg);
$wav_header =~ s/#.*//g; # For v5.005
...
@vals{@wav_fields} = unpack $wav_header, $in or ...
Hope this helps,
Ilya
------------------------------
Date: Fri, 11 Apr 2008 04:31:30 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Unpack - getting values directly into the correct variables
Message-Id: <CQBLj.58897$_v3.18045@edtnps90>
A. Sinan Unur wrote:
> "John W. Krahn" <someone@example.com> wrote in news:khtLj.27964
> $pb5.25660@edtnps89:
>
>> A. Sinan Unur wrote:
> ...
>
>>> @obs{ @fields } = unpack $unpack_tmpl;
>> Don't forget the variable that you want to unpack:
>>
>> @obs{ @fields } = unpack $unpack_tmpl, $_;
>
> I didn't ;-) From perldoc -f unpack:
>
> unpack TEMPLATE,EXPR
> unpack TEMPLATE
> ...
> If EXPR is omitted, unpacks the $_ string.
That must be new for 5.10 cause it doesn't work in 5.8.8
$ perl -le'
$_ = q[1234567890123456789012345678901234567890];
print for unpack q[a4 a7 a3];
'
Not enough arguments for unpack at -e line 3, near "q[a4 a7 a3];"
Execution of -e aborted due to compilation errors.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
------------------------------
Date: Sat, 12 Apr 2008 11:49:09 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Unpack - getting values directly into the correct variables
Message-Id: <slrng011cp.uag.hjp-usenet2@hrunkner.hjp.at>
On 2008-04-10 23:50, Ilya Zakharevich <nospam-abuse@ilyaz.org> wrote:
><PugetSoundSylvia@gmail.com>], who wrote in article <a9c1aa0a-7f6f-4d7d-94e9-dc474b1ba039@t12g2000prg.googlegroups.com>:
>> $BaseBSPFileTemplate =
>> "A3" # RecordType Field 0
>> ."A8" # SequenceNumber Field 1
>> ."A2" # RecordTypeSuffix Field 2
>> ."A6" # CreateDate Field 3
>> ."A6" # TransactionNumber Field 4
>> ."A15" # DataNumber Field 5
>> ."A98" # UpdateDate Field 6
>> ;
>>
>> while (<LOGFILE>) {
>>
>> @fields = unpack( $BaseBSPFileTemplate, $_ );
>>
>> $RecordType = $fields[0];
>> $SequenceNumber = $fields[1];
>> $RecordTypeSuffix = $fields[2];
>
> What a horror...
You could have phrased that more politely :-).
Actually, I think that's pretty low on the horror-scale. Tedious and
redundant, yes, but I've seen much worse code.
> Here is an example (Audio::FindChunks):
>
> my $wav_header = <<EOH;
> a4 # header: 'RIFF'
> V # size: Size of what follows
> a4 # type: 'WAVE'
>
> a4 # type1: 'fmt ' subchunk
> V # size1: Size of the rest of subchunk
> v # format: 1 for pcm
> v # channels: 2 stereo 1 mono
> V # frequency
> V # bytes_per_sec
> v # bytes_per_sample
> v # bits_per_sample_channel
>
> a4 # type2: 'data' subchunk
> V # sizedata: Size of the rest of subchunk
> EOH
>
> my @wav_fields = ($wav_header =~ /^\s*\w+\s*#\s*(\w+)/mg);
>
> $wav_header =~ s/#.*//g; # For v5.005
> ...
> @vals{@wav_fields} = unpack $wav_header, $in or ...
Clever. Maybe a bit too clever for production code. At least I would add
a comment about the format (so that the next maintainer doesn't break
it) and why it works.
I'd use an array of arrays instead:
#!/usr/bin/perl
use warnings;
use strict;
my @BaseBSPFileFormat = (
["RecordType", "A3" ],
["SequenceNumber", "A8" ],
["RecordTypeSuffix", "A2" ],
["CreateDate", "A6" ],
["TransactionNumber", "A6" ],
["DataNumber", "A15" ],
["UpdateDate", "A98" ],
);
my $BaseBSPFileTemplate = join('', map $_->[1], @BaseBSPFileFormat);
my @BaseBSPFileFields = map $_->[0], @BaseBSPFileFormat;
while (<DATA>) {
my %fields;
@fields{@BaseBSPFileFields} = unpack( $BaseBSPFileTemplate, $_ );
for (@BaseBSPFileFields) {
print "$_: $fields{$_}\n";
}
print "\n"
}
__DATA__
111222222223344444455555566666666666666677777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777
111222222223344444455555566666666666666677777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777
hp
------------------------------
Date: Sat, 12 Apr 2008 03:03:41 -0700 (PDT)
From: Brian McCauley <nobull67@gmail.com>
Subject: Re: Unpack - getting values directly into the correct variables
Message-Id: <bfd91bfe-5ff0-4ce5-a677-187a521431f7@8g2000hsu.googlegroups.com>
On Apr 11, 5:31 am, "John W. Krahn" <some...@example.com> wrote:
> A. Sinan Unur wrote:
> > "John W. Krahn" <some...@example.com> wrote in news:khtLj.27964
> > unpack TEMPLATE,EXPR
> > unpack TEMPLATE
> > ...
> > If EXPR is omitted, unpacks the $_ string.
>
> That must be new for 5.10
And about $EXPLETIVE time! It's always bugged me that unpack didn't do
this.
------------------------------
Date: Mon, 14 Apr 2008 06:24:14 -0700 (PDT)
From: sledz@dresearch.de
Subject: use modules OS dependent
Message-Id: <c6676a33-ae66-4329-9b4c-f704244761e6@v26g2000prm.googlegroups.com>
I'm writing an perl script which should communicate over a serial
port. The script should be able to run in Linux and Win32
environments. In both environments exist modules to access the serial
port:
Win32::SerialPort (under Windows)
Device::SerialPort (else)
I know it is possible to detect the OS using Config::Config. But it
seems not possible to use different modules depending on this
information like this:
if ( $OS eq 'WINDOWS' ) {
use Win32::SerialPort qw( :PARAM :STAT );
} else {
use Device::SerialPort qw( :PARAM :STAT );
}
What's the right way to write such an OS dependent application?
Steffen
PS: Fup to comp.lang.perl.modules
------------------------------
Date: Mon, 14 Apr 2008 15:26:46 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: use modules OS dependent
Message-Id: <66h4g0F2kd0c4U2@mid.individual.net>
sledz@dresearch.de wrote:
> I'm writing an perl ...
I just answered your question in clpmodules. Please show some patience.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 1450
***************************************