in reply to Searching for Certain Values

I seem to be running into a problem. Whenever I try to execute your script, I get a "Too many arguments for open at datasearch.pl line 3, near ""data.txt") "
What am I doing wrong?

My current code is pretty much what Saintly gave me:
#!/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"; } }

Replies are listed 'Best First'.
Re^2: Searching for Certain Values
by johngg (Canon) on Jul 30, 2007 at 22:14 UTC
    You are probably running an elderly version of Perl. What do you get when you run /usr/local/bin/perl -v on the command line? The three-argument form of open was introduced in Perl 5.6 according to perl56delta as were lexical filehandles (the my $fh). If you are running an earlier version then change

    open(my $fh, "<", "data.txt") || die "Can't open file: $!";

    to

    open (FH, '<data.txt') || die "Can't open file: $!";

    and

    while(my $line = <$fh>) {

    to

    while(my $line = <FH>) {

    You may want to consider upgrading your version of Perl as 5.005 is positively ancient.

    Cheers,

    JohnGG

      That ought to do it, as I am running the ancient Perl v. 5.005. Thanks! I'll get an update ASAP.
Re^2: Searching for Certain Values
by Dr.Avocado (Novice) on Jul 30, 2007 at 22:10 UTC
    BTW, here is a sample of a file I would need to search:
    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 ---------------------------------------------------------------------- +-------------------------------------