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"; }


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: result is not listening to if
by prospect (Acolyte) on Jul 14, 2017 at 12:53 UTC
    Hi! The thing is in the book they don't use it in the examples and the exercises they only use "warnings;" So I thought "ok, saves me some typing". I wanted the max string length to be 100 yes. Ok thanks for the advice !
      So I thought "ok, saves me some typing".

      Easy mistake to make. Instead you should have thought "ok, I'll program my editor to auto-insert those in every Perl file I create".

        I haven't gotten that far yet, I use Visual Studio Code, I will need to check where I can do that.

      The strict thing is kind of a sore point here in the Monastery. The Learning Perl people don't teach it because that saves them a little bit of explanation in their beginner classes. But then their students show up here with problems that would have been caught by strict and we have to straighten them out.