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
# 8  
Old 03-12-2009
Quote:
Originally Posted by bradleykins
bump, techies, bump
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):
Code:
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, $_; [color=green]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
  • perltidy before posting
# 9  
Old 03-12-2009
If I understand correctly:
Code:
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>;
  '

# 10  
Old 03-12-2009
Quote:
Originally Posted by pludi
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):
Code:
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;
[color=green]$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
  • perltidy before posting

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