Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

struggling with sub-routines

by Anonymous Monk
on Jun 18, 2002 at 13:33 UTC ( #175355=perlquestion: print w/replies, xml ) Need Help??

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

Hi, i have now been struggling with the same bit of code for weeks and would really appreciate some help. I am trying to sort a file into catergories and then have a menu where the user can select the catergory they want to look at then return to the main menu for anothe choice etc. In principal this sounds easy! I posted a question a few week ago and was advised to write a sub-routine to help. I have been trying but can't get anywhere. Could anyone look at my code to suggest anything??? Since i have written the sub-routine - none of the results are displayed when the user selects an option. thankx.
#! /usr/local/bin/perl -w use strict; my $num_of_params; $num_of_params = @ARGV; if ($num_of_params < 2) { die ("\n You haven't entered enough parameters \n\n"); } + open (FH, $ARGV[0]) or die "unable to open file"; open (OUTFILE, ">$ARGV[1]"); my $line; my @array; my $first=0; my $second=0; my $third; my $forth; my $choice; my $others; while (<FH>) { chomp; my @array = split /\s+/; $array[8] =~ tr/%//d; } print STDOUT "\nPlease select the catergory of results that you wish t +o see;\n\n \t1. first \n\t2. second \n\t3. third \n\t4. forth \n\t5. Others etc\ +n\t6. Exit program\n\n"; my $choice = <STDIN>; chomp $choice; print "\n Identifier \t\t%identity \n\n"; catergory ($first); if ($choice eq 1) { print $first; } sub catergory { my @array = @_; if (($array[1] =~ m{/:first\|\w+\|\w+}) && ($array[8] > 95)) { my $first = "$array[1]\t$array[8]\n"; } elsif (($array[1] =~ m{/:second\|\w+\|\w+}) && ($array[8] > 95)) { $second = "$array[1]\t$array[8]\n"; } elsif (($array[1] =~ m{/:third\|\w+\|\w+}) && ($array[8] < 95)) { $third = "$array[1]\t$array[8]\n"; } elsif (($array[1] =~ m{/:forth\|\w+\|\w+}) && ($array[8] < 95)) { $forth = "$array[1]\t$array[8]\n"; } elsif (($choice eq 5) && ($array[1] =~ m{/:other\|\w+\|\w+}) { $others = "$array[1]\t\t$array[8]\n"; } return $first; return $second; return $third; return $forth; return $others; } close OUTFILE;

Replies are listed 'Best First'.
Re: struggling with sub-routines
by arturo (Vicar) on Jun 18, 2002 at 13:51 UTC

    The problem you're seeing has to do with the return call; what it does is just that -- returns the value AND stops execution of the subroutine. So, where your sub has:

    return $first; return $second; return $third; return $forth; return $others;
    The only value returned is $first; the rest of those are ignored. What you apparently want to do is return a list, and fortunately, that's easy to do:
    return ($first, $second, $third, $forth, $others);

    or, better yet, since your names clearly suggest an *array*, why not return an array and avoid declaring so many variables?

    Your code has a number of other issues (e.g. you pass in a scalar, and yet your subroutine is built to deal with an array, you don't use the return value of your subroutine at all), but I'll just stop here and let others help you out with those issues.


    I mistrust all systematizers and avoid them. The will to a system shows a lack of integrity -- F. Nietzsche

Re: struggling with sub-routines
by robobunny (Friar) on Jun 18, 2002 at 13:45 UTC
    you are referencing an array inside your subroutine, but you are not actually passing it an array, you are passing it a scalar ($first). it looks as if you may have intended for @array to be a global, but then you assigned to it inside the subroutine.
      I have taken out the $array = @_; and have also changed the return but it still doesn't work?????
Re: struggling with sub-routines
by r0b (Pilgrim) on Jun 18, 2002 at 16:19 UTC
    As arturo said in his earlier reply, to return a list of values from a subroutine you need to use:
    return ($first, $second, $third, $fourth, $others);
    To retrive this list from your subroutine you need to use the following:
    @ret = catergory();
    I have omitted the $first that you were sending to the subroutine as I believe you meant to send @array (which was obtained from the file) instead although I'm not sure of this.

    There are some other possible problems with your code but I'm still having trouble understanding what your trying to do. If you explain in more depth your aims this would greatly help.

    Good luck with your project!

    eval pack "h*", "072796e647022245d445f475454494c5e622b3";

      The aims of this program are to catergorise the input file with regular expressions, then the user can select from the menu which catergory to look at ; eg
      1. choice1 2. choice2
      etc. i want the program to go back to the main menu after a choice has been made and the results are displayed.
        To loop the menu i would suggest using the following:
        while(1) { print "\nPlease select the catergory of results that you wish to s +ee;\n\n\t1. first \n\t2. second \n\t3. third \n\t4. forth \n\t5. Othe +rs etc\n\t6. Exit program\n\n"; chomp($choice = <STDIN>); if ($choice eq '6') { last; } #your code here }
        As for the rest of the program I am still unsure as to what the files your are parsing will be like. Could you give an example file?

        eval pack "h*", "072796e647022245d445f475454494c5e622b3";

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://175355]
Approved by arturo
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2023-09-29 17:04 GMT
Find Nodes?
    Voting Booth?

    No recent polls found