For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > Remembering Positions in Array after processing the element









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Remembering Positions in Array after processing the element
Edward WIJAYA

2005-07-29, 5:01 pm

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
John W. Krahn

2005-07-29, 10:00 pm

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]/;
}
print @x . " @x";
@x = map "X$_", @x;
print @x . " @x";
for ( @y ) {
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
Chris Charley

2005-07-31, 5:15 pm


----- Original Message -----
From: "John W. Krahn" <krahnj@telus.net>
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:
>
> Hello,
>
>
> Perhaps this will give you some ideas:
>
> @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


John W. Krahn

2005-08-01, 4:00 am

Chris Charley wrote:
>
> ----- Original Message ----- From: "John W. Krahn" <krahnj@telus.net>
>
>
> splice @x, $_, 1, ()
> Are the empty parens needed here? Seemed to work ok for me without them.


Yes, it will work with the three argument form of splice and as to your
other issue you can use unshift instead of push.

unshift @y, [ $_, splice @x, $_, 1 ] if $x[ $_ ] =~ /[^A-Z]/;



John
--
use Perl;
program
fulfillment
Wijaya Edward

2005-08-01, 4:00 am

Hi John,

I was testing your code below with this array:

my @x = qw( * * A B C D );

but how come in the end it gives:
* XA * XB XC XD X

instead of
* * XA XB XC XD

--
Regards,
Edward WIJAYA
SINGAPORE

----- Original Message -----
From: "John W. Krahn" <krahnj@telus.net>
Date: Monday, August 1, 2005 11:03 am
Subject: Re: Remembering Positions in Array after processing the element

> Chris Charley wrote:
[color=darkred]
> without them.
>
> Yes, it will work with the three argument form of splice and as to
> yourother issue you can use unshift instead of push.
>
> unshift @y, [ $_, splice @x, $_, 1 ] if $x[ $_ ] =~ /[^A-Z]/;
>
>
>
> John
> --
> use Perl;
> program
> fulfillment
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


John W. Krahn

2005-08-01, 4:00 am

Wijaya Edward wrote:
> Hi John,


Hello,

> I was testing your code below with this array:
>
> my @x = qw( * * A B C D );
>
> but how come in the end it gives:
> * XA * XB XC XD X
>
> instead of
> * * XA XB XC XD


I guess you didn't see Chris' posting. :-)

$ perl -le'
@x = qw( * * A B C D );
print @x . " @x";
for ( reverse 0 .. $#x ) {
unshift @y, [ $_, splice @x, $_, 1 ] if $x[ $_ ] =~ /[^A-Z]/;
}
print @x . " @x";
@x = map "X$_", @x;
print @x . " @x";
for ( @y ) {
splice @x, $_->[0], 0, $_->[1];
}
print @x . " @x";

'
6 * * A B C D
4 A B C D
4 XA XB XC XD
6 * * XA XB XC XD



John
--
use Perl;
program
fulfillment
Chris Charley

2005-08-01, 4:00 am

Hello John

I don't feel very comfortable with some of the code I listed. I thought
that using @i and @j was a way to avoid warnings (as originally proposed by
Edward W.) when the array argument had bad data, but the same situation
occurs even with my solution. I guess the question is how would one want to
handle bad data - or even if it was likely that no bad data would ever be a
possibility. Also, could there be more than one "*" in the array. These
questions would need to be answered before one could make a final
determination on how to procede. :-(

Thanks for the tip on using unshift.

Chris


Wijaya Edward

2005-08-01, 4:00 am

Dear John,

Thanks so much for your snippet.
It's been really really helpful.

> I guess you didn't see Chris' posting. :-)


I'm really sorry.
Will try to be more attentive next time.

---
Regards,
Edward WIJAYA

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com