List questions on form

R

Roger

Here's the code, a comment at the end about what I'm trying to do.


@list = ("w","d","e","e","d","r","f","e","d",
"c","v","g","h","t","y","h","g","t","h","y","D","P");


while( @list ) # just debugging here
{
@end = @list[16..19];
splice (@list,16,17, "Z", "X"); # after this 18..21 are undef
splice(@list ,18,1, @end);
($Fld1) = "|" . join "|", @list ;
($Fld1) .= "|";
print $Fld1;
}

#The basic idea here is to take a list, remove some values in the middle
#and replace them with some other values, and then make the entire record
#pipe delimited, beginning to end.
#It might be a hash would be a better choice, but my 'expertise' does
#not yet reach to that data type
#I have accomplished the tasks mentioned but it's real, real ugly, I know.
#Obviously I'm very new to Perl, thanks for any help.
 
C

Chief S.

Roger said:
Here's the code, a comment at the end about what I'm trying to do.
@list = ("w","d","e","e","d","r","f","e","d",
"c","v","g","h","t","y","h","g","t","h","y","D","P");

A cleaner way to do this sort of thing:

@list = qw(w d e e d r f e d c v g h t y h g t h y D P);
@end = @list[16..19];
splice (@list,16,17, "Z", "X"); # after this 18..21 are undef

Why 17? Are you understanding splice() correctly?
splice(@list ,18,1, @end);
($Fld1) = "|" . join "|", @list ;
($Fld1) .= "|";
print $Fld1;

One join() will work:

$Fld1 = join('|', '', @list, '');
#The basic idea here is to take a list, remove some values in the middle
#and replace them with some other values, and then make the entire record
#pipe delimited, beginning to end.

It's not clear exactly what you're trying to do, but this seems like
something achievable with a single splice() command.
 
G

Gunnar Hjalmarsson

Something's missing here:

use strict;
use warnings;
@list = ("w","d","e","e","d","r","f","e","d",
"c","v","g","h","t","y","h","g","t","h","y","D","P");

Better written as

my @list = qw/w d e e d r f e d c v g h t y h g t h y D P/;
while( @list ) # just debugging here
{

A while loop like that makes an endless loop. Take it away.
@end = @list[16..19];
splice (@list,16,17, "Z", "X"); # after this 18..21 are undef

You should study the documentation for splice():

perldoc -f splice

One thing you'll notice is that the third argument is the number of
elements to be removed. Your use of 17 indicates that you have
misunderstood that.
splice(@list ,18,1, @end);

The push() function would be more suitable here.

perldoc -f push

This is an easier way to accomplish the exchange of elements:

my @end = splice @list, 16;
push @list, 'Z', 'X', @end[0..3];
($Fld1) = "|" . join "|", @list ;
($Fld1) .= "|";
print $Fld1;

No need to concatenate the parts into a scalar variable.

print '|', (join '|', @list), "|\n";
#It might be a hash would be a better choice, but my
#'expertise' does not yet reach to that data type

Can't see how a hash would be better.
 
R

Roger

Chief S. said:
Why 17? Are you understanding splice() correctly?
@end = @list[16..19];
splice (@list,16,17, "Z", "X"); # after this 18..21 are undef

on account of ...
# remove elements 1 and 2
# replace with new values
splice (@rainbow, 1, 2, "yellow", "orange");

I got the above line of code from:
http://www.devshed.com/c/a/Perl/Array-Manipulation-in-Perl/7/

just trying to learn here. From the above splice example it looks like
you could go...
splice (@myarray, 1,2,3,4,5,"valfor1","valfor2","valfor3", "valfor4",
"valfor5";
but that does not seem to work.

I have read the docs on split but the above example sort of got me on
the wrong path. Thanks for your help. I'm still working on it.
Roger
 
R

Roger

Gunnar Hjalmarsson said:
Something's missing here:

use strict;
use warnings;
@list = ("w","d","e","e","d","r","f","e","d",
"c","v","g","h","t","y","h","g","t","h","y","D","P");

Better written as

my @list = qw/w d e e d r f e d c v g h t y h g t h y D P/;
while( @list ) # just debugging here
{

A while loop like that makes an endless loop. Take it away.
@end = @list[16..19];
splice (@list,16,17, "Z", "X"); # after this 18..21 are undef

You should study the documentation for splice():

perldoc -f splice

One thing you'll notice is that the third argument is the number of
elements to be removed. Your use of 17 indicates that you have
misunderstood that.
splice(@list ,18,1, @end);

The push() function would be more suitable here.

perldoc -f push

This is an easier way to accomplish the exchange of elements:

my @end = splice @list, 16;
push @list, 'Z', 'X', @end[0..3];
($Fld1) = "|" . join "|", @list ;
($Fld1) .= "|";
print $Fld1;

No need to concatenate the parts into a scalar variable.

print '|', (join '|', @list), "|\n";
#It might be a hash would be a better choice, but my
#'expertise' does not yet reach to that data type

Can't see how a hash would be better.


Thanks for your reply I am studying it. I thought I understood splice,
will have to read more.
Roger
 
G

Gunnar Hjalmarsson

Roger said:
Chief S. said:
Why 17? Are you understanding splice() correctly?
@end = @list[16..19];
splice (@list,16,17, "Z", "X"); # after this 18..21 are undef

on account of ...
# remove elements 1 and 2
# replace with new values
splice (@rainbow, 1, 2, "yellow", "orange");

Yes: 'Remove 2 elements, starting with element 1'.
 
J

Joe Smith

Roger said:
on account of ...
# remove elements 1 and 2
# replace with new values
splice (@rainbow, 1, 2, "yellow", "orange");

That's an unfortunately ambiguous example.
Maybe this will show where you are misunderstanding it.

# remove 2 elements; 7 and 8
# replace with new values
splice (@rainbow, 7, 2, "yellow", "orange");

-Joe
 
R

Roger

Joe Smith said:
That's an unfortunately ambiguous example.
Maybe this will show where you are misunderstanding it.

# remove 2 elements; 7 and 8
# replace with new values
splice (@rainbow, 7, 2, "yellow", "orange");

-Joe

It is an unfortunately ambiguous example. What was confusing was the
LENGTH part. For me, the best example (where it would be obvious)
would be a LENGTH of 0 so you were inserting items into the list.
That's the hard part. Items must be shuffeled when that happens. Thats
the hard part. I was thinking LENGTH was how many of the new items to
add.
splice (@rainbow, 7, 0, "yellow", "orange");
inserts items into the list but doesn't screw the list up. PLus it's a
little funky the subscripts are really zero based, but are not
reflected in the call to splice.
Thanks a bunch for your input, you knew what was messing my mind up!

Roger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top