Solved

perl scripting help

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
My 2003 Land Rover discovery 2 key for code for car alarm 7 79
count8 challlenge 13 85
mapShare challenge 13 68
python sqlite question 11 42
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now