PERL - copy fiel contents to array then compare against other file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL - copy fiel contents to array then compare against other file
# 1  
Old 03-11-2009
PERL - copy fiel contents to array then compare against other file

Basically to illuminate i want to take a file with mutliple lines,

C:\searching4theseletters.txt
a
b
c

Read this into an array

@ARRAY

and then use this to compare against another file

C:\inputletters.txt
b
o
a
c
n
a
j

My hope is to then be able to take the one found and print it out to the screen as a match.

I am currently using this:

Code:
 
use Getopt::Long;
 
my @ARRAY = ();
my $currentLetter= undef;
my $ARRAYlength = -1;
my $help = 0;
my FILEname="C:\searching4theseletters.txt";
my lettersfile ="C:\inputletters.txt";
 
open FILE, $FILEname or die "couldn't open $FILEname: $!";
while (<FILE>)
{
@ARRAY = <FILE>;
}
close FILE or die "problems closing $FILEname: $!";
my $linenumber = 0;
$ARRAYlength = @ARRAY;
#print " $ARRAYlength";
#print(@ARRAY);
open FILE, $lettersfile or die "couldn't open $lettersfile: $!";
while (<FILE>)
{
my $line = $_;
chomp($line);
my $originalline = $line;
$line =~ s!^\"!!;
$line =~ s!\"$!!;
$line =~ s!\"\"!\"!g;
$linenumber++;
for(my $x;$x < $ARRAYlength+1;$x++)
{
$currentLetter=@ARRAY[$x];

if ($line =~ /$currentLetter/)
{ 
#print ($currentLetter); 
print ($line);
print "\n";
last;
}
}
next;
}
close FILE or die "problems closing $lettersfile: $!";
exit 0;

The problem is that for some reason when reading in my text file it puts alot of extra lines in the file, if this wasnt bad enough currentLetters doesnt seem to get populated, and it prints every line out it encounters.

Any thoughts?

Nik
# 2  
Old 03-11-2009
I have also tried to use

Code:
$currentLetter = pop(@ARRAY);

with no success either, i have also used

Code:
push(@ARRAY,$_);

# 3  
Old 03-11-2009
anybody?
# 4  
Old 03-11-2009
this is wrong:

open FILE, $FILEname or die "couldn't open $FILEname: $!";
while (<FILE>)
{
@ARRAY = <FILE>;
}

if you want to read the file into an array you would do it like this:

open FILE, $FILEname or die "couldn't open $FILEname: $!";
@ARRAY = <FILE>;
close FILE;

The problem I have with trying to help anymore is that these types of tasks are always data sensitive, and your sample data does not appear to be your real data. Post some of the real data and describe better what you need to match/find.

And be patient, bumping your thread after an hour is not appropriate. This is a help forum, not tech support.
# 5  
Old 03-11-2009
The test data is indicative of what i am looking for alphanumeric data, contained within a CSV, you can create sample datat by making it 16 digits of random data as long as its a-Z or 0-9

This is what i am looking for.

Apologies for not being patient, just been searching through numerous threads on here for some time before i actually posted and its now kinda defunct i manually went about the problem in another way.

yet i still wish to finish this.
# 6  
Old 03-12-2009
bump, techies, bump
# 7  
Old 03-12-2009
Please follow the forum rules!
Do not 'bump up' questions if they are not answered promptly!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl: array name tha contains contents of a variable

Hi there I have a counter called my $counter = 0; I am trying to build an array that will have a name that is for example my @array0 = ("some", "stuff"); but instead of hard coding the "0" in the array name i want to use whatever value the aforementioned $counter has in it...so ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

2. Shell Programming and Scripting

Compare file to array, replace with corresponding second array

ok, so here is the issue, I have 2 arrays. I need to be able to create a loop that will find ${ARRAY1 in the text doc, and replace it with ${ARRAY2 then write the results. I already have that working. The problem is, I need it to do that same result across however many items are in the 2... (2 Replies)
Discussion started by: gentlefury
2 Replies

3. Shell Programming and Scripting

compare two value in array - Perl

Hi, I just wondering if I want to compare previous value to next value if it same count + 1 if not doesn't count how do we compare in Perl? Thank (2 Replies)
Discussion started by: guidely
2 Replies

4. Shell Programming and Scripting

Perl question - How do I print contents of an array on a single line?

I have the following code: print @testarray; which returns: 8 8 8 9 How do I return the array like this: The output is: 8, 8, 8, 9 (5 Replies)
Discussion started by: streetfighter2
5 Replies

5. Shell Programming and Scripting

Need help to copy contents of a file

Hi, I am stuck up with a problem of copying the contents of a directory where one of the folder name is changed daily. Problem: I have the folder structure as: RefWorlds2/LINGCC4_X64/odsdev/odessy/UTI/621GA_build_xxx/.../.. In the above path the build number (xxx) will be changed... (3 Replies)
Discussion started by: SathaKarni
3 Replies

6. Shell Programming and Scripting

Perl help: Creating a multidimensional array of subdirectories and its contents

I'm currently working with dozens of FASTA files, and I'm tired of having to manually change the filename in my Perl script. I'm trying to write a simple Perl script that'll create a 2-dimensional array containing the name of the folders and its contents. For example, I would like the output... (6 Replies)
Discussion started by: shwang3
6 Replies

7. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

8. Shell Programming and Scripting

Perl script to copy contents of a web page

Hi All, Sorry to ask this question and i am not sure whether it is possible. please reply to my question. Thanks in advance. I need a perl script ( or any linux compatible scripts ) to copy the graphical contents of the webpage to a word pad. Say for example, i have a documentation site... (10 Replies)
Discussion started by: anand.linux1984
10 Replies

9. Shell Programming and Scripting

How to match all array contents and display all highest matched sentences in perl?

Hi, I have an array with 3 words in it and i have to match all the array contents and display the exact matched sentence i.e all 3 words should match with the sentence. Here are sentences. $arr1="Our data suggests that epithelial shape and growth control are unequally affected depending... (5 Replies)
Discussion started by: vanitham
5 Replies

10. UNIX for Dummies Questions & Answers

compare array contents with file

I have an array "arrA" with the following contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf My data file "Files.dat" has the same contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf I have a script that compares... (0 Replies)
Discussion started by: orahi001
0 Replies
Login or Register to Ask a Question