From: James Egan on 14 Nov 2009 14:06 On Fri, 13 Nov 2009 20:37:20 -0600, Tad McClellan wrote: > nobody <nobody(a)nowhere.com> wrote: >> I'm trying to process flat files with many thousands of records. In >> these files several rows comprise the information for a single >> customer. In the example __DATA__ below, I'm trying to fill the >> variables with the customer information while the customer number is >> 06020004293, then for customer number 07020000279, and finally customer >> number 09020000251. I answered my own question. See the block below: elsif ( $code eq 'B' ) { $buffer{Daughter2} = substr $_, 14, 17; if (!defined $buffer{Daughter}) { $buffer{Daughter} = substr $_, 14, 17; } #!/usr/bin/perl use warnings; use strict; my $flag = 0; my %buffer; while ( <DATA> ) { chomp; my $code = substr $_, 12, 1; if ( $code eq 'A' ) { if ( keys %buffer) { output(%buffer); %buffer = (); } $buffer{Name} = substr $_, 14, 17; $buffer{Street} = substr $_, 32, 17; } elsif ( $code eq 'B' ) { $buffer{Daughter2} = substr $_, 14, 17; if (!defined $buffer{Daughter}) { $buffer{Daughter} = substr $_, 14, 17; } } elsif ( $code eq 'C' ) { $buffer{Company} = substr $_, 32, 18; } elsif ( $code eq 'D' ) { $buffer{OldCompany} = substr $_, 14, 18; } else { warn "code '$code' is invalid\n"; } } output(%buffer); sub output { my %h = @_; foreach my $key qw/Name Daughter Daughter2 Company OldCompany Street/ { print "$key: "; print $h{$key} if defined $h{$key}; print "\n"; } print "\n"; } __DATA__ 06020004293 A Fred Flintstone 123 Bedrock Road 06020004293 B Jane Flintstone 123 Bedrock Road 06020004293 B Sue Flintstone 123 Bedrock Road 06020004293 C Bedrock Gravel Pit 06020004293 D Loney Toons 123 Bedrock Road 07020000279 A George Washington 234 Washington Ave. 07020000279 C Washington D.C. 234 Washington Ave. 07020000279 B Linda Washington 234 Washington Ave. 07020000279 B Sue Washington 234 Washington Ave. 09020000251 A Joe Smith 54 Abbey Road 09020000251 C Smallville 54 Abbey Road
From: Ron Bergin on 14 Nov 2009 15:56 On Nov 14, 11:06 am, James Egan <jegan...(a)comcast.net> wrote: > On Fri, 13 Nov 2009 20:37:20 -0600, Tad McClellan wrote: > > nobody <nob...(a)nowhere.com> wrote: > >> I'm trying to process flat files with many thousands of records. In > >> these files several rows comprise the information for a single > >> customer. In the example __DATA__ below, I'm trying to fill the > >> variables with the customer information while the customer number is > >> 06020004293, then for customer number 07020000279, and finally customer > >> number 09020000251. > > I answered my own question. See the block below: > > elsif ( $code eq 'B' ) { > $buffer{Daughter2} = substr $_, 14, 17; > > if (!defined $buffer{Daughter}) { > $buffer{Daughter} = substr $_, 14, 17; > } > > #!/usr/bin/perl > use warnings; > use strict; > > my $flag = 0; > > my %buffer; > while ( <DATA> ) { > chomp; > my $code = substr $_, 12, 1; > > if ( $code eq 'A' ) { > if ( keys %buffer) { > output(%buffer); > %buffer = (); > } > $buffer{Name} = substr $_, 14, 17; > $buffer{Street} = substr $_, 32, 17; > } > elsif ( $code eq 'B' ) { > $buffer{Daughter2} = substr $_, 14, 17; > > if (!defined $buffer{Daughter}) { > $buffer{Daughter} = substr $_, 14, 17; > } > > } > elsif ( $code eq 'C' ) { > $buffer{Company} = substr $_, 32, 18; > } > elsif ( $code eq 'D' ) { > $buffer{OldCompany} = substr $_, 14, 18; > } > else { > warn "code '$code' is invalid\n"; > } > > } > > output(%buffer); > > sub output { > my %h = @_; > foreach my $key qw/Name Daughter Daughter2 Company OldCompany Street/ > { > print "$key: "; > print $h{$key} if defined $h{$key}; > print "\n"; > } > print "\n"; > > } > > __DATA__ > 06020004293 A Fred Flintstone 123 Bedrock Road > 06020004293 B Jane Flintstone 123 Bedrock Road > 06020004293 B Sue Flintstone 123 Bedrock Road > 06020004293 C Bedrock Gravel Pit > 06020004293 D Loney Toons 123 Bedrock Road > 07020000279 A George Washington 234 Washington Ave. > 07020000279 C Washington D.C. 234 Washington Ave. > 07020000279 B Linda Washington 234 Washington Ave. > 07020000279 B Sue Washington 234 Washington Ave. > 09020000251 A Joe Smith 54 Abbey Road > 09020000251 C Smallville 54 Abbey Road Personally, I don't like using a series of if/elsif blocks when testing the same var over and over again. I prefer to use a dispatch table. This version puts the whole file into the hash, but it's very easy to add the few extra lines to only store 1 record at a time like Tad showed. #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %accounts; my %account = ( A => \&Name, B => \&Children, C => \&Company, D => \&OldCompany, ); while ( <DATA> ) { chomp; my $id = substr $_, 0, 11; my $code = substr $_, 12, 1; if ( exists $account{$code} ) { $account{$code}->($id); } else { warn "code '$code' is invalid\n"; } } print Dumper \%accounts; sub Name { $accounts{$_[0]}{Name} = substr $_, 14, 17; $accounts{$_[0]}{Street} = substr $_, 32, 17; } sub Children { push @{$accounts{$_[0]}{Children}}, substr $_, 14, 17; } sub Company { $accounts{$_[0]}{Company} = substr $_, 32, 18; } sub OldCompany { $accounts{$_[0]}{OldCompany} = substr $_, 14, 18; } __DATA__ 06020004293 A Fred Flintstone 123 Bedrock Road 06020004293 B Jane Flintstone 123 Bedrock Road 06020004293 B Sue Flintstone 123 Bedrock Road 06020004293 C Bedrock Gravel Pit 06020004293 D Loney Toons 123 Bedrock Road 07020000279 A George Washington 234 Washington Ave. 07020000279 C Washington D.C. 234 Washington Ave. 07020000279 B Linda Washington 234 Washington Ave. 07020000279 B Sue Washington 234 Washington Ave. 09020000251 A Joe Smith 54 Abbey Road 09020000251 C Smallville 54 Abbey Road
From: Doug Miller on 14 Nov 2009 18:04 In article <poCLm.191369$Ca6.97784(a)en-nntp-03.dc1.easynews.com>, nobody <nobody(a)nowhere.com> wrote: >Hey dude, you're confused. I'm working with various data files in >various formats. Some are flat files, some are delimited. You should >learn some manners, give up your lame attempts at humor, And learn how >to spell genera. You, too. It's "genre". "Genera" is something different.
From: nobody on 14 Nov 2009 18:17 On Sat, 14 Nov 2009 23:04:44 +0000, Doug Miller wrote: > In article <poCLm.191369$Ca6.97784(a)en-nntp-03.dc1.easynews.com>, nobody > <nobody(a)nowhere.com> wrote: > >>Hey dude, you're confused. I'm working with various data files in >>various formats. Some are flat files, some are delimited. You should >>learn some manners, give up your lame attempts at humor, And learn how >>to spell genera. > > You, too. It's "genre". "Genera" is something different. I know that only too well, and stand corrected!
From: Tad McClellan on 14 Nov 2009 20:08 nobody <nobody(a)nowhere.com> wrote: ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^ [ I would suggest choosing a less-generic posting address. I have had that one killfiled for several years. That is, your posts are being scored way down because of what somebody else did while using that address... ] > On Fri, 13 Nov 2009 20:37:20 -0600, Tad McClellan wrote: > > Thanks for your answer, it does exactly as I asked. You're welcome. > However, the data > files I'm dealing with are more complicated. If you change the question, the answer is likely to change. So you should ask the real question right up front. > Any help would be greatly > appreciated again! Sorry, you have already used up all of your coupons. -- Tad McClellan email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: How to get a program output into array Next: Syntax for Perl script |