We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

perl scripting help

on
Medium Priority
234 Views
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";
}

Comment
Watch Question

View Solution Only

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
@array = sort{\$a<=>\$b} @array;

Commented:
i know this works, but im trying to do it the cheap way.
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
@array = sort{\$a<=>\$b} @array;
is the cheap way

Commented:
well then the more complicated way as you can see with my code.
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
@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";

Not the solution you were looking for? Getting a personalized solution is easy.

Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile