On Oct 29, 11:09 am, [EMAIL PROTECTED] (John W . Krahn) wrote: > On Monday 29 October 2007 06:42, Mike Tran wrote: > > > Hey all, > > Hello, > > > I'm new with Perl and need help with this simple script. I'm still > > playing around with the script below to get a feel for Perl. My > > script below is incomplete and I'm doing an array within an array > > which is incorrect. Please help. > > You are not using arrays you are using hashes. > > > > > Here's what I want to do; I have to flat files (pipe delimited, > > export from a database) that I want to parse through and assign > > variables for each column. Basically, I want to parse through > > exclude_bases.txt and do: if base_no in exclude_bases.txt equals to > > base_no in base.txt then search in the "description" field of > > base.txt for the string listed in the "keyword" field in > > exclude_bases.tx and replace with "new_keyword" in exclude_bases.txt > > and write the out put into a new file called "new_bases.txt". > > > Any suggestions on how I could accomplish the above task is greatly > > appreciated. Thanks all. > > > Flat Files: > > > base.txt: > > base_no|name|description > > 10000|test|test desc > > 10001|test2|test desc 2 > > 10002|test3|test desc 3 > > > exclude_bases.txt: > > base_no|keyword|new_keyword| > > 10000|test desc|testdesc|0 > > 10001|test desc 2|testdesc2|0 > > 10002|test desc 3|testdesc3|1 > [snip]
> It looks like you may want something like this: > > #!/usr/bin/perl > use strict; > use warnings; > > my $exclude_bases = 'exclude_bases.txt'; > my $current_base = 'base.txt'; > my $output = 'new_bases.txt'; > > open EXCLUDE, '<', $exclude_bases or die "Could not open > '$exclude_bases' $!"; > > my %exclude_bases; > while ( <EXCLUDE> ) { > next if $. == 1; # exclude header > chomp; > my ( $exbase_no, $keyword, $new_keyword ) = split /\|/; > $exclude_bases{ $exbase_no } = { from => qr/\Q$keyword/, to => > $new_keyword }; > } Since you're not taking into account the blank lines, you'll be generating these warnings: Use of uninitialized value in quotemeta at C:\test\JohnKrahn.pl line 17, <EXCLUDE> line 2. Use of uninitialized value in hash element at C:\test\JohnKrahn.pl line 17, <EXCLUDE> line 2. Use of uninitialized value in quotemeta at C:\test\JohnKrahn.pl line 17, <EXCLUDE> line 4. Use of uninitialized value in hash element at C:\test\JohnKrahn.pl line 17, <EXCLUDE> line 4. > > close EXCLUDE; > > open BASE, '<', $current_base or die "Could not open '$current_base' > $!"; > open OUT, '>', $output or die "Could not open '$output' $!"; > > while ( <BASE> ) { > my ( $base_no, $name, $description ) = split /\|/; > if ( exists $exclude_bases{ $base_no } ) { > $description =~ > s/$exclude_bases{$base_no}{from}/$exclude_bases{$base_no}{to}/g; > $_ = join '|', $base_no, $name, $description; > } > print OUT; > } Again, blank lines are not taken into account, but no warnings since the printing is being handled within the conditional block. Since a complete solution, to what appears to be a homework question, has already been provided, I guess I'll show mine. #!/usr/bin/perl use warnings; use strict; my %base; open (my $new, '>', 'new_bases.txt') || die "new_bases.txt <$!>"; open (my $exclude, '<', 'exclude_bases.txt') || die "exclude_bases.txt <$!>"; open (my $base, '<', 'base.txt') || die "base.txt <$!>"; while (<$base>) { next if /^\s*$/; # skip over blank lines print and next if $. == 1; # print header chomp; my @fields = split /\|/; $base{$fields[0]} = [EMAIL PROTECTED]; } close $base; while (<$exclude>) { next if /^\s*$/; # skip over blank lines chomp; my @fields = split /\|/; if ( exists $base{$fields[0]} and $fields[1] eq $base{$fields[0]} [2]) { $base{$fields[0]}[2] = $fields[2]; # if you want to retain the empty lines between records, # you can use "\n\n" in the print statement, # or reassign the output record separator print $new join('|', @{$base{$fields[0]}}), "\n"; } } close $exclude; close $new; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/