Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl reverse

Status
Not open for further replies.

phlx

Technical User
Joined
Dec 3, 2004
Messages
1
Location
US
Hi, I am new to perl, And i'm wondering, how could i reverse the order of all the elements in an array? (without using the built in function reverse() of course). Like I said i'm relatively new and i've just been tinkering around. If anyone could point me in the right direction I would appreciate it
 
Look into the functions pop and push. Should allow you to do what you want.

- Rieekan
 
Code:
my @array2;
$index= scalar (@array);
$index--;
foreach (@array) {
  $array2[$index]=$_;
  $index--;
}

But why???
--Paul


Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
Sounds like homework to me - only logical reason for not using an efficient built-in function that you're actually aware of.
 
#!/usr/bin/perl -w
use strict;

my @array = qw(1 2 3 4 5);

my @array2;
foreach my $value (@array)
{
unshift @array2, $value;
}

print @array2;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top