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};
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.