Hello and welcome to the Webmasters Forums!. This is the best place to get webmasters resources for free. Get $2 for free today, read more - Make your payment today. Download premium and professional templates for free. Get free web hosting without ads, read more. You can get lot more by simply join with this forum. To gain full access to the forums you must sign up for a free account.


Post Reply  Post Thread 

Literals and Operators

Post Bank
Posting Manager
******

Posts: 995
Group: Forum Team
Joined: Sep 2006
Status: Online
Make money from now. You can make money just for posting on this forum. Every discussions on this community gives you more money. $2 minimum payout. So get your payment today, SignIn with this forum.

Signin to Remove this Post

bomber
Junior Member
*


Posts: 34
Group: Registered
Joined: Sep 2006
Status: Offline
Reputation: 0
Points: 250 (Donate)
Post: #1

Smile Literals and Operators


Example: Numbers and Characters

Code:
#!/usr/local/bin/perl
    print '007',' has been portrayed by at least ', 004, ' actors. ';
    print 7+3, ' ', 7*3, ' ', 7/3, ' ', 7%3, ' ', 7**3, ' ';
    $x = 7;
    print $x;
    print '   Doesn\'t resolve variables like $x and backslashes \n. ';
    print "Does resolve $x and backslash\n";
    $y = "A line containing $x and ending with line feed.\n";
    print $y;
    $y = "Con" . "cat" . "enation!\n";
    print $y;


This produces:

007 has been portrayed by at least 4 actors. 10 21
2.3333333333333335 1 343 7
Doesn't resolve variables like $x and backslashes \n. Does
resolve 7 and backslash
A line containing 7 and ending with line feed.
Concatenation!

Questions:

1. Why does the output from the first few print statements run together?
2. Is it necessary to declare variables in Perl? Is it possible?

Example: Comparisons

Code:
# The following "<<" variation of
    # data input simplifies CGI forms.
    $x = 'operator';
    print <<THATSALL;
    A common mistake:  Confusing the assignment $x =
    and the numeric comparison $x ==, and the character
    comparison $x eq.
    THATSALL
    $x = 7;
    if ($x == 7) { print "x is $x\n"; }
    if ($x = 5)  {
        print "x is now $x,",
        "the assignment is successful.\n";
    }
    $x = 'stuff';
    if ($x eq 'stuff') {
        print "Use eq, ne, lt, gt, etc for strings.\n";
    }


This produces:

A common mistake: Confusing the assignment operator =
and the numeric comparison operator ==, and the character
comparison operator eq.
x is 7
x is now 5, the assignment is successful.
Use eq, ne, lt, gt, etc for strings.

Example: Ordinary Arrays

Code:
@stuff = ('This', 'is', 'a', 'list.');
    print "Lists and strings are indexed from 0.\n";
    print "So \$stuff[1] = $stuff[1], ",
         "and \$#stuff = $#stuff.\n";
    print @stuff,"\n";
    print join('...',@stuff),"\n";
    splice(@stuff, 3, 0, ('fine', 'little'));
    print join('...',@stuff),"\n";


This produces:

Lists and strings are indexed from 0.
So $stuff[1] = is, and $#stuff = 3.
Thisisalist.
This...is...a...list.
This...is...a...fine...little...list.

Homework: Validate a date.

Code:
#!/usr/local/bin/perl
    print "Enter numeric:  month  day  year\n";
    $_ = <STDIN>;
    ($month,$day,$year) = split;


Complete this program. Print an error message if the month is not valid. Print an error message if the day is not valid for the given month (31 is ok for January but not for February). See if you can avoid using conditionals (if, unless, ?,...) statements but instead use data structures.

Approach this incrementally. On the first draft, assume that the user enters 3 numbers separated by spaces and that February has 28 days. Subsequent refinements should account for bad input and leap year. Finally, find a Perl builtin function that converts a date to system time, and see how to use that to validate time data generally.
Homework: Play with associative arrays.

Start with a few assignments like:

$name{12345} = 'John Doe';
$name{24680} = 'Jane Smith';

Print these scalars. What is the value of an associative array element that has never been assigned? What happens if you assign an associative array to a scalar? What happens if you assign an associative array to a normal array?

Code:
$blunk = %name;
    @persons = %name;
    print '$blunk=',$blunk,', @persons=',
        join(', ',@persons),"\n";


What happens if you assign a normal array to an associative array?

19-09-2006 12:19 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:

Forum Jump:

Sign In to Remove Ads

Download 1000's of web templates. Unlimited access!
World's Best Web Hosting
Website of the Month

Create-a-Page for Free
SOTM June 2008


Accepting Submissions
for July 2008
Resources

Recommended Sites:



Visit our Sponsors!

Current time: 08-10-2008, 01:32 AM


Copyright © 2002-2008 MyBB Group
Powered By MyBB