Hello,
I am trying to write a sub, it is supposed to increment a scalar by one on the occurence of a letter, eg: if (/i/i){ $i++; }, the problem is even if there are 2 letters in the string, ie: "it is", the value only gets incremanted once, here is the code, the particular sub im talking about is commented out...
#!perl -w
use strict;
sub input{
print "Enter name of first person: ";
my $name1 = <STDIN>;
chomp $name1;
print "Enter name of second person: ";
my $name2 = <STDIN>;
chomp $name2;
my $names = ($name1 . $name2);
return $names;
}
#sub figure{
# my @perc = @_;
# my $letter;
# my $l = 0;
# my $o = 0;
# my $v = 0;
# my $e = 0;
# my $s = 0;
#
# foreach $_ (@perc){
# if (/l/i){ $l++;}
# if (/o/i){ $o++;}
# if (/v/i){ $v++;}
# if (/e/i){ $e++;}
# if (/s/i){ $s++;}
# }
#
# my $letters = ($l . $o . $v . $e . $s);
# return $letters;
#}
my $names = input();
print $names;
my $percent = figure($names);
print $percent;
incase you are wondering is is one of those lovetests some girls do (and some guys mabey), it is for my girlfriend.
Please help, has it got something to do with the fact that the if statments are in a foreach loop, ive tried repalcing with: while (/i/g){ $i++; last), but that didn't help
skottieb
I am trying to write a sub, it is supposed to increment a scalar by one on the occurence of a letter, eg: if (/i/i){ $i++; }, the problem is even if there are 2 letters in the string, ie: "it is", the value only gets incremanted once, here is the code, the particular sub im talking about is commented out...
#!perl -w
use strict;
sub input{
print "Enter name of first person: ";
my $name1 = <STDIN>;
chomp $name1;
print "Enter name of second person: ";
my $name2 = <STDIN>;
chomp $name2;
my $names = ($name1 . $name2);
return $names;
}
#sub figure{
# my @perc = @_;
# my $letter;
# my $l = 0;
# my $o = 0;
# my $v = 0;
# my $e = 0;
# my $s = 0;
#
# foreach $_ (@perc){
# if (/l/i){ $l++;}
# if (/o/i){ $o++;}
# if (/v/i){ $v++;}
# if (/e/i){ $e++;}
# if (/s/i){ $s++;}
# }
#
# my $letters = ($l . $o . $v . $e . $s);
# return $letters;
#}
my $names = input();
print $names;
my $percent = figure($names);
print $percent;
incase you are wondering is is one of those lovetests some girls do (and some guys mabey), it is for my girlfriend.
Please help, has it got something to do with the fact that the if statments are in a foreach loop, ive tried repalcing with: while (/i/g){ $i++; last), but that didn't help
skottieb