Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

perl scripting help

Posted on 2006-10-19
5
Medium Priority
?
223 Views
Last Modified: 2010-04-16
Hi. I have a script here but it is not doing what its suppose to do. I am trying to make it so it sorts my array ascending. Here is the script.
#!/usr/bin/perl
@array = (5,3,2,1,4);
$i=1;
$a=1;
$s=1;
var1=0;
var2=0;
var3=0;
var4=0;
var5=0;
while($i < $#array) {
for($j=0; $j < $#array; $j++){
if ($array[$j] == $a) {
$var[$s]=$a;
$a=$a+1;
$i=$i+1;
$s=$s+1;
}
}
}
@array = ($var1,$var2,$var3,$var4,$var5);
foreach $elem (@array){
print "$elem";
}


0
Comment
Question by:micromarch
  • 3
  • 2
5 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 17770865
@array = sort{$a<=>$b} @array;
0
 
LVL 2

Author Comment

by:micromarch
ID: 17770867
i know this works, but im trying to do it the cheap way.
0
 
LVL 85

Expert Comment

by:ozo
ID: 17770886
@array = sort{$a<=>$b} @array;
is the cheap way
0
 
LVL 2

Author Comment

by:micromarch
ID: 17770892
well then the more complicated way as you can see with my code.
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 17770913
@array = (5,3,2,1,4);
for my $i ( 0..$#array ){
          for my $j ( $i+1 .. $#array ){
               @array[$i,$j] = @array[$j,$i] if $array[$j] < $array[$i];
          }
}
print "@array\n";
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question