Is there a delete operator in Perl?

T

trashman.horlicks

Yes, I know theres one for hashes, but I was wondering if there was one
for elements in a linked list?
I have such a list, and it is being read sequentially, with new items
being placed either at the end, or the
middle of the list. Now, I know how to do this in C++, but I was
wondering if there was a way of deleting
used/old list elements? And is there a way to add an array element to
the middle of a list?
 
U

Uri Guttman

th> Yes, I know theres one for hashes, but I was wondering if there
th> was one for elements in a linked list? I have such a list, and it
th> is being read sequentially, with new items being placed either at
th> the end, or the middle of the list. Now, I know how to do this in
th> C++, but I was wondering if there was a way of deleting used/old
th> list elements? And is there a way to add an array element to the
th> middle of a list?

perl doesn't have linked lists in the lang. do you mean arrays? look at
perldoc -f splice which is likely what you want.

uri
 
T

trashman.horlicks

th> was one for elements in a linked list? I have such a list, and it
th> is being read sequentially, with new items being placed either at
th> the end, or the middle of the list. Now, I know how to do this in
th> C++, but I was wondering if there was a way of deleting used/old
th> list elements? And is there a way to add an array element to the
th> middle of a list?

perl doesn't have linked lists in the lang. do you mean arrays? look at
perldoc -f splice which is likely what you want.

Thanks - it sounds very much like what I need, particularly the delete
aspect of it.
I'm still a bit unsure as to how to introduce elements into an array,
so inserting "3" at element 3 in array {0,1,2,4} would give
{0,1,2,3,4}.
Could you give me an example please? :)
 
J

John Bokma

(e-mail address removed) wrote:

[ splice ]
I'm still a bit unsure as to how to introduce elements into an array,
so inserting "3" at element 3 in array {0,1,2,4} would give

That's /not/ an array.
{0,1,2,3,4}.
Could you give me an example please? :)

perldoc -f splice

look at the first in the "following equivalences" and reread what each
parameter means and why they have the value they have in that first one.
 
J

John W. Krahn

Thanks - it sounds very much like what I need, particularly the delete
aspect of it.
I'm still a bit unsure as to how to introduce elements into an array,
so inserting "3" at element 3 in array {0,1,2,4} would give
{0,1,2,3,4}.
Could you give me an example please? :)

$ perl -le'
my @array = ( 0, 1, 2, 4 );
print "@array";
splice @array, 3, 0, 3;
print "@array";
'
0 1 2 4
0 1 2 3 4




John
 
T

Ted Zlatanov

Yes, I know theres one for hashes, but I was wondering if there was
one for elements in a linked list? I have such a list, and it is
being read sequentially, with new items being placed either at the
end, or the middle of the list. Now, I know how to do this in C++,
but I was wondering if there was a way of deleting used/old list
elements? And is there a way to add an array element to the middle
of a list?

Look at perldoc -f splice to make modifications to a list in any
place.

push/pop and shift/unshift (again, use perldoc -f to look them up)
deal with adding and deleting elements at the beginning or end of a
list.

bugbear suggested these operations may be slow for large lists. I'd
say instead you should write your code to work correctly first, then
worry about optimizing for speed. You'd have to have really large
lists to notice slow performance.

Ted
 
X

xhoster

Ted Zlatanov said:
Look at perldoc -f splice to make modifications to a list in any
place.

push/pop and shift/unshift (again, use perldoc -f to look them up)
deal with adding and deleting elements at the beginning or end of a
list.

bugbear suggested these operations may be slow for large lists.

Not push/pop and shift/unshift so much as splice. (And splice near one
end or the other is pretty fast, too).
I'd
say instead you should write your code to work correctly first, then
worry about optimizing for speed.

While that is generally true, it sounds to me like he is implementing some
kind of fancy LRU cache. If that is the case, I'd say he is already in
the "optimizing for speed" stage.

Xho
 
T

Ted Zlatanov

While that is generally true, it sounds to me like he is implementing some
kind of fancy LRU cache. If that is the case, I'd say he is already in
the "optimizing for speed" stage.

No, it's *always* true you should not optimize prematurely. Correct
code is absolutely essential. At worst, you can use the correct but
slow code to write a test, and compare the results to the fast but
possibly buggy method you write subsequently.

Ted
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top