----- Original Message -----
From: "John W. Krahn" <[EMAIL PROTECTED]>
Newsgroups: perl.beginners
To: "Perl Beginners" <beginners@perl.org>
Sent: Friday, July 29, 2005 10:35 PM
Subject: Re: Remembering Positions in Array after processing the element
Edward WIJAYA wrote:
Hi,
Hello,
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.
Perhaps this will give you some ideas:
perl -le'
@x = qw( A B C * D );
print @x . " @x";
for ( reverse 0 .. $#x ) {
push @y, [ $_, splice @x, $_, 1, () ] if $x[ $_ ] =~ /[^A-Z]/;
}
splice @x, $_, 1, ()
Are the empty parens needed here? Seemed to work ok for me without them.
print @x . " @x";
@x = map "X$_", @x;
print @x . " @x";
for ( @y ) {
for ( reverse @y )
Need to restore the 'special' characters beginning from the beginning
of the array - (can see the error your way with data set [A * B * C]).
The chars and their positions were stored in the @y array in reverse
order. :-)
splice @x, $_->[0], 0, $_->[1];
}
print @x . " @x";
'
5 A B C * D
4 A B C D
4 XA XB XC XD
5 XA XB XC * XD
John
--
use Perl;
program
fulfillment
Hi John
I wanted to try to make up a function like Edward wanted and used your
solution
and 'added' to it.
#!/usr/bin/perl
use strict;
use warnings;
my @x = qw( A * B * C);
print "@{[get_array(@x)]}";
sub get_array{
my @x = @_;
my @y;
for ( reverse 0 .. $#x ) {
push @y, [ $_, splice @x, $_, 1, () ] if $x[ $_ ] =~ /[^A-Z]/;
}
my @i = @x[1..$#x];
my @j = @x[0..$#x-1];
@x = map "$i[$_]$j[$_]", 0..$#i;
if ($x[0]) {
unshift @x, $x[0];
}
else {
local $" = ", ";
die "died: fed bad array, [ @_ ] $!";
}
for ( reverse @y ) {
splice @x, $_->[0], 0, $_->[1];
}
return @x;
}
Chris
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>