Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Reply to replies and update

by Maze (Sexton)
on Aug 09, 2006 at 22:48 UTC ( [id://566505]=note: print w/replies, xml ) Need Help??


in reply to Confused about 'Name "main::whenfound" used only once:'

hm - right, i fix up the indentations
use the appropriate things for finding values
put the declarations in the correct 'scope'
use strict
and er... stop the braindeaded use of qw...

and so with various other edits, this is what i've come up with

#!/usr/bin/perl use strict; use warnings; sub argscan{ my @subarg = ($_[0],$_[1],$_[2]); my $argcount; my $found; my $notfound; my @whenfound; if (@ARGV == 1) { return 0; } elsif ($subarg[0] = undef) { $argcount = @ARGV; return $argcount; } else { while ($argcount <= @ARGV) { if ($ARGV[$argcount] == $subarg[0]){ $found++; $whenfound[$argcount] = 1; } else { $notfound++; $whenfound[$argcount] = 0; } $argcount++; } if ($subarg[1] eq "foundcount") { if (defined($found)) { return $found } } elsif ($subarg[1] eq "notfoundcount") { if (defined($notfound)) { return $notfound } } elsif ($subarg[1] eq "huntfor") { if (defined($found)) { return $whenfound[$subarg[2]] } } else {return 0 } } } my $argnumber = argscan(); my $carnumber = argscan(qw(car foundcount)); my $notcarnumber = argscan(qw(car notfoundcount)); my $bunnynumber = argscan(qw(bunny foundcount)); print "there are $argnumber arguments \n there are $carnumber cars there are $bunnynumber bunnies $notcarnumber args are not cars \n ";

which executes and produces the output:
there are 0 arguments

        there are 0 cars
        there are 0 bunnies
        0 args are not cars
when I execute it with any given argument

and gives this output when I execute it with any arguments:
Use of uninitialized value in numeric le (<=) at testargscan.pl line 18.
Use of uninitialized value in array element at testargscan.pl line 19.
Use of uninitialized value in numeric eq (==) at testargscan.pl line 19.
Use of uninitialized value in numeric eq (==) at testargscan.pl line 19.
Use of uninitialized value in array element at testargscan.pl line 21.
Use of uninitialized value in string eq at testargscan.pl line 29.
Use of uninitialized value in string eq at testargscan.pl line 29.
Use of uninitialized value in string eq at testargscan.pl line 29.
Use of uninitialized value in numeric le (<=) at testargscan.pl line 18.
Use of uninitialized value in array element at testargscan.pl line 19.
Use of uninitialized value in numeric eq (==) at testargscan.pl line 19.
Use of uninitialized value in numeric eq (==) at testargscan.pl line 19.
Use of uninitialized value in array element at testargscan.pl line 21.
Use of uninitialized value in numeric le (<=) at testargscan.pl line 18.
Use of uninitialized value in array element at testargscan.pl line 19.
Use of uninitialized value in numeric eq (==) at testargscan.pl line 19.
Use of uninitialized value in numeric eq (==) at testargscan.pl line 19.
Use of uninitialized value in array element at testargscan.pl line 21.
Use of uninitialized value in numeric le (<=) at testargscan.pl line 18.
Use of uninitialized value in array element at testargscan.pl line 19.
Use of uninitialized value in numeric eq (==) at testargscan.pl line 19.
Use of uninitialized value in numeric eq (==) at testargscan.pl line 19.
Use of uninitialized value in array element at testargscan.pl line 21.
there are 0 arguments

        there are 1 cars
        there are 1 bunnies
         args are not cars
which isn't the bahaviour i'm looking for

what I am looking for is a simple test of a subroutine, which I'm trying to use in a larger program for XML handling, cycles through the argument array looking for the values specified when I call it (in this case - cars and bunnies, just because I could) and returning what I ask for.

so should be pretty easy, except I must be doing something fundamentally wrong
thanks for peoples help, and as for tutorials, i've only had a breif look at llama book and read some source code, so i'm not surprised I'm making careless mistakes
i'll get back to it

Replies are listed 'Best First'.
Re: Reply to replies and update
by bigmacbear (Monk) on Aug 10, 2006 at 02:18 UTC

    It appears you are trying to make one subroutine do the work of several, and distinguishing which by the number and content of your "subargs" (which I will call "parameters" to your sub, to distinguish them from the command line arguments in @ARGV). This one sub does five different things:

    • With no parameters passed, counts the number of arguments passed on the command line;
    • Otherwise, takes its first parameter and compares it against each command line argument, keeping a count of the number of matches and nonmatches;
    • If the second parameter matches a given string, returns the count of matches determined above;
    • If the second parameter matches a different string, returns the count of nonmatches given above; and
    • If the second parameter matches yet something else, tries to do something I can't figure out. Since this is never tested by the test cases given, I'm going to ignore this case entirely and leave it to you and to the other monks.

    I would structure this monster as a number of different subroutines. Basically, get rid of the second parameter to your catch-all sub; then write one subroutine to do each of the five tasks above, using the sub's name to convey the same information. The first sub, however, is probably better written right into your mainline code, as it operates on the global @ARGV -- for now. The second sub would be called in turn by the following two or three.

    Furthermore, I wouldn't necessarily rely on the fact that the list you are searching for resides in @ARGV, for the simple reason that a real program will very likely accept its data from a file or other source rather than as discrete command-line arguments. Copy the arguments into a normal array and pass a reference to this array to the task subroutines above. If you have no idea what I mean by references at the moment, click or run perldoc perlreftut.

    Oh, and your uninitialized variables problems will pretty much Go Away if you do two things:

    • set all your "my" variables to a value (preferably 0 or "") when you declare them, and
    • instead of testing for defined (which you haven't quite got the hang of yet), just test for nonzero/non-null: if ($variable) instead of if (defined $variable).

    You're getting there, just not quite there yet.

Re: Reply to replies and update
by starbolin (Hermit) on Aug 10, 2006 at 05:45 UTC

    Your line 12:

    elsif ($subarg[0] = undef) {

    Always evaluates to true. You probably meant:
    elsif ($subarg[0] == undef) {

    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}

      Good catch, but $subarg[0] = undef always evaluates to *false* (not true), and $subarg[0] == undef is also wrong.

      elsif (not defined $subarg[0]) {

        ++ikegami


        s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://566505]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found