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

Hey Monks, I have a delim-tab file
ID NAME FAMILYTAG EFFECT 001 John Black Positive 002 Kate Rhodes, Mich Positive 003 Aaron Sunders Negative 004 Shirley Rhodes Negative 005 Dexter Sunders Positive
I want to input this file(which is much larger) and read in a name for eg: Kate. I want the script to read Kate, recognize it's family tag i.e it contains "Rhodes" and then output the other family member i.e Shirley. Is there a way to do this? The output file will look like this
Kate Rhodes Shirley Rhodes
I was thinking maybe some sort of pattern match might work for such a script? Am I thinking in the right direction?

Replies are listed 'Best First'.
Re: read a name, recognise a tag and output
by toolic (Bishop) on Apr 26, 2012 at 16:56 UTC
    Here's one way to get the output you want, given your input...
    use warnings; use strict; my %names; while (<DATA>) { next if /^ID/; my ($first, $last) = (split)[1 .. 2]; $last =~ s/,//; push @{ $names{$last} }, $first; } print "$_ Rhodes\n" for @{ $names{Rhodes} }; __DATA__ ID NAME FAMILYTAG EFFECT 001 John Black Positive 002 Kate Rhodes, Mich Positive 003 Aaron Sunders Negative 004 Shirley Rhodes Negative 005 Dexter Sunders Positive

    See also:

      Thanks toolic..let me try if it works with the larger .txt file!
        Is there a way to input Kate first rather than just inputting Rhodes?
Re: read a name, recognise a tag and output
by ww (Archbishop) on Apr 26, 2012 at 16:34 UTC

    That's probably possible -- but as is the case with many things that can be done with regexen but which are outside its core utility, it's apt to be awkward, verbose and perhaps fragile.

    Better, perhaps, you may wish to study up on the various CSV modules and the many compound data structures available to you -- Array of Arrays; Hash of Arrays, etc. Either approach should work well.

    If you Super Search with the terms seen unique you'll find some quite helpful nodes.

    PS: Your problem statement implies that you're prepared to abandon the content of the "Effect" column. If that's not so, you'll want to make that clear with an update to your OP.

    Update: tweaked first sentence for clarity.

      Yes, the only columns I need it to output is name and the tag. Effects column can be abandoned.
Re: read a name, recognise a tag and output
by RichardK (Parson) on Apr 26, 2012 at 16:36 UTC

    This is perl so there's lots of ways to do it :)

    You'll learn much more by experimenting, so just have a go and see what happens.

    If you need some hints try perlintro and perlfaq

    If you get stuck then ask for some help on a specific problem.