Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

increment operator

Status
Not open for further replies.

skottieb

Programmer
Jun 26, 2002
118
AU
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 &quot;Enter name of second person: &quot;;
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 think you were close... just don't want the last statement in there:

$_ = &quot;it is simple&quot;;
$i++ while /i/ig;
print $i, &quot;\n&quot;; # outputs '3'

--jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top