CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
How to include another column to check in script



Jun 8, 2016, 10:01 AM

Post #1 of 4 (2019 views)
How to include another column to check in script Can't Post

I have a script that checks for duplicates. However it does not work of course when we have 3 columns.

So my intention is to check both column 2 and column3. if the values in column 2 and 3 match, the column 1 should be printed.

For example -
if this is the case

then column 1 should not be printed.

but if this were the case, it should be printed:


columns are separated by columns

I am assuming that I would need to change this line:

my ($column_1, $column_2) = split /,/;


my ($column_1, $column_2, $column_3) = split /,/;

I tried to change the next line to

print {$FH6} "$column_1\n" if (defined $duplicates{$column_2} and defined $duplicates{$column_3});

but this did not lead to the result I needed. It looks like only column2 is being checked.

can someone help?

my %duplicates; 
while (<$FH2>) {
my ($column_1, $column_2) = split /,/;
print $column_1;
print {$FH6} "$column_1\n" if defined $duplicates{$column_2};
close $FH6;
close $FH2;
open my $fh, '<', '/tmp/tst.txt' or die "unable to open file 'file' for reading : $!";
while (my $line = <$fh>) {
print $line;
close $fh;


Jun 8, 2016, 1:07 PM

Post #2 of 4 (2004 views)
Re: [regex2012] How to include another column to check in script [In reply to] Can't Post

actually I just removed the commas from the 3rd column and I didn't need them there, so the script worked. But I would still like to know how to do it with 3 columns.


Jun 8, 2016, 1:51 PM

Post #3 of 4 (2001 views)
Re: [regex2012] How to include another column to check in script [In reply to] Can't Post

Your requirements are not at all clear. Let me guess.

  • Your input file ($FH2) is a .csv file containing three columns. (Does it have a header record?)

  • For each line, you want to print the contents of the first field to $FH6 if the contents of both the second and third fields have appeared previously in the file. (I have no idea if you care whether or not both appeared previously in the same record.) Note: The first occurrence is NOT considered a "match".

  • All code related to $f2 has nothing to do with the question.

  • Your code is close to right if you intend both the second and third field match a previous record. The key to your hash should be a concatenation of the two fields. Otherwise, you have to keep a separate hash for each field.

    UPDATE: Sorry, I overlooked your second post. You basically already found the solution I suggested. I still do not understand your requirements well enough to answer your new question.
    Good Luck,

    (This post was edited by BillKSmith on Jun 8, 2016, 1:57 PM)

    Veteran / Moderator

    Jun 8, 2016, 3:40 PM

    Post #4 of 4 (1997 views)
    Re: [regex2012] How to include another column to check in script [In reply to] Can't Post

    I agree with Bill that your requirement is not clear.

    The bottom line, however, is that your OP code is rightly populating the hash with column 2:


    but is not doing it for column 3. So, at the very least, you might need an additional line saying something like:


    But, depending on your real requirement, maybe it should be another hash for column 3, or maybe you should use a concatenation of columns 2 and 3, or maybe yet something else.


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives