RE: Newbie Question about subroutine
or you could just change this: my $vInput = @_; to thisL my $vInput = @_[0]; -Original Message- From: Barry Carroll [mailto:[EMAIL PROTECTED]] Sent: Tuesday, August 07, 2001 12:03 PM To: '[EMAIL PROTECTED]' Subject: Newbie Question about subroutine Hi all, i want to have a subroutine for checking user input: here is a snippet of the code: print ("Is your Terminal ANSI compliant?\nYes or No, \(y\) or \(n\)?\n"); chomp ($input = ); verifyInput($input); sub verifyInput { # Subroutine to verify input # Will return the correct value # and discard bad values my $vInput = @_; processing in here } The problem is that I tried entering 'y', 'n', anything!, but no luck. I found out that $vInput is always getting assigned the numeric value '1'. The rest of the sub after this is fine, if i could just figure out why $vInput is being assigned '1' Any ideas? - Thanks in advance :) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Newbie Question about subroutine
Thanks, I didn't know that, i got around the problem with this: print ("Is your Terminal ANSI compliant?\nYes or No, \(y\) or \(n\)?\n"); chomp ($input = ); verifyInput($input); sub verifyInput { # Subroutine to verify input # Will return the correct value # and discard bad values my ($vInput) = @_; .. } just put the sclar in brackets :) -Original Message- From: Mooney Christophe-CMOONEY1 [mailto:[EMAIL PROTECTED]] Sent: Tuesday, August 07, 2001 5:19 PM To: '[EMAIL PROTECTED]' Subject: RE: Newbie Question about subroutine @_ is a list and $vInput is a scalar. A list in scalar context returns the number of elements in the array. So, $vInput=@_ will put the number of elements in @_ into $vInput, which, naturally, is always one. To extract the single argument from the list, do this: $vInput=shift; This will take the first (and only) element in @_ and assign it to $vInput. -Original Message- From: Barry Carroll [mailto:[EMAIL PROTECTED]] Sent: Tuesday, August 07, 2001 11:03 AM To: '[EMAIL PROTECTED]' Subject: Newbie Question about subroutine Hi all, i want to have a subroutine for checking user input: here is a snippet of the code: print ("Is your Terminal ANSI compliant?\nYes or No, \(y\) or \(n\)?\n"); chomp ($input = ); verifyInput($input); sub verifyInput { # Subroutine to verify input # Will return the correct value # and discard bad values my $vInput = @_; processing in here } The problem is that I tried entering 'y', 'n', anything!, but no luck. I found out that $vInput is always getting assigned the numeric value '1'. The rest of the sub after this is fine, if i could just figure out why $vInput is being assigned '1' Any ideas? - Thanks in advance :) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Newbie Question about subroutine
On Tue, 7 Aug 2001, Barry Carroll wrote: > here is a snippet of the code: > > print ("Is your Terminal ANSI compliant?\nYes or No, \(y\) or \(n\)?\n"); First of all, get rid of those backslashes. No need to put your string in parens. You can do this: print "Is your Terminal ANSI compliant?\nYes or No, (y) or (n)?\n" > sub verifyInput > { > # Subroutine to verify input > # Will return the correct value > # and discard bad values > > my $vInput = @_; The problem is that you are assigning an array to a scalar, which means your array is put into a scalar context, which means the scalar will be assigned number of elements in the array. Since you are only passing one argument to the sub, you just need: sub verifyInput { my $vInput = shift; ... } If you have multiple arguments, you can do: my ($vInput1, $vInput2) = @_; or my $vInput1 = shift; my $vInput2 = shift; -- Brett http://www.chapelperilous.net/btfwk/ The way I understand it, the Russians are sort of a combination of evil and incompetence... sort of like the Post Office with tanks. -- Emo Philips -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Newbie Question about subroutine
@_ is a list and $vInput is a scalar. A list in scalar context returns the number of elements in the array. So, $vInput=@_ will put the number of elements in @_ into $vInput, which, naturally, is always one. To extract the single argument from the list, do this: $vInput=shift; This will take the first (and only) element in @_ and assign it to $vInput. -Original Message- From: Barry Carroll [mailto:[EMAIL PROTECTED]] Sent: Tuesday, August 07, 2001 11:03 AM To: '[EMAIL PROTECTED]' Subject: Newbie Question about subroutine Hi all, i want to have a subroutine for checking user input: here is a snippet of the code: print ("Is your Terminal ANSI compliant?\nYes or No, \(y\) or \(n\)?\n"); chomp ($input = ); verifyInput($input); sub verifyInput { # Subroutine to verify input # Will return the correct value # and discard bad values my $vInput = @_; processing in here } The problem is that I tried entering 'y', 'n', anything!, but no luck. I found out that $vInput is always getting assigned the numeric value '1'. The rest of the sub after this is fine, if i could just figure out why $vInput is being assigned '1' Any ideas? - Thanks in advance :) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Newbie Question about subroutine
Hi all, i want to have a subroutine for checking user input: here is a snippet of the code: print ("Is your Terminal ANSI compliant?\nYes or No, \(y\) or \(n\)?\n"); chomp ($input = ); verifyInput($input); sub verifyInput { # Subroutine to verify input # Will return the correct value # and discard bad values my $vInput = @_; processing in here } The problem is that I tried entering 'y', 'n', anything!, but no luck. I found out that $vInput is always getting assigned the numeric value '1'. The rest of the sub after this is fine, if i could just figure out why $vInput is being assigned '1' Any ideas? - Thanks in advance :) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]