Solved

perl scripting help

Posted on 2006-10-19
5
218 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 84

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 84

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 84

Accepted Solution

by:
ozo earned 500 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

828 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