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:
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.
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.
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.
Rule#4: "Do not 'bump up' questions if they are not answered promptly."
If you bump up again, the only help you'll get from me from now on will be a rot13, uuencode-d patch file without any explanations.
That said, I took a look at your program, here are the changes (based on what you posted, comments in green):
use Getopt::Long;
my @ARRAY = ();
my $currentLetter = undef;
my $ARRAYlength = -1;
my $help = 0;
my $FILEname = "C:\\searching4theseletters.txt"; you have to escape a literal backslash in double quotes
my $lettersfile = "C:\\inputletters.txt"; same
open FILE, $FILEname or die "couldn't open $FILEname: $!";
while (<FILE>) {
chomp $_;
push @ARRAY, $_; Gets rid of that nasty \n at the end of the line
}
close FILE or die "problems closing $FILEname: $!";
my $linenumber = 0;
$ARRAYlength = scalar @ARRAY; Mostly for correctness
#print " $ARRAYlength";
#print(@ARRAY);
open FILE, $lettersfile or die "couldn't open $lettersfile: $!";
while ( my $line = <FILE> ) { Saves you one line
chomp($line);
my $originalline = $line;
$line =~ s!^\"!!;
$line =~ s!\"$!!;
$line =~ s!\"\"!\"!g;
$linenumber++;
for ( my $x = 0 ; $x < $ARRAYlength + 1 ; $x++ ) { Initialize your variables
$currentLetter = $ARRAY[$x]; If you want a scalar, use $ (Array = @, Element of an array = $)
if ( $line =~ /$currentLetter/ ) {
#print ($currentLetter);
print($line);
print "\n";
last;
}
#next; Not needed, end of the loop anyways
}
}
close FILE or die "problems closing $lettersfile: $!";
exit 0;
Things you might want to consider next time:
use strict;
use warnings;
use diagnostics;
use Data:: Dumper; to examine your structures
calling perl -c on your file to check for syntax correctness
perl -e'
open SH, "searching4theseletters.txt" or die "$!\n";
open IH, "inputletters.txt" or die "$!\n";
@sa = <SH>;
@h{@sa} = (1) x @sa;
print grep $h{$_}, <IH>;
'
Thank you very much for this very insightful response, and very well formatted, i appreciate your thoroughness and your preperation in helping assist in my understanding, you make an excellent teacher.
On another note, what then is the process to get an answer should my post be completely ignored?
Should i give up?
I have now read the forum rules, and shall adhere when i visit.
I had not intended on coming back, but since this very usefull answer i can see that there are usefull people here.