Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

subtracting numbers

by frank1 (Beadle)
on Dec 23, 2020 at 10:20 UTC ( [id://11125668]=perlquestion: print w/replies, xml ) Need Help??

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

i want when i enter in like 50 and number Greater than or equal, 3 should be subtracted and then print out what is sent.

same applies to other

#!/usr/bin/perl -w use strict; use warnings; print "Enter a number\n"; my $number = <>; chomp($number); my $sendthis = ''; # this area is working if ($number) { if ($number >= 10 ) { $sendthis = $number - 2; print "You sent: $sendthis\n"; } # this area not working if ($number >= 20 ) { $sendthis = $number - 3; print "You sent: $sendthis\n"; # WHEN I ENTER IN 23 # THE OUTPUT IS # You sent: 21 ---- I DONT NEED THIS # You sent: 20 ---- THIS IS THE RIGHT ANSWER } # and this not working if ($number >= 50 ) { $sendthis = $number - 4; print "You sent: $sendthis\n"; # WHEN I ENTER IN 55 # THE OUTPUT IS # You sent: 53 ---- I DONT NEED THIS # You sent: 52 ---- I DONT NEED THIS # You sent: 51 ---- THIS IS THE RIGHT ANSWER } }

Replies are listed 'Best First'.
Re: subtracting numbers
by marto (Cardinal) on Dec 23, 2020 at 10:40 UTC

    Welcome. If this is a homework or exercise question it may help to post the actual challenge. Assuming that you want to prompt the user to enter one number, then subtract from that a number depending on user input value, try something like this.

    #!/usr/bin/perl use strict; use warnings; print "Enter a number\n"; my $number = <>; chomp($number); my $sendthis = ''; if ( $number ) { if ( $number >= 50 ) { $sendthis = $number - 4; } elsif ( $number >= 20 ) { $sendthis = $number - 3; } elsif ( $number >= 10 ) { $sendthis = $number - 2; }else{ $sendthis = $number; } } print "You sent: $sendthis\n";

      i appreciate

Re: subtracting numbers
by bliako (Monsignor) on Dec 23, 2020 at 10:42 UTC

    if ($number >= 10 ) { captures all numbers greater-or-equal than 10: 10, 11, 20, 50 etc.

    When you enter 10 50, it is matched by if ($number >= 10 ) { but also by if ($number >= 20 ) { and also by if ($number >= 50 ) { and so on.

    If I understand correctly you want to match ranges: 10-20 or 20-50. But not both. One way to achieve this is to use an elsif

    if( $number >= 50 ){ $sendthis = $number - 4; } elsif( $number >= 20 ){ $sendthis = $number - 3; } elsif( $number >= 10 ){ $sendthis = $number - 2; }

    Notice that you start checking for the greatest number in the range (50) and go down. Also notice that indenting (append tab or spaces in code within code-blocks) makes reading code easier.

    bw, bliako

Re: subtracting numbers
by hippo (Bishop) on Dec 23, 2020 at 10:43 UTC

    This is a problem of logic, rather than Perl. The number 55 matches all three of your >= criteria, so you will of course get all 3 outputs. If you only want one output for each then consider reversing the order of your tests and using elsif for the other blocks. eg:

    if ($number >= 50 ) { # ... } elsif ($number >= 20) { # ... } elsif ($number >= 10) { # ... }

    This way you will get, at most, one line of output for any value of $number.


    🦛

Re: subtracting numbers
by kcott (Archbishop) on Dec 23, 2020 at 13:08 UTC

    G'day frank1,

    In the "release notes for Perl 5.32.0", you'll see the introduction of "Chained comparisons capability". If you have that version, you can do this:

    $ perl -E ' my @nums = qw{-1 0 1 9 10 11 19 20 21 23 49 50 51 55}; for my $in (@nums) { my $out; if ($in < 10) { $out = $in; } elsif (10 <= $in < 20) { $out = $in - 2; } elsif (20 <= $in < 50) { $out = $in - 3; } else { $out = $in - 4; } printf "IN: %2d OUT: %2d\n", $in, $out; } ' IN: -1 OUT: -1 IN: 0 OUT: 0 IN: 1 OUT: 1 IN: 9 OUT: 9 IN: 10 OUT: 8 IN: 11 OUT: 9 IN: 19 OUT: 17 IN: 20 OUT: 17 IN: 21 OUT: 18 IN: 23 OUT: 20 IN: 49 OUT: 46 IN: 50 OUT: 46 IN: 51 OUT: 47 IN: 55 OUT: 51

    I've been using that new feature quite a lot. I believe it makes the definition of ranges, such as are in use here, much clearer and easier to read.

    Note all the numbers that I've used for testing. This checks for edge cases around zero and the numbers where conditions change (as well as your two arbitrary numbers 23 & 55). This helps to highlight mistakes, as off-by-one errors are common when using <, <=, >= and >: you should aim to test like this regardless of whatever language or version you might be using.

    Your written description only mentions "3 should be subtracted"; however, your code has instances where 2, 3 or 4 are subtracted. I'll leave you to make adjustments depending on what you really want.

    — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11125668]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found