Remembering Positions in Array after processing the element
- From: ewijaya@xxxxxxxxxxxxxx (Edward WIJAYA)
- Date: Fri, 29 Jul 2005 20:26:00 +0800
Hi, I wanted to append two subsequent characters (non *) in an array and then storing them into new array. But then I also want to restore the '*' string in its initial position in the new array result.
I have the code below but still not achieving the task. How can I modify them so that I can get it to return result as stated in the example below?
__BEGIN__ #!/usr/bin/perl -w use strict; use Data::Dumper;
# For first elem
# BA also acceptable
my @array = ('A','B','*','C'); # returns: ['AB', 'BA','*','CB']
my @array1 = ('*','A','B','C'); # returns: ['*', 'AB','BA','CB']
my @array2 = ('A','B','C','*'); # returns: ['AB', 'BA','CB','*']
my @array3 = ('A','B','C','D'); # returns: ['AB', 'BA', 'CB','DC']my @result = get_array(@array);
print Dumper \@result ; # See above ('returns') for the desired result
sub get_array{ my @array = @_;
my @result;
foreach ( 0 .. $#array-1 )
{
my $st = $array[$_+1].$array[$_];
push @result, $st;
}
return @result;
}__END__
Thanks and hope to hear from you again.
-- Regards, Edward WIJAYA SINGAPORE .
- Prev by Date: Re: Getting Date Stamp of a file on a Win32 System
- Next by Date: regular expression match question
- Previous by thread: Grep uniqueness issue
- Next by thread: regular expression match question
- Index(es):
Relevant Pages
|