List questions on form

Discussion in 'Perl Misc' started by Roger, May 6, 2004.

  1. Roger

    Roger Guest

    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.
    Roger, May 6, 2004
    #1
    1. Advertising

  2. Roger

    Chief S. Guest

    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.
    Chief S., May 6, 2004
    #2
    1. Advertising

  3. 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
    Gunnar Hjalmarsson, May 6, 2004
    #3
  4. Roger

    Roger Guest

    "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/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
    Roger, May 7, 2004
    #4
  5. Roger

    Roger Guest

    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
    Roger, May 7, 2004
    #5
  6. 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
    Gunnar Hjalmarsson, May 7, 2004
    #6
  7. Roger

    Joe Smith Guest

    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
    Joe Smith, May 7, 2004
    #7
  8. Roger

    Roger Guest

    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
    Roger, May 8, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Li Zhang
    Replies:
    4
    Views:
    6,045
    softip
    Feb 27, 2009
  2. ibiza
    Replies:
    5
    Views:
    1,159
    S. Justin Gengo [MCP]
    Feb 9, 2006
  3. roopa
    Replies:
    6
    Views:
    727
    Jerry Coffin
    Aug 27, 2004
  4. rob c
    Replies:
    4
    Views:
    311
    McKirahan
    Dec 30, 2005
  5. Ravi Prabakaran
    Replies:
    8
    Views:
    101
    Terry Reedy
    Dec 16, 2013
Loading...

Share This Page