Perl
  Home arrow Perl arrow Page 7 - Perl 101 (Part 4) - Mind Games
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 4) - Mind Games
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2000-06-29


    Table of Contents:
  • Perl 101 (Part 4) - Mind Games
  • Handle With Care
  • Different Strokes
  • A Little Brainwashing
  • Die! Die! Die!
  • Testing Times
  • Popguns And Pushpins
  • Shifting Things Around
  • The Real World
  • Miscellaneous Stuff

  • 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 4) - Mind Games - Popguns And Pushpins
    ( Page 7 of 10 )

    If you've been paying attention, you should now know the basics of reading and writing files in Perl. And as you've seen, reading a file into an array is a simple and efficient way of getting data from an external file into your Perl program. But once you've got it in, what do you do with it?

    Well, over the next couple of pages, we're going to be taking a look at some of Perl's most useful array functions - these babies will let you split, join and re-arrange array elements to your heart - followed by a real-life example that should help you put it all in context.

    Let's start with a basic example:
    #!/usr/bin/perl
    
    # open file and define a handle for it
    open(MIND,"thoughts.txt") || die "Unable to open file!\n";
    
    # suck the file into an array
    @file = <MIND>;
    
    # close file when done
    close(MIND);
    
    # use a loop to keep reading the file
    # until it reaches the end
    foreach $line (@file)
           {
    	print $line;
           }

    At this point, the contents of file "thoughts.txt" are stored in the array @file. Now, let's add some code that asks the user for his thoughts on what he's just seen, adds those comments to the end of the array, and writes the whole shebang back to the file "thoughts.txt"
    #!/usr/bin/perl
    
    # open file and define a handle for it
    open(MIND,"thoughts.txt") || die("Unable to open file!\n");
    
    # suck the file into an array
    @file = <MIND>;
    
    # close file when done
    close(MIND);
    
    # use a loop to keep reading the file
    # until it reaches the end
    foreach $line (@file)
    	{
    	print $line;
    	}
    
    # ask for input and process it
    print "What do you think?\n";
    $comment = <STDIN>;
    
    # add comment to end of array
    push (@file, $comment);
    
    # open file for writing
    open(MIND,">thoughts.txt") || die("Unable to open file!\n");
    
    # print array back into file
    foreach $line (@file)
            {
    	print MIND $line;
    	}
    
    # close file when done
    close(MIND);

    The important thing to note here is the push() function, which adds an element - the user's input - to the end of an array. The entire array is then written back to the file "thoughts.txt", destroying the original contents in the process. So, if you were to run
    $ cat thoughts.txt

    at your shell, you'd see
    We're running out of space on planet Earth.
    Scientists are attempting to colonize Mars.
    I have a huge amount of empty real estate in my mind.
    Imagine if I could rent it to the citizens of Earth for a nominal monthly
    fee.
    Would I be rich? Or just crazy?
    This idea sucks!

    Now, how about removing that last line? Simple - use the pop() function to remove the last item from the array:
    #!/usr/bin/perl
    
    # open file and define a handle for it
    open(MIND,"thoughts.txt") || die("Unable to open file!\n");
    
    # suck the file into an array
    @file = <MIND>;
    
    # close file when done
    close(MIND);
    
    # remove last element of array
    pop @file;
    
    # open file for writing
    open(MIND,">thoughts.txt") || die("Unable to open file!\n");
    
    # print array back into file
    foreach $line (@file)
    	{
    	print MIND $line;
    	}
    
    # close file when done
    close (MIND);

    The pop() function removes the last element of the array specified. And when you "cat" the file again, you'll see that the last line has been removed.

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek