Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$_ $& $+ $` and the other $ 2

Status
Not open for further replies.

KeziaKristina

Programmer
Nov 1, 2002
13
ID
I am new comer in Perl, and i still confuse with the differences among $_ $& $+ and the other $. Anybody can help to explain that. Please with example so I can more understand.
Thanks.
 
_ refers to what is currently in memory, e.g. the currenmt iteration variable if this is not name, e.g when reading a file or looping over an array you can say things like
while(<IN>){....
#Here $_ play the role of
#each line in the file that is being looped is $_
# this means you do not really have fo say
my @array=<IN>; foreach my $line(@array){...}
#$_ takes the role of line

next, when you do pattern matching
eg.
my $string='abcAAAabcBBBabcCCC';
die unless $string=~/abc(.*)abc(.*)abc(.)/;
#now $1 is 'AAA', $2='BBB', $3='CCC;

next, if you have an array @a
$#a is the subscript of the last element of the array
(=number of elemnentss of the array -1), since array styarts at 0

$& returns the entire matched string
$+ returns the last bracket matched

There are also many more
$...
I suggest you get the camel book(programming perl) , which is
a must for any beginner and an extremely readable text. Make sure you do NOYT skip the first chapter

svar
 
perldoc perlvar

on the command line to view what you could otherwise read in the camel book on this subject.

--jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top