Perl
  Home arrow Perl arrow Page 7 - Perl 101 (Part 3) - Looping The Loop
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

Perl 101 (Part 3) - Looping The Loop
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2000-06-15


    Table of Contents:
  • Perl 101 (Part 3) - Looping The Loop
  • While You Were Sleeping...
  • ...Or Until You Wake Up
  • Dos And Don'ts
  • For Pete's Sake!
  • Every Comedian Needs An Exit
  • Grade School
  • Playing With Friends
  • So Many Choices...

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Perl 101 (Part 3) - Looping The Loop - Grade School
    ( Page 7 of 9 )

    Before we go on to our last - also known as final - control structure of the day, we're going to take a quick detour and introduce you to a different kind of variable.

    As you already know, Perl comes with scalar variables, which can be used to store a single value. But what you may not know is that it also comes with a mechanism to store multiple values in a single variable. This variable is known as an "array variable", and it is a useful method of storing and representing related information.

    All the standard rules which apply when naming scalar variables also apply in the case of array variables, with one important exception - where a scalar variable name is preceded by a dollar [$] sign, an array variable name is preceded by an @ symbol.

    Note also that Perl does not place any restrictions on array and scalar variables sharing the same name in Perl - so the array @stuff is different from the scalar $stuff.

    Now, let's say that you wanted to create an array containing the names of your friends:

    @friends = ("Rachel", "Monica", "Phoebe", "Chandler", "Joey", "Ross");
    Thus the array variable @friends contains six elements.

    Each element of the array is a scalar variable, and can be accessed using scalar notation, with a suffix denoting the element's position in the array. So, in order to extract the first element of the array @friends, you'd use

    $friends[0]
    while

    $friends[5]
    would give you the sixth element of the array, Ross.

    Similarly, if you wanted to modify a particular element of the array, you could use the scalar notation above to accomplish your task, like this:

    $friends[3] = "Janice";
    Your array would now contain

    ("Rachel", "Monica", "Phoebe", "Janice", "Joey", "Ross");
    Note that the first element of an array is always referred to by the index 0 - this concept, known as "zero-based indexing" often confuses novice programmers, and is just one more reason why geeks have so few friends.

    An array can contain both string and numeric data - for example, this is perfectly valid:

    @mix = ("hello", 34747, 3, "bonjour");
    How about a quick example to illustrate how data can be stored in a single array variable:

    #!/usr/bin/perl # this example demonstrates how a single # array variable can hold a student's gradea # in six subjects # set up some variables @subjectlist = ("Math", "Lit.", "Physics", "Biology"); @gradelist = (); $total = 0; # get and store input for($x=0; $x<4; $x++) { print("What was your grade in $subjectlist[$x]? "); $gradelist[$x] = <STDIN>; chomp($gradelist[$x]); } # display it in neatly formatted rows print ("SUBJECT\t\tGRADE\n"); for($y=0; $y<4; $y++) { print ("$subjectlist[$y]\t\t$gradelist[$y]\n"); $total += $gradelist[$y]; } # print a total print ("TOTAL: $total\n");
    And here's what it looks like:

    What was your grade in Math? 10 What was your grade in Lit.? 20 What was your grade in Physics? 30 What was your grade in Biology? 40 SUBJECT GRADE Math 10 Lit. 20 Physics 30 Biology 40 TOTAL: 100
    Let's walk you through this: we've begun by initializing two array variables and one scalar variable. The array @subjectlist contains a list of the subjects to be graded, and the array @gradelist will be populated by the user with the actual grades. The scalar variable $total will be used to display a total figure at the end.

    Next, we've used a "for" loop to display a question, and assign the user's input to the @gradelist array in the appropriate slot via the $x counter. Once all four subjects are taken care of [note the conditional expression in the "for" loop, which automatically stops looping when the counter reaches 4], we've simply used the print() function and a second "for" loop to display a neatly-tabulated row of grades and subjects.

    The second loop also adds each grade to the variable $total, and this displays this total value at the end of the program.

    A couple of other points to note:

    * The \t character used in the print() statements above is used to generate a single "tab" space.

    * The notation

    $total += $gradelist[$y];

    is simply a Perl shortcut for

    $total = $total + $gradelist[$y];

    This article copyright Melonfire 2000. All rights reserved.

     
     
    >>> More Perl Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek