in reply to result is not listening to if
Hi prospect,
Point #1: Always ALWAYS use strict; and declare your vars with my()!
Now, from your post it is not clear whether you want to check the result of an arithmetic operation, or the length of a string.
The x operator in Perl is not the multiplication operator, but the string (or list) repetition operator. See perlop.
Thus your code creates a string from repeating $string $num times.
To check the length of a string (the only thing that makes sense if you are using x), use length.
use strict; use warnings; use feature 'say'; print 'Enter a string: '; chomp( my $string = <STDIN> ); print 'Enter a number: '; chomp( my $num = <STDIN> ); my $result = $string x $num; say "The result is :\n $result"; my $length = length $result; if ( $length >= 100 ){ say "Length $length is too big, bye"; } else { say "Length $length OK"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: result is not listening to if
by prospect (Acolyte) on Jul 14, 2017 at 12:53 UTC | |
by hippo (Archbishop) on Jul 14, 2017 at 14:11 UTC | |
by prospect (Acolyte) on Jul 14, 2017 at 14:23 UTC | |
by karlgoethebier (Abbot) on Jul 14, 2017 at 17:57 UTC | |
by Anonymous Monk on Jul 14, 2017 at 13:49 UTC |