Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Dealing with Lists and Arrays

by @rocks (Scribe)
on Nov 10, 2002 at 23:35 UTC ( #211821=perlmeditation: print w/replies, xml ) Need Help??

Hello, recently in my perl programming hobby, I have found some pretty tricky things done by the pdevs. (I am talking about Mr. Schwartz and Mr. Phoenix) One of these is the  shift and unshift command structures compared to the push and pop command structures. I would like to praise the creators (all creators) of perl 5.6.1 for thinking of shiftand unshift. I don't know much about the earlier version(s) and in what version these commands started in. Any info about this subject would be great.

But, the reason I am talking about these 2 commands is because recently a programmer told me there is no way to add on to the front of a list or array. I didn't believe this so I went into the blessed book, Learning Perl 3rd. I was happy to see that Mr. Schwartz and Mr. Phoenix included ways of adding on to the front of lists and arrays; also know as shift and unshift.

I would like to take a moment and thank these great pdevs for writing the great Learning Perl Series. These great books allowed me to gain honor in the eyes of programmers that I can show, even at 13, I can correct you at least once thanks to these guys. :-) Thanks Perl.

-@rocks

Replies are listed 'Best First'.
Re: Dealing with Lists and Arrays
by chromatic (Archbishop) on Nov 11, 2002 at 01:35 UTC

    push, pop, shift and unshift were all present in Perl 1, released in 1987. It's surprising how many features it had.

    Be careful not to confuse lists and arrays, though, as you can't use any of these operators to modify a list.

Re: Dealing with Lists and Arrays
by Zaxo (Archbishop) on Nov 10, 2002 at 23:52 UTC

    You may be interested in learning splice, map, and grep as well. Perl is truly rich in methods of manipulating lists and arrays.

    Pronouncements like your programming friend made about what can be done with lists usually have a hidden assumption about how the list is implemented at low level. That could be linked nodes, fixed records adjacent in memory, or some flavor of tree. Each has strengths and weaknesses in the fundamental operations of insertion, deletion, searching, and indexing.

    After Compline,
    Zaxo

Re: Dealing with Lists and Arrays
by talexb (Chancellor) on Nov 11, 2002 at 14:43 UTC

    I'm not sure that shift and unshift are so amazingly unique. After all, it's possible to simulate their behaviour with push and pop

    Hmm, after tinkering quickly with some code, this is all I could come up with.

    #!/usr/bin/perl -w my @data = qw/1 2 3 4 5/; print "Data is " . join ( ":", @data ) . "\n"; @data = reverse @data; my $foo = pop @data; @data = reverse @data; print "Data is " . join ( ":", @data ) . "\n"; @data = reverse @data; push ( @data, 17 ); @data = reverse @data; print "Data is " . join ( ":", @data ) . "\n";

    So maybe shift and unshift are useful after all. Is there an easier way to simulate the behaviour that I've missed?

    --t. alex
    but my friends call me T.

      It's not only a matter of merely simulating the behavior of shift and unshift, it may have a lot to do with execution efficiency since a reverse is an expensive operation. It involves creating a new list and populating it with the elements of the original in O(n) time (n = length of the list).

      A shift is typically O(1) time since it involves just taking out the first element of which you have the address anyway -- it's the start of the list -- and making the second element the first. The same holds for unshift which is a simple insertion at the beginning of the list.

      So for reasons of efficiency, you really want shift and unshift, not only to make code look more elegant.

      Hope this helps, -gjb-

Re: Dealing with Lists and Arrays
by perrin (Chancellor) on Nov 11, 2002 at 19:02 UTC
    Just a quick note about your post: you should only link to nodes that already exist. Your links to "pdevs", "Mr. Schwartz", "Mr. Phoenix", etc. are all broken.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://211821]
Approved by hossman
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2022-10-04 19:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (18 votes). Check out past polls.

    Notices?