How do i sort a perl array?
-
Use the sort operator of course.
Here we sort the @animals array. You can see the output is in sorted order.
@animals = ("lion", "tiger", "bear", "monkey"); @gobackwards = sort(@animals); foreach $animal (@gobackwards) { $n += 1; print "Hello, my name is $animal and I'm an animal, in the 100m sprint my number is $n\n"; }
Results is:
Hello, my name is bear and I'm an animal, in the 100m sprint my number is 1
Hello, my name is lion and I'm an animal, in the 100m sprint my number is 2
Hello, my name is monkey and I'm an animal, in the 100m sprint my number is 3
Hello, my name is tiger and I'm an animal, in the 100m sprint my number is 4
© Lightnetics 2024