Sponsored Content
Top Forums Shell Programming and Scripting PERL - copy fiel contents to array then compare against other file Post 302296471 by bradleykins on Wednesday 11th of March 2009 06:59:53 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
WC(1)									FSF								     WC(1)

NAME
wc - print the number of bytes, words, and lines in files SYNOPSIS
wc [OPTION]... [FILE]... DESCRIPTION
Print byte, word, and newline counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard input. -c, --bytes print the byte counts -m, --chars print the character counts -l, --lines print the newline counts -L, --max-line-length print the length of the longest line -w, --words print the word counts --help display this help and exit --version output version information and exit AUTHOR
Written by Paul Rubin and David MacKenzie. REPORTING BUGS
Report bugs to <bug-coreutils@gnu.org>. COPYRIGHT
Copyright (C) 2002 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICU- LAR PURPOSE. SEE ALSO
The full documentation for wc is maintained as a Texinfo manual. If the info and wc programs are properly installed at your site, the com- mand info wc should give you access to the complete manual. wc (coreutils) 4.5.3 February 2003 WC(1)
All times are GMT -4. The time now is 04:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy