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
>
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $exclude_bases = "exclude_bases.txt";
> my $current_base = "base.txt";
> my $output = "new_bases.txt";
>
> my %exclude_bases;
> my $exclude_text;
> my $exbase_no;
> my $keyword;
> my $new_keyword;
You should define variables in the smallest possible scope.
> open(EXCLUDE,"exclude_bases.txt" )|| die("Could not open file!");
You have stored the file name in the $exclude_bases variable above so
why do you not use the variable instead? You should include the $!
variable in the error message so you know *why* it failed.
> %exclude_bases=<EXCLUDE>;
You are assigning the list from the file to a hash. The list elements
are defined by the value of the $/ variable so here each element of the
list is a line from the file. Because you are assigning to a hash the
first element is a key and the second element is the value for that
key. if any of the keys (odd elements) are the same as a previous key
then the old value will be overwritten by the new value and you will
lose lines from the file.
> close(EXCLUDE);
>
> my $base_no ="";
> my $name="";
> my $description="";
> my $current_base="";
> my $base_text="";
> my %bases;
>
> open(BASE,"base.txt")|| die("Could not open file!");
Same as above.
> %bases=<BASE>;
Same as above.
> close(BASE);
>
> #choping lines and assign variables to base.txt
> foreach $base_text (%bases)
> {
> chop($base_text);
You should use chomp() instead of chop().
> ($base_no,$name,$description)=split(/\|/,$base_text);
>
> #choping lines and assign variables to exclude_bases.txt
> foreach $exclude_text (%exclude_bases)
> {
> chop($exclude_text);
Same as above.
> ($exbase_no,$keyword,$new_keyword)=split(/\|/,$base_text);
>
> if ($exbase_no=$base_no) {
You are assigning the value of $base_no to the variable $exbase_no. If
the value of $base_no is "true" then the expression is "true". You
want to use a comparison operator like '==' or 'eq'.
> $keyword =~ s/$keyword/$new_keyword/g;}
Your description said that you wanted to modify 'the "description"
field of base.txt' so that sould be:
$description =~ s/$keyword/$new_keyword/g;}
> }
> }
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 };
}
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;
}
__END__
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/