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

In reply to about while() combine with ARGV by gdnew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.