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

N

ngoc

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
 
J

John W. Krahn

ngoc said:
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
 
M

Mintcake

John said:
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.
 
D

Dave

Mintcake said:
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'.
 
T

Tad McClellan

Mintcake said:
John W. Krahn wrote:
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.
 

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,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top