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

Hi Perlmonks. I am new to Perl and I'm a little bit confuse about the error given when I compile my prog.

I have a prog that handle two kinds of input.
1.If number of argument passed=3 eg perl file.pl input.db output
It will check the input and search for age in the record and print it to the output file.
2.If number of argument passed=4 eg perl file.pl input.db tom output
It will check the input file, search people whose name tom and print the age to the output.

I have no problem with the search module.
But it's the problem with argv for the 2nd input. Every time I compile using perl prog.pl input.db tom output it always give error like:
Can't open tom:no such file or directory.
The same thing also happened if I use "tom" instead. What's wrong with my program?

Well,in my program I use while(<>) purposely to read other part of the module.
Here I include part of my working program.

#!/usr/bin/perl use strict; my $counter=1; my $no='D000001'; my $name=''; #to save the name to be searched my $result; my $flag=0; ###checking the argument if($#ARGV==1){ print "First type of search\n"; print "input: $ARGV[0]\n"; print "output: $ARGV[1]\n"; $result=">".$ARGV[1]; #get file for output $flag=1; check(); } elsif($#ARGV==2){ $name=$ARGV[1]; print "Second type of search\n"; print "input: $ARGV[0]\n"; print "name : $name\n"; print "output: $ARGV[2]\n"; $result=">".$ARGV[2]; #get file for output $flag=2; check(); } else { print "Invalid number of argument\n";} + sub check{ # Read an entire record at a time $/ = "//\n"; # each record separated by //\n open(OUT,"$result") or die "Can't open $result."; #open result for +output while (<>){ # Read the entry print "$no\n"; if($flag==1){ ###print "$_"; if(/AGE\s*(\d*)/){ print OUT "RECORD NO\t$no\n"; print OUT "AGE\t$1\n";}} if ($flag==2){ search($name);} $no++; } close(OUT); } sub search{ print "search done\n"; ##do some searching and other things ##I didn't include the function ##since actually the code is very long, and it doesn't only do sim +ple search }
This is part of my input files:
NAME Tom AGE 21 // NAME Janice AGE 34 // NAME Clarice Age 45 //
Thank you in advanced.
Regards,

gdnew

Replies are listed 'Best First'.
Re: about while() combine with ARGV
by japhy (Canon) on Feb 19, 2002 at 08:49 UTC
    First off, you might want to use @ARGV == 2 and @ARGV == 3 instead of that nasty $#ARGV business. It's "cleaner" and it is easier to understand in my opinion.

    Anyway, the problem is that you're not removing the elements from @ARGV, and so the while loop is trying to read them as files.

    if (@ARGV == 2) { $result = ">" . pop(@ARGV); # removes element from @ARGV # ... } elsif (@ARGV == 3) { $result = ">" . pop(@ARGV); $name = pop(@ARGV); # ... }

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      I'm a big fan of using shift for taking command line arguments. This way, they get assingned in the order that they were passed in. Just one less thing to debug.

      thor
        Except that since the first element of @ARGV is the one he wants to keep, he'd have to then put it back somehow.

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      I never thought about that.
      Thanks..