Solved

perl scripting help

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

770 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