Gossamer Forum
Home : General : Perl Programming :

2 questions -->perl

Quote Reply
2 questions -->perl
First of all if you have:
Code:
if ($element =~ $StructureInFile){
$RealUserName = "true";
$element++;
chop $element;
if($element =~ $PasswordStructure){
print "$element<BR>";
print "Password is correct<BR>";
}else{print "password is wrong<BR>";}
}

will the $element++ work nested in an if statement that is in a foreach loop?

-- - - - - - - - - - - - - - - - - - - - -
Next question:
while going through a foreach loop, how would you break out of it if a condition is true, but then move to the next element to see if the next element produces a match also?
A student who WILL master PERL!
Quote Reply
Re: [instruction] 2 questions -->perl In reply to
Use next; to skip a loop iteration.

Last edited by:

Paul: Oct 20, 2002, 5:59 AM
Quote Reply
Re: [Paul] 2 questions -->perl In reply to
can I use the next inside the if function, or does it have to stay in the foreach loop?
A student who WILL master PERL!
Quote Reply
Re: [instruction] 2 questions -->perl In reply to
Yeah you can use it inside an if....basically anywhere inside a loop.

Last edited by:

Paul: Oct 20, 2002, 6:08 AM
Quote Reply
Re: [Paul] 2 questions -->perl In reply to
Sorry , but what I am saying is that the if is inside the foreach loop. The "next;" would be for the foreach loop, but can I put it in the if loop that is inside the foreach loop and it still increment the for each loop.
A student who WILL master PERL!
Quote Reply
Re: [instruction] 2 questions -->perl In reply to
Yeah I knew what you meant. The next; can go anywhere inside the loop block Wink
Quote Reply
Re: [Paul] 2 questions -->perl In reply to
cool Cool thank you.
A student who WILL master PERL!
Quote Reply
Re: [Paul] 2 questions -->perl In reply to
How do you get a subroutine to return 2 or more variables.

I want to assign to differrent variables two differrent values, which will be found in the subroutine.
A student who WILL master PERL!
Quote Reply
Re: [instruction] 2 questions -->perl In reply to
You need to get a good perl book or browse perldoc. These are all questions, the answers for which you can find in the tutorials.

http://www.perldoc.com/...p;restrict=perl5.6.1

http://www.perldoc.com/...6.1/pod/perlsub.html

http://www.perldoc.com

Last edited by:

Paul: Oct 20, 2002, 7:36 AM
Quote Reply
Re: [instruction] 2 questions -->perl In reply to
In Reply To:
How do you get a subroutine to return 2 or more variables.

I want to assign to differrent variables two differrent values, which will be found in the subroutine.

Just have your sub return a list of values. Here, 3 values are passed to a sub, which manipulates 2 of them using the 3rd, and then returns the two manipulated values. The first line assigns the returned values to two other variables.
Code:
($product, $sum) = mysub($this, $that, $the_other);

sub mysub {
my $this = shift;
my $that = shift;
my $the_other = shift;
$this *= $the_other;
$that += $the_other;
return $this, $that;
}
Quote Reply
Re: [SandyM] 2 questions -->perl In reply to
SmileThank you very much, but I have one more question now-->what is the * and the + doing in the following:
Quote:
$this *= $the_other;
$that += $the_other;
Crazy
Thanks again.
A student who WILL master PERL!
Quote Reply
Re: [instruction] 2 questions -->perl In reply to
multiply and assign
add and assign

Essentially it is the same as:

$this = $this * $the_other;
$that = $that + $the_other;

Can be found in the perldocs :)

Last edited by:

Paul: Oct 22, 2002, 8:52 AM
Quote Reply
Re: [Paul] 2 questions -->perl In reply to
ok thank you, I have been studying the perl docs. I appreciate the link to that document.
A student who WILL master PERL!