CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
2 array issue

 



viddy
Novice

Jul 14, 2009, 5:47 AM

Post #1 of 3 (564 views)
2 array issue Can't Post

Hello, I am having a problem doing a simple array comparison. I know that I probably should have used a hash to do this but I would rather stick with the array approach just because I am not sure of what is wrong with it....Any how, basically I got 2 txt files that I want to compare and have a list printed out of the ones that are not in one of the lists when the script is done. The master list is the list of all possible numbers/codes and the compiled list is the ones that I have already done and want to rule out. I would like the final result to be a list of the numbers that were in the master list but not the compiled. I have posted my code. Thank you in advance. Any advice or feedback would be greatly appreciated!

#!/usr/bin/perl
use strict;
use warnings;

#open master list
my @masterCodes;
open(MASTER,"<master.txt") or die "Couldn't open file 1: $!\n";
while(<MASTER>)
{
chomp();
push(@masterCodes,"$_");
}

#open compiled list
my @compiledCodes;
open(COMPILED,"<compiled.txt") or die "Couldn't open file 2: $!\n";
while(<COMPILED>)
{
chomp();
push(@compiledCodes,"$_");
}

my $totalSize = $#masterCodes;
my $i = 0;

foreach my $code (@compiledCodes)
{
while ($i < $totalSize)
{
if ($code = $masterCodes[$i])
{
print "$code\n";
}
$i++;
}
}


viddy
Novice

Jul 14, 2009, 5:54 AM

Post #2 of 3 (562 views)
Re: [viddy] 2 array issue [In reply to] Can't Post

I actually just fixed it. I just need to work on excluding the results that I have already done. Any way in case someone ever cares:



#!/usr/bin/perl
use strict;
use warnings;

#open master list
my @masterCodes;
open(MASTER,"<master.txt") or die "Couldn't open file 1: $!\n";
while(<MASTER>)
{
chomp();
push(@masterCodes,"$_");
}

#open compiled list
my @compiledCodes;
open(COMPILED,"<compiled.txt") or die "Couldn't open file 2: $!\n";
while(<COMPILED>)
{
chomp();
push(@compiledCodes,"$_");
}

my $totalSize = $#masterCodes;


foreach my $code (@compiledCodes)
{
my $i = 0;
while ($i < $totalSize)
{
if ($code eq $masterCodes[$i])
{
print "$code\n";
}
$i++;
}
}


viddy
Novice

Jul 14, 2009, 6:19 AM

Post #3 of 3 (557 views)
Re: [viddy] 2 array issue [In reply to] Can't Post

Would someone be able to tell me how I can get the OPPOSITE of the results I am getting? With my code above I get simply another copy of the compiled list..the list of codes I have already done. What I am looking for is the codes that are NOT on the compiled list, but on only on the master list. I feel like it is rather simple or obvious but perhaps I have been staring at the code for too long....or I just suck?!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives