Dr.Avocado has asked for the wisdom of the Perl Monks concerning the following question:

Dear more experienced Perl Monks,

Sorry about the confusion: I've added the code I'm using so you can better see what I'm trying to do. I've been working on a script that searches a data sheet for certain values and prints values from rows that match a certain description.
Here's an example of a sheet I'm searching:
Score | Points | Time | Points | Record | Size | Points | Age | Points + | Difficulty | Size | Points | Name 4 |15 |356 |17 |45 |14 |45 |24 |12 + |3 |1 |34 |team A 6 |24 |354 |45 |345 |53 |25 |47 |34 + |3 |3 |45 |team B 3 |18 |303 |34 |234 |32 |48 |67 |32 + |23 |4 |22 |team C 7 |13 |322 |26 |33 |56 |57 |46 |23 + |3 |1 |14 |team D 5 |10 |353 |24 |58 |82 |35 |33 |12 + |5 |2 |35 |team E 5 |30 |264 |48 |26 |23 |23 |73 |23 + |5 |2 |65 |team F 6 |18 |363 |58* |39 |71 |35 |75 |46 + |2 |4 |23* |team_triumph ---------------------------------------------------------------------- +------------------------------------- x |x |x |x |x |x |x |x |x + |x |x |x |Total ---------------------------------------------------------------------- +------------------------------------- Score | Points | Time | Points | Record | Size | Points | Age | Points + | Difficulty | Size | Points | Name 2 |32 |443 |34 |464 |38 |89 |9 |43 + |3 |4 |353 |Team C 5 |24 |343 |543 |923 |478 |0 |35 |3 + |3 |2 |39 |Team B 6 |5 |263 |232 |92 |43 |48 |96 |46 + |4 |52 |78 |team_victory ---------------------------------------------------------------------- +------------------------------------- x |x |x |x |x |x |x |x |x + |x |x |x |Total ---------------------------------------------------------------------- +------------------------------------- Score | Points | Time | Points | Record | Size | Points | Age | Points + | Difficulty | Size | Points | Name 5 |76 |366 |37 |593 |453 |34 |68 |65 + |35 |4 |54 |Team D 3 |34 |235 |102 |967 |290 |2 |54 |2 + |3 |6 |3 |Team C 2 |643 |643 |34 |291 |10 |2 |43 |53 + |3 |7 |46 |Team F 5 |43 |362 |2 |152 |35 |35 |24 |5 + |2 |43 |7 |Team G 6 |7 |643 |6* |45 |0 |97 |75 |883 + |1 |2 |344* |team_intrepid ---------------------------------------------------------------------- +------------------------------------- x |x |x |x |x |x |x |x |x + |x |x |x |Total ---------------------------------------------------------------------- +-------------------------------------
I've been searching column "Name" for the values I want and using those rows by using
#!/usr/local/bin/perl open(my $fh, "<", "data.txt") || die "Can't open file: $!"; # Run through all lines of the file, one by one while(my $line = <$fh>) { # Break up the line on whitespace, assign columns to vars my( $score,$scorePoints, $time,$timePoints, $record,$recordPoints, $size,$sizePoints, $age,$agePoints, $diff,$diffPoints, $size2,$size2Points, $name ) = split(/\s+/,$line,13); # Check to see if name matches if($name =~ /(intrepid|triumph)/) { print "$name\n", "Time: $timePoints, Difficulty: $diffPoints\n\n"; } }

Is there a more flexible way of checking for other strings in addition to "intrepid" and "triumph", perhaps using an array and seeing if the Name column contains any values in the array? My only question is, how do I do that? And could I store these values in a separate configuration file so a user could easily change them?
Also, would there be any way of referencing the value that was discovered, i.e. "triumph", "victory", "intrepid", or any other value being searched for if a string containing that value is found?
Thanks for any help. If you wish to see a copy of the actual code I'm using right now, I received help on it in a previous node: http://perlmonks.org/index.pl?node_id=629617.

Replies are listed 'Best First'.
Re: Searching for Numerous Values
by moritz (Cardinal) on Aug 01, 2007 at 17:16 UTC
    BTW if you want to parse your sheets, take a look at unpack, that's good for parsing fixed width tables.
Re: Searching for Numerous Values
by bkchapin (Acolyte) on Aug 01, 2007 at 19:51 UTC
    Maybe a hash would work?
    my %names = ( 'intrepid' => 1, 'triumph' => 2, ); while (my $line = <$fh>) { ... if ($names{$name}) { print "$name\n", ... } }
Re: Searching for Numerous Values
by ikegami (Patriarch) on Aug 01, 2007 at 17:02 UTC
    If I understand correctly, you want to quickly eliminate the rows in which you are not interested?
    while (<$fh>) { chomp; my ($name) = /\|team_(triumph|victory)\z/ or next; ... }

    The above skips lines that aren't for interesting teams, and places the name of the team ('triumph' or 'victory') in $name.

      No, I guess I misunderstood.
      my @teams = ( "triumph", "victory", "intrepid" ); my $re = join '|', map quotemeta, @teams; while (<$fh>) { chomp; my ($name) = /\|team_($re)\z/ or next; ... }