On Oct 29, 6:42 am, [EMAIL PROTECTED] (Mike Tran) wrote:
> Hey all,
>
> 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.
>
> 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.
This sounds like a homework assignment, so I won't provide a complete
solution, but I will give a few pointers and maybe a little code.
>
> 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
Your description of your required output doesn't say what you want to
do with the 4th field in the exclude_bases.txt.
>
> #!/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 don't really need most of those and the following global vars.
You should define the vars in the smallest scope that they require.
>
> open(EXCLUDE,"exclude_bases.txt" )|| die("Could not open file!");
Why didn't you use the $exclude_bases var that you previously defined?
You should use the 3 arg form of open and include $! in the die
statement which will tell you why it failed.
>
> %exclude_bases=<EXCLUDE>;
Why are you slurping the data into a hash?  If you use the
Data::Dumper module to output that hash, I think you'll find that it's
not in the format that you wanted.
>
> 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!");
>
> %bases=<BASE>;
>
> close(BASE);
See my previous comments.
>
> #choping lines and assign variables to base.txt
>
> foreach $base_text (%bases)
>
> {
>
>   chop($base_text);
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);
>
>   ($exbase_no,$keyword,$new_keyword)=split(/\|/,$base_text);
>
>   if ($exbase_no=$base_no) {
>
>   $keyword =~ s/$keyword/$new_keyword/g;}
>
> }
> }
>
> ~

Here's how I'd load the hash.

my %base;

open (my $base, '<', 'base.txt') || die "base.txt <$!>";
while (<$base>) {
      next if /^\s*$/;
      chomp;
      my @fields = split /\|/;
      $base{$fields[0]} = [EMAIL PROTECTED];
}
close $base;


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to