perl.beginners
[Top] [All Lists]

Re: array within array

Subject: Re: array within array
From: Beginner
Date: Mon, 29 Oct 2007 15:29:57 -0000
Newsgroups: perl.beginners

On 29 Oct 2007 at 8:42, 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. 
> 
>  
> 
> 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;
> 
>  
> 
> open(EXCLUDE,"exclude_bases.txt" )|| die("Could not open file!");
> 
> %exclude_bases=<EXCLUDE>;
> 
> 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);
> 
>  
> 
> #choping lines and assign variables to base.txt
> 
> foreach $base_text (%bases)
> 
> {
> 
>   chop($base_text);
> 
>   ($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;}
> 

Well here my effort. It should help you get closer to what your after 
but it's not complete. I would use hashes not arrays, you'll find 
them extremely useful for de-duping data 

I haven't run the script below, please do a perl -c first. 

Check out "exists"

perldoc -f exists



#!/usr/bin/perl

use strict;     # Well done
use warnings; # ditto.

my $exclude_bases = "exclude_bases.txt";
 
open(EXCLUDE,"excludes.txt" )|| die("Could not open file!");
while (<EXCLUDE>) {
        chomp;
        my @fields = split(/|/,$_);
        $exclude_bases{$F[0]} = 0; # $f[0] contains base_no
}
close(EXCLUDE);

open(BASE,"base.txt")|| die("Could not open file!");
while (<BASE>) {
        chomp;
        my @fields = split(/|/,$_);
        if (! exists($exlude_bases{$_}) ) {
                print "$f[2]\n";
        }
}
close(BASE);


HTH,
Dp.



<Prev in Thread] Current Thread [Next in Thread>