prospect has asked for the wisdom of the Perl Monks concerning the following question:

This is a little experimentation on the exercises in the "Learning Perl" book, this what I tried:

#! /usr/perl use warnings; print "Enter a string"; $string = <STDIN>; print "Enter value"; chomp($num = <STDIN>); $result = $string x "$num"; print "The result is :\n $result"; if ($result >= "100"){ print "Too big, bye"; } else { }

$result doesn't want listen to my "if" control structure :( so it goes 100+.

The goal was to have the script detect if the output would be more than 100. And then display a message

Help please ?

Replies are listed 'Best First'.
Re: result is not listening to if
by choroba (Cardinal) on Jul 14, 2017 at 12:22 UTC
    What did you use as input? The if works correctly: it takes the $result, i.e. the $string repeated $num times, and compares it as a number to 100. So, to get the "Too big" message, you need to enter
    101 1

    then 101 x 1 is 101 which is greater or equal to 100.

    You probably wanted to compare just the $num? Also, don't use double quotes around variables, especially when using numeric comparison or operators:

    #!/usr/bin/perl use warnings; use strict; print 'Enter a string '; my $string = <STDIN>; print 'Enter value '; chomp( my $num = <STDIN> ); if ($num >= 100) { print "Too big, bye\n"; } else { my $result = $string x $num; print "The result is:\n$result"; }

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Thank you! I'm going to mess around with your example a bit more.
Re: result is not listening to if
by 1nickt (Canon) on Jul 14, 2017 at 12:35 UTC

    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.
      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".

        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.
Re: result is not listening to if
by thanos1983 (Parson) on Jul 14, 2017 at 13:06 UTC

    Hello prospect,

    Welcome to the Monastery. I am wondering what you are trying to do with your script. Can you provide us a short description?

    I am curious because you ask at user to enter a string. A string could be an int but also could be an alphabetical character string. Having said that, I would also like to point out that character x is not the same as * read here perlop/Operator Precedence and Associativity about the * character and here for the x perlop/Multiplicative Operators.

    So having said that as the fellow monk choroba pointed out if you want to compare numbers do not use double quotes e.g. "100", this is not the same as 100. Double quote characters in Perl convert the int to string.

    So again back to the original question. What are you trying to achieve?

    Are you trying to replicate the string n times, or you just want to multiply integers and then check if the output is bigger than 100?

    Help us to help you.

    Looking forward to your reply, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Hello! I just tought I would try to add a limit on the string output to an exercise in the Perl book I'm reading, The excercise was "8 Write a program that prompts for and reads a string and a number (on separate lines of input) and prints out the string the number of times indicated by the num- ber on separate lines. (Hint: use the x operator.) If the user enters “fred” and “3”, the output should be three lines, each saying “fred”. If the user enters “fred” and “299792,” there may be a lot of output." I though that if you put double quoutes around a variable value in Perl, Perl would know for example that "20" is a number. And that if you put variable value between single quotes like this "my $number = '20'. Perl would take it as a literal string. I'll check out those links thank you. Regards

        I though that if you put double quoutes around a variable value in Perl, Perl would know for example that "20" is a number. And that if you put variable value between single quotes like this "my $number = '20'. Perl would take it as a literal string.

        I think you may have misread or misinterpreted something. "20" is exactly the same as '20' in perl. But if $z has value 20 then "$z" and '$z' are quite different.

        #!/usr/bin/env perl use strict; use warnings; my $dql = "20"; my $sql = '20'; my $z = 20; my $dqv = "$z"; my $sqv = '$z'; print "dql: $dql\n"; print "sql: $sql\n"; print "dqv: $dqv\n"; print "sqv: $sqv\n";

        The assignment to $dqv is what is known as interpolation. Double quotes do it but single quotes do not. See the "interpolates" column in the table in Quote and Quote-like Operators for the full list.

        Note that by putting any sort of quotes around a numerical value you are essentially converting it into a string. $z above is a pure number the other variables are all strings. But Perl isn't strongly typed so you can get away with plenty of mixing and matching.

        Addendum: The choice of variable names in this example might not be obvious. They are abbreviations for "double quoted literal", "single quoted literal", "double quoted variable" and "single quoted variable". (Thanks 1nickt for pointing out the lack of clarity).

        Hello again prospect,

        Regarding your question you can find a very similar question on the forum that contains many interesting answers: A function to determine if a string is numeric, similarly How to determine if something is numeric?.

        A most common way to convert a numerical string to int is casting int. For example from documentation:

        For example, int(-6.725/0.025) produces -268 rather than the correct - +269; that's because it's really more like -268.99999999999994315658 i +nstead.

        Hope this helps, BR.

        Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: result is not listening to if
by prospect (Acolyte) on Jul 17, 2017 at 07:37 UTC

    Thanks everyone for all the help, I have a lot of references and sources to get through, Regards!