Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extra output from Array 2

Status
Not open for further replies.

JohnLucania

Programmer
Oct 10, 2005
96
US
#! /usr/bin/perl

use warnings;
use strict;

my @array = (1 .. 100);

sub NumAscOrder {
@array = sort {$a <=> $b} (@array);
print "@array\n";
}
sub NumDescOrder {
@array = sort {$b <=> $a} (@array);
print "@array\n";
}

print "From largest to smallest: \n";
print NumAscOrder();
print "\n";

print "From smallest to largest: \n";
print NumDescOrder();
print "\n";

why both return 1 twice? 1 & 1 for From largest to smallest. 100 & 1 for From smallest to largest.

jl

 
Code:
print "@array\n";
isn't doing what you expect. Because @array is in a double-quoted string, it is in scalar context. Try something like
Code:
print join( ' ', @array ), "\n";
.

f


[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
because with print NumAscOrder() you print if the func returns success.

just call:
&NumAscOrder
instrad of:
print NumAscOrder

You already print the array in it.


Corwin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top