CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
String/Array functions

 



simon
Deleted

Jul 8, 2000, 12:42 AM

Post #1 of 3 (689 views)
String/Array functions Can't Post

Could someone write a "simple" example for each of these questions.
Write a program that shows what the shift() and unshift() fuNCtions do.

Write a program that shows what the push() and pop() fuNCtions do.

thanks
-simon


gvh
Deleted

Jul 8, 2000, 11:23 AM

Post #2 of 3 (689 views)
Re: String/Array functions [In reply to] Can't Post

#! perl -w

use strict;

# simple script to show the effect of
# push pop shift and unshift

# let's create an array with some elements

my @animals = ("horse", "cow", "bird", "goat", "dog");
print "We're starting with: @animals\n";

push @animals, "butterfly";

print "\nWe've pushed a butterfly : @animals\n";

my $popped = pop @animals;

print "\nWe've popped : @animals\n";
print "and this is what we popped : $popped\n";

my $shifted = shift @animals;

print "\nWe've shifted now : @animals\n";
print "and this is what we got : $shifted\n";

my $item = "mockingbird";
unshift @animals, ($item);

print "\nWe've unshifted a $item : @animals\n";

print <<eind;

To summarize:

push : puts an extra element at the right end of a list
pop : retrieves and removes the rightmost element of a list
shift : retrieves and removes the leftmost element from a list
unshift : adds a new element at the left side of a list

If the list is not mentionned, @ARGV is used at program level
and @_ inside subroutines.

I hope that this clarified things.

Guido

eind


simon
Deleted

Jul 8, 2000, 9:45 PM

Post #3 of 3 (689 views)
Re: String/Array functions [In reply to] Can't Post

Thanks bud.
Really appracite it. Great example for someone at my level =).
take care..
-simon

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives