|
Prev: FAQ 8.45 How do I install a module from CPAN?
Next: FAQ 8.47 How do I keep my own module/library directory?
From: PugetSoundSylvia on 10 Apr 2008 11:52 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
From: xhoster on 10 Apr 2008 12:02 PugetSoundSylvia(a)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.
From: John W. Krahn on 10 Apr 2008 13:48 PugetSoundSylvia(a)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
From: A. Sinan Unur on 10 Apr 2008 13:53 PugetSoundSylvia(a)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(a)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/
From: John W. Krahn on 10 Apr 2008 14:47 A. Sinan Unur wrote: > PugetSoundSylvia(a)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
|
Next
|
Last
Pages: 1 2 Prev: FAQ 8.45 How do I install a module from CPAN? Next: FAQ 8.47 How do I keep my own module/library directory? |