Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > List questions on form

Reply
Thread Tools

List questions on form

 
 
Roger
Guest
Posts: n/a
 
      05-06-2004
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.
 
Reply With Quote
 
 
 
 
Chief S.
Guest
Posts: n/a
 
      05-06-2004
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.

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-06-2004
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

 
Reply With Quote
 
Roger
Guest
Posts: n/a
 
      05-06-2004
"Chief S." <> 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
 
Reply With Quote
 
Roger
Guest
Posts: n/a
 
      05-06-2004
Gunnar Hjalmarsson <> wrote in message news:<>...
> 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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-06-2004
Roger wrote:
> "Chief S." <> 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

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      05-07-2004
Roger wrote:

> "Chief S." <> 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
 
Reply With Quote
 
Roger
Guest
Posts: n/a
 
      05-08-2004
Joe Smith <> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to retrieve form field value if form is EncType=multipart/form-dataForm? Li Zhang ASP .Net 4 02-27-2009 01:23 AM
how to transform a list form of hyphens to an html unordered list ibiza ASP .Net 5 02-09-2006 04:10 PM
<form>...</form> - how to supress blank space after </form> in IE? rob c Javascript 4 12-30-2005 01:18 PM
Difference Between List x; and List x(); , if 'List' is a Class? roopa C++ 6 08-27-2004 06:18 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM



Advertisments