in reply to Comparing numbers

Well, where do we start?

First up, foreach my $number ($file) is trying to iterate over a single value, ie the string '3.34, 3.67, 4.75, 4.98'. The code inside the loop is only going to see '3.34, 3.67, 4.75, 4.98' and not the individual values. Did you test this code? Did you put a print inside the loop to see what $number contained?

So, what you're really looking for is to split up the string in $file into the individual values. Let's have a look in perlfunc to see if there's anything available to help us...look at that...a function called split. So far, so good. Let's see how that works...

split /PATTERN/,EXPR,LIMIT
split /PATTERN/,EXPR
split /PATTERN/
split

Splits a string into a list of strings and returns that list
That sounds like the business. So, if we apply split to $file we can get get the numbers out of it - cool! But, wait, what PATTERN to use? From looking at the contents of $file we can see that each number is seperated by ", ", sheesh, that was pretty easy. So we get the line:
my @numbers = split /, /,$file;
And how do we check that @numbers contains what we think it should? We could use a print statement, something like:
print join("\n",@numbers) . "\n";
or we could run our little script in the perl debugger perl -d difference.pl, assuming of course that you've created a file called 'difference.pl' and that you have perl installed on your computer.

So what do we need to do next? We have to iterate over them calculating the differences. OK, how do we do that? The most obvious choice would be a foreach loop. And how should the iteration be defined? My personal choice would be to start from the second position and work towards the end, but you may have a different preference, it's entirely up to you - after all it's your homework and your grade. Using my choice of iteration definition gives us:

foreach my $index (1 .. $#numbers)
"Why 1?" I hear you say, "I though you said we were going to start at the second position?". We're starting at 1 because, in Perl, unless you specifically tell it otherwise, arrays are assumed to start at index 0, making index 1 the second index.

Now we're getting somewhere. Next we have to compute the difference. That's pretty easy:

my $difference = abs($numbers[$index] - $numbers[$index - 1]);
Now all we have to do is to keep track of the maximum difference. My personal preference is to use the terniary operator "?:", but, again, your homework, your grade, so feel free to use your imagination and use any construct you feel comfortable with. Of course, we must remember to initialise the variable holding the maximum difference correctly, otherwise this work will all be for nothing.
$max_difference = ($index == 1 or $difference > $max_difference)?$diff +erence:$max_difference;
Finally, all we need to do now is to print the result:
print "$max_difference\n";
Putting the whole thing together (adding variable definitions and other appropriate code furniture):
use strict; use warnings; my $file='3.34, 3.67, 4.75, 4.98'; my @numbers=split /, /,$file; my $max_difference; foreach my $index(1 .. $#numbers) { my $difference = abs($numbers[$index] - $numbers[$index - 1]); $max_difference = ($index == 1 or $difference > $max_difference)?$d +ifference:$max_difference; } print "$max_difference\n";
Be sure to post back and let everyone know how your grade!

rdfield