Wednesday, 15 January 2014

linux - Perl program for sorting an array not working -



linux - Perl program for sorting an array not working -

i new perl programming.... wrote simple script sorting of numbers in array script not working expected. please help me this.

#!/usr/bin/perl print ("enter numbers...... please come in blank space @ end\n"); $input = <stdin>; chop ($input); $a = 1; until ( $input == "" ) { @array[$a-1] = $input ; $a++; $input = <stdin>; chop ($input); } print ("@array\n"); $count = 1; $y = 1; while ( $count < @array ) { if ( $array[$y-1] > $array[$y] ) { @array[$y-1,$y] = @array[$y,$y-1]; $y++; } $count++; } print ("@array\n");

i get:

enter numbers...... please come in blank space @ end 2 4 3 2 4 3 2 4 3

http://codepad.org/ltvxt8zg

#!/usr/bin/perl utilize strict; utilize warnings; $"=','; @input; while(<data>) { s/\s+$//; force @input, $_; } print "original input: @input\n"; @input = sort { $a <=> $b } @input; print "sorted input: @input\n"; __end__ 3 1 2

linux perl

No comments:

Post a Comment