How do i reverse the elements in a perl array?
-
Use the reverse perl operator.
@animals = ("lion", "tiger", "bear", "monkey"); @gobackwards = reverse(@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"; }
Result is:
Hello, my name is monkey and I'm an animal, in the 100m sprint my number is 1
Hello, my name is bear and I'm an animal, in the 100m sprint my number is 2
Hello, my name is tiger and I'm an animal, in the 100m sprint my number is 3
Hello, my name is lion and I'm an animal, in the 100m sprint my number is 4- Takes the array list and reverses it.
© Lightnetics 2024