Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   List questions on form (http://www.velocityreviews.com/forums/t886340-list-questions-on-form.html)

Roger 05-06-2004 05:43 PM

List questions on form
 
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.

Chief S. 05-06-2004 06:37 PM

Re: List questions on form
 
Roger wrote:
> 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.

--
Chief S.


Gunnar Hjalmarsson 05-06-2004 06:55 PM

Re: List questions on form
 
Roger wrote:
>


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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Roger 05-06-2004 11:36 PM

Re: List questions on form
 
"Chief S." <chiefS@edu.edu> wrote in message
>
> 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/Arra...ion-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

Roger 05-06-2004 11:45 PM

Re: List questions on form
 
Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<2fvg9jF2ql5fU1@uni-berlin.de>...
> Roger wrote:
> >

>
> 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

Gunnar Hjalmarsson 05-06-2004 11:50 PM

Re: List questions on form
 
Roger wrote:
> "Chief S." <chiefS@edu.edu> wrote in message
>>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'.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Joe Smith 05-07-2004 10:48 AM

Re: List questions on form
 
Roger wrote:

> "Chief S." <chiefS@edu.edu> wrote in message
>
>>Why 17? Are you understanding splice() correctly?

>
> 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

Roger 05-08-2004 04:09 AM

Re: List questions on form
 
Joe Smith <Joe.Smith@inwap.com> wrote in message
>
> 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


All times are GMT. The time now is 02:14 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.