Solved

perl scripting help

Posted on 2006-10-19
5
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 …
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…

630 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