Remembering Positions in Array after processing the element



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
.



Relevant Pages

  • Re: A challenging file to parse
    ... Do the same thing, but when storing in an array, store it at ... Also the data format. ...
    (comp.lang.c)
  • Re: Serializing data vs Storing it in a seperate table
    ... and storing it in a single entry in a table or storing each element of ... Do some research on "database normalization". ... It really depends how big the array is and whether you ...
    (comp.lang.php)
  • Re: A challenging file to parse
    ... algorithm problem and found a Orun-time and Omemory solution, ... Do the same thing, but when storing in an array, store it at ...
    (comp.lang.c)
  • Resizing a structur array in C
    ... doubles but to storing mxArray* pointers. ... I tried to resize a structure array using the C ... not the size of a pointer. ...
    (comp.soft-sys.matlab)
  • Re: an array to a database, am having hard time , help!!
    ... have you tried putting single quotes around the data when you INSERT it? ... Both basically set up a list of acceptable values for the ... As you are storing an array, I would advise you to convert it to a ... an array of the numbers for whatever purpose you wish. ...
    (comp.lang.php)