Dermot Paikkos wrote:
> Hi,
> 
> I am a bit stuck with references and was hoping someone could help me 
> out.
> 
> In File1 I need to isolate the last number in the column and see if 
> it exists in file2. If so, I would like to add the content of the 
> line from file1 and print it out. There may be other ways of doing 
> this but I wanted to try to do it with references as this is my 
> current weak spot :-(
> 
> Below is some sample data and my effort. What is happening with my 
> current attempt is that it is simply looping through the 2nd file and 
> printing the last assignment to the hashref. So I guess the problem 
> is with the way I am creating the hashref rather then dereferencing 
> it. 
>

You are not creating a list of lists. You only have a single hashref
which is going to be set to the last line of the first file *always*.

> I am sure this is a simple problem. Can anyone offer a bit of 
> assistance . I promise will do some more reading tonight.
> 
> Thanx.
> Dp.
> 
> 
> 
> ======== my effort.pl ==========
> #!/bin/perl
> 
> 
> use strict;
> use warnings;
> 

Excellent start!

> my $file = "file1.txt";
> my $file2 = "file2.txt";

Single quotes are fine above.

> my $in = 0;
> my $found = 0;
> my $hashref;
> 

Generally it is preferred to declare variables when we first use them
rather than in a list at the top of the program.

> open(FH,$file) or die "Can't open $file:$!\n";
> 

Nice, parens aren't needed around open above.

> while (<FH>) {
>       chomp;
>       my @f = split(/\t/,);
>       next if ($f[3] =~ /NULL/i);
>       if ($f[3] =~ /(p|s)\d{4}/i){
>               ++$in;
>               my $num = uc($f[3]);
>               my $rest = "$f[0]"."\t"."$f[1]"."\t"."$f[2]"."\t";

Are you actually meaning to assign $rest into the hashref below? And in
either case the above can be written much more succinctly as,

my $rest = "$f[0]\t$f[1]\t$f[2]\t";

Or using a join and array slice,

my $rest = join "\t", @f[0..2];

> #             print "$num\n";
>               $hashref = {
>                       'number' => $num,
>                       'rest' =>  $_,
>               };

Right here you are continually clobbering $hashref. You need to be
assigning to a list of hashes, for instance,

$matches->{$num} = { 'number' => $num, 'rest' => $rest };

Assuming you want duplicates clobbered as well.

>       }                                       
> }
> close(FH);
> 
> 
> open FH2, $file2 or die "Can't open $file2:$!\n";
> while (<FH2>) {
>       chomp;
>       next if ($_ !~ /(P|S)\d{4}/);
>       (my $num) = ($_ =~ /\s(\w\d{4})/);
>       print "Comparing $num with $hashref->{'number'}\n";
>       if (exists($hashref->{'number'}) ) {
> #             print "$hashref->{rest} $hashref->{'number'}\n";
>               ++$found;
>       }
> }
> close(FH2);
> 
> print "Done. Found $found / $in\n\n";
> 
> 

I think what might help you the most is to improve your variable naming
conventions. If you named your variables something other than 'hashref'
for instance you might have caught the problems yourself. The first
indication of the problem is that you have nothing named "list_of" or
"matched_numbers" or "part_numbers" or "temperatures", etc.  Using
generic names like file1/file2, hashref, f, num, etc. give no context
about what the program is working on or does. When I see 'rest' I think,

sleep 360;

HTH,

http://danconia.org

[snip samples]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to