There's more than one way to do things PerlMonks

### Need help converting!!!

by reynoldswrap530 (Initiate)
 on Nov 15, 2015 at 22:54 UTC Need Help??

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

Hello, I have been working with python for a while and I have to convert this algorithm into Perl and I have never used Perl so I just started but I need this done asap so any help would be appreciated.

1. Start

2. Ask user to input a positive integer (or a negative integer to stop)

3. Record the user's input

4. Test the input >= 0

4a. If true

4a1. Add the input to "running total"

4a2. Increment "number of inputs counter"

4a3. Test if "number of inputs counter" is equal to 1

4a31. If true, replace both "highest input" and "lowest input" with the input

4a5. If the input > "highest input" then replace "highest input" with input

4a6. If the input < "lowest input" then replace "lowest input" with input

4a7. Goto step 2

4b. If false

4b1. Goto step 5

5. Calculate the average (formula: "running total" /"number of inputs counter" )

6. Display/communicate the "highest input", "lowest input" and average

7. Terminate

Thanks for anybody who could convert this to Perl.

Replies are listed 'Best First'.
Re: Need help converting!!!
by choroba (Cardinal) on Nov 15, 2015 at 23:07 UTC

1. Use a shebang line at the top of your program, usually followed by strict and warnings.

2. Use print to display text to the user.

4. Use if and comparison operators.

1. A block starts with a {.

2. Use ++ to increment.

3. Use == to test numeric equality.

1. Use = for assignment.

4. -

5. > is an operator.

6. < is an operator, too.

7. Rather than using goto, wrap the whole part in a while loop.

2. The while condition should test whether the loop should end or not. You'll have to adjust the algorithm a bit, as you can't test before you get the input, but the test is the first thing in the loop (or last, see do).

5. / is the division operator .

6. Use print again.

7. Perl terminates once it has no more lines to execute.

```(\$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,

4. >= is the "greater than or equal" comparison operator.

Sorry choroba, I just had to...

honestly, i dont really understand the whole "goto" thing or a legit reason to use it. in my mind though, it is a failure of the programmer to even understand their own logic, which makes them use such a shortcut, especially when there are plenty of other things you can do.
"goto" is older.

Many highlevel constructs are just gotos in disguise and their documentation still contains expressions like "go to beginning of loop"

A language agnostic algorithm description might still say "go to" (like in this case)

And because most use cases have been abstracted away doesn't mean there are no legit applications left for goto

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Given that the OP was presenting a relatively-plain-English description of an algorithm, rather than actual code (or even pseudocode!), I'm rather confident that "goto 2" was intended to mean "repeat the above process, starting at step 2", not that he used or expected to use an actual goto statement.
Re: Need help converting!!!
by stevieb (Canon) on Nov 16, 2015 at 00:03 UTC

Welcome to the Monastery!

Not taking away from choroba or tonto, but being a Python dev, and even though this is PerlMonks, I'd request to see what Python code you have before you ask us to write Perl code for you.

Throwing in a bit of Perl code that you've tried would be hugely helpful (and would go a long way) as well.

-stevieb

Re: Need help converting!!!
by BillKSmith (Monsignor) on Nov 16, 2015 at 15:34 UTC
If you interested in the operator interface more than the details of the algorithm, use of perl modules can simplify the progam and provide improved error reporting/recovery.
```use strict;
use warnings;
use IO::Prompt::Hooked;
use List::Util qw(max min sum);
my %prompt_options = (
message  => 'input a positive integer (or a negative integer to st
+op)',
validate => qr/^-?\d+\$/,
error    => "Input must be a positive integer\n",
escape   => qr/^-\d/,
);

my @input_array;
push @input_array, \$_ while (defined (\$_ = prompt(%prompt_options)));
die "No inputs were entered\n" if (!@input_array) ;

print "\n\n\n";
print 'Highest input: ', max( @input_array ), "\n";
print 'Lowest input:  ', min( @input_array ), "\n";
print 'average:       ', sum( @input_array ) / @input_array, "\n";

This approach is wasteful of both computer time and memory, but neither should be a problem with the small amount of data that a human operator is likely to enter. The advantage is that this is easy to write and you can hve confidence in the computations done by the modules.

Bill
Re: Need help converting!!!
by james28909 (Deacon) on Nov 16, 2015 at 02:08 UTC
It seems perlmonks is a "InsertLanguageHere2perl converter". Wish I would have known this sooner :P
Re: Need help converting!!!
by Lennotoecom (Pilgrim) on Nov 16, 2015 at 18:57 UTC
looks like a college assn
but who cares
```while(print "in> " and chomp(\$a = <STDIN>) and \$a >= 0){
\$c++;
\$s += \$a;
\$m = \$a if !\$m;
\$M = \$a if \$M < \$a;
\$m = \$a if \$m > \$a;
}

print "max: \$M\navg: ".\$s/\$c."\nmin: \$m\n";

here is another one
a bit uglier and shorter
```push @r, \$a and \$s += \$a while print "in> " and chomp(\$a = <STDIN>) an
+d \$a >= 0;
@r = sort {\$a cmp \$b} @r;
print "max: \$r[\$r-1]\navg: ".\$s / @r." \nmin: \$r[0]\n";

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1147761]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-06-15 22:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?
 • erzuuli ‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.