List Util insert_at(index, value), delete_at(index)

Discussion in 'Perl Misc' started by ngoc, May 11, 2006.

  1. ngoc

    ngoc Guest

    Hi
    Searching around CPAN to find abstract method for insert and delete
    element in a List or Array, but did not find.
    Searching for something like insert_at(index, value), delete_at(index)
    Thanks
    ngoc
    ngoc, May 11, 2006
    #1
    1. Advertising

  2. ngoc wrote:
    >
    > Searching around CPAN to find abstract method for insert and delete
    > element in a List


    A list is a literal and cannot be modified at run time.

    perldoc -q "What is the difference between a list and an array"

    > or Array,


    perldoc -f splice

    > but did not find.
    > Searching for something like insert_at(index, value),


    splice @array, $index, 1, 'value';

    > delete_at(index)


    splice @array, $index, 1;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, May 11, 2006
    #2
    1. Advertising

  3. ngoc

    Anno Siegel Guest

    John W. Krahn <> wrote in comp.lang.perl.misc:
    > ngoc wrote:
    > >
    > > Searching around CPAN to find abstract method for insert and delete
    > > element in a List


    [...]

    > > but did not find.
    > > Searching for something like insert_at(index, value),

    >
    > splice @array, $index, 1, 'value';


    splice @array, $index, 0, 'value';

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, May 11, 2006
    #3
  4. ngoc

    Mintcake Guest

    John W. Krahn wrote:
    > A list is a literal and cannot be modified at run time.
    >
    > perldoc -q "What is the difference between a list and an array"


    A list is not necessarily a literal.

    I looked a the perldoc mentioned and saw the following:

    As a side note, there's no such thing as a list in scalar
    context.
    When you say

    $scalar = (2, 5, 7, 9);

    you're using the comma operator in scalar context, so it uses the
    scalar comma operator. There never was a list there at all!
    This
    causes the last value to be returned: 9.

    Can someone explain what is happening here...

    perl -le '$scalar = () = (2, 5, 7, 9); print $scalar'

    This prints 4. I've never quite understood why.
    Mintcake, May 11, 2006
    #4
  5. ngoc

    Dave Guest

    "Mintcake" <> wrote in message
    news:...
    >
    > John W. Krahn wrote:
    >> A list is a literal and cannot be modified at run time.
    >>
    >> perldoc -q "What is the difference between a list and an array"

    >
    > A list is not necessarily a literal.
    >
    > I looked a the perldoc mentioned and saw the following:
    >
    > As a side note, there's no such thing as a list in scalar
    > context.
    > When you say
    >
    > $scalar = (2, 5, 7, 9);
    >
    > you're using the comma operator in scalar context, so it uses the
    > scalar comma operator. There never was a list there at all!
    > This
    > causes the last value to be returned: 9.
    >
    > Can someone explain what is happening here...
    >
    > perl -le '$scalar = () = (2, 5, 7, 9); print $scalar'
    >
    > This prints 4. I've never quite understood why.


    It is equivalent to
    perl -le '$scalar = ( () = (2, 5, 7, 9) ); print $scalar'

    Camel book p75
    'List assignment in scalar context returns the number of elements produced
    by the expression on the right side of the assignment'.
    Dave, May 11, 2006
    #5
  6. Mintcake <> wrote:
    > John W. Krahn wrote:


    >> perldoc -q "What is the difference between a list and an array"


    > you're using the comma operator in scalar context,


    > Can someone explain what is happening here...



    First let me point out that the below has nothing to do with
    the comma operator nor with the distinction between a list
    and an array.


    > perl -le '$scalar = () = (2, 5, 7, 9); print $scalar'
    >
    > This prints 4. I've never quite understood why.



    () = (2, 5, 7, 9)

    is a "list assignment".

    This list assignment is in scalar context because its value
    is being assigned to a scalar:

    $scalar = <list assignment>

    So you store whatever the value of a list assignment in scalar context
    is to $scalar.

    The value of a list assignment in scalar context is the number
    of elements in the right hand side of the list assignment.

    This is documented in the "List value constructors" section in perldata.pod.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 11, 2006
    #6
    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. E Tepp

    Pause Util Value is returned

    E Tepp, Dec 15, 2003, in forum: Java
    Replies:
    2
    Views:
    433
    Matthew Zimmer
    Dec 15, 2003
  2. Replies:
    1
    Views:
    1,110
  3. SadaYama
    Replies:
    2
    Views:
    341
  4. Bil Kleb
    Replies:
    9
    Views:
    130
    Bil Kleb
    Jul 2, 2009
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    284
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page