Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^5: reverse a string in place

by stevieb (Abbot)
on Jun 28, 2015 at 15:36 UTC ( #1132390=note: print w/replies, xml ) Need Help??


in reply to Re^4: reverse a string in place
in thread reverse a string in place

Here's a couple very common example use cases for shift, unshift, push. The @INC array is special in Perl, and is used to store directories where libraries are housed. To add a custom location, it's common to unshift @INC

The most common use of shift is extracting parameters from the special @_ array within a subroutine. @_ in Perl is the default array, and is implicitly used to store all parameters going into a subroutine. In the shift line I don't say shift @_;, because the @_ is implicit.

In the last example, I use push to add elements to a new array based on contents from a different array.

#!/usr/bin/perl # unshift print "$_\n" for @INC; unshift @INC, '/home/steve/my_lib_dir'; print "$_\n" for @INC; # shift parameter("Hello, world!"); sub parameter { my $param = shift; print "$param\n"; } # push my @numbers = qw(1 2 3 4 5); my @totals; for my $elem (@numbers){ push @totals, $elem + 5; } print "$_\n" for @totals;

-stevieb

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1132390]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-05-24 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?