Perl
  Home arrow Perl arrow Page 4 - Perl: Working with Files
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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? 
PERL

Perl: Working with Files
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2007-11-07

    Table of Contents:
  • Perl: Working with Files
  • How to Open a File
  • How to Read a File
  • Manipulating Data
  • How the Code Works

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Perl: Working with Files - Manipulating Data


    (Page 4 of 5 )

    Now that we have read the contents of our file, let's put that lazy, good for nothing data to work. As you may or may not have guessed, our table of data consists of super hero names, their super powers, and their weaknesses. So for instance, the Indian super hero Apache Chief has the ability to grow tall; unfortunately he also wears a man-skirt, and people can see under it when he does, which is a weakness. Similarly, the Incredible Hulk transforms in a super strong behemoth, but when he does so, his pants explode. I mean, talk about a weakness. The guy has to buy new pants every time he gets angry.

    Let's create a program that utilizes all of the data in the file:


    #!/usr/bin/perl


    $my_file=”super.txt”;


    open(PLOT, $my_file) || die(“I refuse to open your file!”);

    @my_data=<PLOT>;

    close(PLOT);


    print “Content-type: text/html\n”;

    print “<HTML><BODY>”;


    foreach $superhero (@my_data)


    {

    chop($superhero);

    ($hero_name, $hero_power, $hero_weakness)=split(/\|/,$superhero);

    print “My name is $hero_name, my super power is $hero_power, and my weakness is $hero_weakness. I hate my life.”;

    print “<BR>\n\n"

    }


    print “</BODY></HTML>”;



    The above code will print out the text:

      My name is The Incredible Hulk, my super power is Super Strength, and my   weakness is I rip my pants

      My name is Daredevil, my super power is heightened senses, and my weakness is I have poor fashion sense due to blindness

      My name is Apache Chief, my super power is the ability to grow very tall, and my weakness is I wear a skirt

    More Perl Articles
    More By James Payne


       · Unfortunately this article is really superficial, anybody can learn much more by...
       · It looks like the code has been altered by whatever software is being used to...
       · Hey, thanks for the comment. To be honest with you, this is a beginner's series,...
       · Echoing the comment about the missing slashes...I can't get the 'Superhero' program...
       · Hi Tom,Did you copy and paste the code into your editor? If so, that might be...
     

       

    PERL ARTICLES

    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway