2015_newbie has asked for the wisdom of the Perl Monks concerning the following question:

I had tried to post this as a response to a write up by someone, but that didn't work (permission denied) so I posted it as a new question. Here are two files. What I am trying to do is get the script to print out /vol/cat,feline as a match when it finds that there is an identical line in the second.txt. So there is /vol/cat,feline in both files, and I want that line to be printed. After that, I want the script to also print the # by the /vol/cat,feline -like this match: /vol/cat,feline match: record 10 Because 10 is the # associated with the volume /vol/cat/,feline in the second file. The script I created does print out /vol/cat,feline so long as the numbers column is removed from the second file. But because the second.txt has a separate column for numbers, the script does not see /vol/cat,feline as being a match and does not print it out. I need to know how to make it look at only the second and third columns of the second.txt (delimited by commas)and compare them to the first and second columns of first.txt.
first.txt /vol/cat,feline /vol/dog,canine /vol/cat,feline /vol/cat,feline /vol/amphibian,FROG /vol/amphibian,FROG
second.txt 9,/vol/elephant,fourfeet 1999,/vol/dolphin,fish 10,/vol/cat,feline 1111,/vol/goldfish,fish 2222,/vol/spider,arachnid 5555,/vol/camel,dromedary <code> #!/usr/bin/perl use strict; use warnings; sub get_file { open my $FILE, '<', shift or die $!; return map {$_ => $_} <$FILE>; return map {$_->[0] => $_->[1]} <$FILE>; } my %a = get_file '/tmp/first.txt'; my %b = get_file '/tmp/second.txt'; { no warnings 'uninitialized'; print "$_\n" for grep {$_} @a{keys %b}; }

Replies are listed 'Best First'.
Re: comparing 2 columns
by GotToBTru (Prior) on Jan 04, 2016 at 15:07 UTC

    Your second return statement in get_file never gets executed. It returns control to the main program at that point.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: comparing 2 columns
by kennethk (Abbot) on Jan 04, 2016 at 16:43 UTC
    It appears you have CSV, so you may consider using Text::CSV to do data import.

    You never actually separate your columns. If a parsing module is heavier-weight than you want, you probably want to do a split on commas

    my @fields = split /,/, $line;

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: comparing 2 columns
by Corion (Patriarch) on Jan 04, 2016 at 15:07 UTC

    If your files have a different format, why do you read them in through the same subroutine?

    Maybe you want to read perlfaq4 on "difference" ? Reading that should give you an idea of what the last line of your script does. After knowing what the last line does, you should be able to modify get_file or the data structure in %a and %b accordingly.

Re: comparing 2 columns
by kikuchiyo (Hermit) on Jan 04, 2016 at 19:52 UTC

    This should do what you want:

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1151834 use strict; use warnings; open my $FH1, '<', shift or die $!; my %keys; while (<$FH1>) { chomp; $keys{$_}++; } close $FH1; open my $FH2, '<', shift or die $!; while (<$FH2>) { chomp; my ($record, $key) = split ',', $_, 2; print "match: $key record: $record\n" if exists $keys{$key}; } close $FH2;

    Your code looks like something you've copied from somewhere without really understanding it. I'd advise against doing that: cargo cult code like that might be inefficient, might not actually do what you want, or it might even do damage to your system or data without you knowing it.

      It works just great! Thanks so much! I tried to see how it works and it appears that in this line: print "match: $key record: $record\n" if exists $keys{$key}; $key holds the value of the second and third columns and record holds the first column. what is this: my ($record, $key) = split ',', $_, 2; the split is for the comma but what is the 2? Is that for 2 columns? Not sure -can you explain? I have been reading Alvin Alexander tutorial on hash but I am still green.
        ... split ... what is the 2?

        Please see split documentation,  LIMIT in particular.


        Give a man a fish:  <%-{-{-{-<