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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Beginner 1

Status
Not open for further replies.

syntax2

Programmer
Feb 4, 2009
1
EG
Hi

Can someone tell me what '$'tands for in perl?i am basically an ASP Programmer and was just working on a couple of perl files.I came across many instances of the '$' being used with some filename or somethingafter.
like this... $r_home.what does this mean?

Thanks in advance.
 
I happened to be writing an informal 'how-to' for some of my co-workers. The following is valid code that will run and generate output demonstrating each of the syntaxes used. I hope this helps.

Code:
#!perl
#Perl "Memory Structures" Frequently Asked Questions
# from the simple to the more complex.....

###############  $string   #######################
# a simple scalar variable is denoted with a $
# a single equal sign assigns the value on the right side
# to the variable on the left side.

    $name = 'joe';    # the variable labelled 'name' contains the string 'joe'
    $who = $name;     # now, $who contains 'joe', just like $name


###############  @arrays  ########################
# an array (list) of string variables,(maybe names) is denoted with an @

    @names = ('joe','jane','robert','phillip');

# refering to one element of an array
# note that we count from zero, not from one.

    $who1 = $names[0];
    $who2 = $names[1];
    $who3 = $names[2];
    $who4 = $names[3];
    # the [3], refers to 'the fourth element of the 
    # array labelled, names'.  Also, note the use of
    # '$' in the array label because we are refering
    # to one element of the array.
    print "who1 - $who1\n";
    print "who2 - $who2\n";
    print "who3 - $who3\n";
    print "who4 - $who4\n\n";

# changing an element in an array

    @names = ('joe','jane','robert','phillip');
    $names[2] = 'rob'; # robert prefers his nickname
    print "Changed robert's name\n";
    foreach (@names) { print "$_ \n"; }
    
# build an array from two others.

    @names = '';
    @names2 = ('joe','jane');
    @names3 = ('robert','phillip');
    @names = (@names2,@names3);
    print "\nJoined 2 arrays into 1.\n";
    foreach (@names) { print "$_ \n"; }
    
    
# push a new element onto an array

    push @names,helen;
    print "\nAdded helen to array\n";
    foreach (@names) { print "$_ \n"; }

# remove/catch last element from array

    $lastOne = pop(@names);
    print "\n$lastOne is no longer part of the array.\n";
    foreach (@names) { print "$_ \n"; }
    
# remove and catch the first element of an array

    $firstOne = shift(@names);
    print "\nFirst name was $firstOne, is now $names[0]\n";
    foreach (@names) { print "$_ \n"; }
    
# put a new element on the front of the list
# where push adds the element to the end unshift puts the new
# element first.

    unshift @names,'joe';
    print "\nPut first element back in first slot.\n";
    foreach (@names) { print "$_ \n"; }
    
# delete an element in an array

    undef $names[2];
    print "\nDeleted the third element\n";
    foreach (@names) { print "$_ \n"; }

###############  %hashes  ########################
# a hash, or associative array, is simply a list of
# name/value pairs.  A telephone book is a list of 
# name/value pairs.  For each key (name-address) there
# is a value (phone number).

%phone_numbers = (
    'joe','573-1668',
    'jane','573-1543',
    'rob','123-4567',
    'phillip','234-5678'
    );

# print rob's number by refering to it with it's key (rob).

print "\nRob's number is -> $phone_numbers{'rob'}\n";

# Note that when refering to one value in a hash, you 
# use a '$' on the front (after all, you are asking for a 
# single value here, not the entire hash).
# Also, note the braces instead of the brackets that were
# used with the arrays.

# Change a value in a hash
# Rob's number is wrong, fix it

$phone_numbers{'rob'} = '234-4567';
print "\nFixed Rob's number -> $phone_numbers{'rob'}\n";

# to add a new name/value pair to a hash, simply set the value.

$phone_numbers{'mike'} = '987-6543';

# get all of the keys in a hash using the 'keys' function

@who = keys(%phone_numbers);
print "\nkeys in hash are -> @who\n";

# print phone numbers for everyone in the hash

foreach $who (keys(%phone_numbers))
    {
    print "NAME - $who\tNUMBER - $phone_numbers{$who}\n";
    }


keep the rudder amid ship and beware the odd typo
 
Sorry that looks so jumbled. You should be able to undo a few line wraps and get it back in working order fairly easily. :cool:


keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top