Computing.Net > Forums > Programming > Perl, compare lines in an array

Perl, compare lines in an array

Reply to Message Icon

Original Message
Name: Shr0Om
Date: September 20, 2006 at 06:56:32 Pacific
Subject: Perl, compare lines in an array
OS: Win XP
CPU/Ram: amd 64 3200
Model/Manufacturer: custom
Comment:

I just started learning Perl, and i was hoping someone could tell me how to:

I have read a file into an array, and sorted it.
I want to read line1, then compare it with line2. If match, create ErrorLog.txt & print "duplicate found on line#" to the file. Then compare line 2 with line3 and so on..

Also, instead of specifying what file i should read into the array, is it possible just to take the first avaible txt file?
In batch i would just make a list of txt files (already sorted), and the first txt file would be read.

And FINALLY, im looking for a pause function. Sleep wont do what i want.
For now i just created a
$junk = <STDIN>;
but i want the user to be able to press Any key to continue, not just enter.

Any help appreciated:)


Report Offensive Message For Removal


Response Number 1
Name: FishMonger
Date: September 20, 2006 at 08:53:09 Pacific
Subject: Perl, compare lines in an array
Reply: (edit)

I'm just leaving for work so I'll be brief and add more detail latter.

For comparing the lines and search for duplicates, it's easier to use a hash instead of the array.

There are several ways to readin a list of files in sorted order. These are the 2 most common.

1. use the opendir and readdir functions
2. use the < > diamond operator

Use the TermReadKey module
http://search.cpan.org/~jstowe/Term...


Report Offensive Follow Up For Removal

Response Number 2
Name: Shr0Om
Date: September 20, 2006 at 12:24:22 Pacific
Subject: Perl, compare lines in an array
Reply: (edit)

FishMonger: I see now hash would be much easier to work with. I'll have a look at it tomorrow. Thnx for the tip:)


Report Offensive Follow Up For Removal

Response Number 3
Name: FishMonger
Date: September 20, 2006 at 21:04:22 Pacific
Subject: Perl, compare lines in an array
Reply: (edit)

Shr0Om,

Posting code in this forum is a joke, so if you need additional help, you can email me your script and I'll look it over and give a few suggestions.


Report Offensive Follow Up For Removal

Response Number 4
Name: Shr0Om
Date: September 21, 2006 at 00:19:38 Pacific
Subject: Perl, compare lines in an array
Reply: (edit)

FishMonger: Thnx mate:)
I still havent started writing the script yet. Just been practising writing small snippets on how to read a file into a hash etc..

I guess an idea would be to index the data.
Like, collmn A is the data from the file, and collumn B would be a counted index. Like,

$Filedata,$count++

"198374;1
183984;2
174874;3
143523..."

then compare $Filedata from index 1 with $Filedata at index 2.. Then compare $Filedata from index 2 with $Filedta from index 3 etc.. Hope you know what i mean? :]
If you do, i was hoping you could paste a small code snippet on howtodo just to get me started?

As for the "open first avaible txt file" i guess this could work:
$File = (`dir /b *.txt`);

How do i ONLY take the first entry in $File so i this would work?:

open (LIST1, $File) || die "File not found\n";



Report Offensive Follow Up For Removal

Response Number 5
Name: Shr0Om
Date: September 21, 2006 at 00:26:54 Pacific
Subject: Perl, compare lines in an array
Reply: (edit)

As for the "open first avaible txt file" i found the solution.

@File = (`dir /b *.txt`);
open( FILE, "< $File[0]" ) or die "Can't open $filename : $!";


Report Offensive Follow Up For Removal


Response Number 6
Name: FishMonger
Date: September 21, 2006 at 09:44:40 Pacific
Subject: Perl, compare lines in an array
Reply: (edit)

Do you want to proccess the 1st file or do you want to loop through each of the .txt files in the directory?

If you only want the 1st file, then you could do this.

my $file = <*.txt>;
open(FILE, $file) || die "Can't open $file: $!";

If you want to loop through the files.

while ( my $file = <*.txt> ) {
open(FILE, $file) || die "Can't open $file: $!";

# process the file

close FILE;
}


Report Offensive Follow Up For Removal

Response Number 7
Name: FishMonger
Date: September 21, 2006 at 09:51:50 Pacific
Subject: Perl, compare lines in an array
Reply: (edit)

This example will need a little tweeking to get your desired output, but it's close.

while ( my $file = <*.txt> ) {
my %lines;
open(FILE, $file) || die $!;

while (<FILE>) {
chomp;
$lines{$_}++; # build the hash
}

foreach my $line (keys %lines) {
if ($lines{$line} > 1) {
print "$file line duplicated:$line\n";
}
}

}


Report Offensive Follow Up For Removal

Response Number 8
Name: Shr0Om
Date: September 21, 2006 at 11:18:28 Pacific
Subject: Perl, compare lines in an array
Reply: (edit)

Thnx FishMonger! I'll have a look at it tomorrow :D


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Perl, compare lines in an array

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge