SunQuest
 
       Perl
  Home arrow Perl arrow Page 9 - 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  
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 101 (Part 4) - Mind Games
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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 101 (Part 4) - Mind Games - The Real World


    (Page 9 of 10 )

    Now, while all this is well and good, you're probably wondering just how useful all this is. Well, we've put together a little case study for you, based on our real-world experiences, which should help to put everything you've learned today in context [do note that for purposes of illustration, we've over-simplified some aspects of the case study, and completely omitted others].

    As a content production house, we have a substantial content catalog on a variety of subjects, much of it stored in ASCII text files for easy retrieval. Some time ago, we decided to use sections of this content on a customer's Web site, and so had to come up with a method of reading our data files, and generating HTML output from them. Since Perl is particularly suited to text processing, we decided to use it to turn our raw data into the output the customer needed.

    Here's a sample raw data file, which contains a movie review and related information - let's call it "sample.dat":
    The Insider
    1999
    Al Pacino and Russell Crowe
    Michael Mann
    178.00
    5
    This is the body of the review.
    It could consist of multiple paragraphs.
    We're not including the entire review here...you've probably already seen
    the movie. If you haven't, you should!

    Within the data block, there are two main components - the header, which contains information about the title, the cast and director, and the length; and the review itself. We know that the first six lines of the file are restricted to header information, and anything following those six lines can be considered a part of the review.

    And here's the script we wrote to extract data from this file, HTML-ize it and display it:
    #!/usr/bin/perl
    # open the data file
    open(DATA, "sample.dat") || die ("Unable to open file!\n");
    # read it into an array
    @data = <DATA>;
    # clean it up - remove the line breaks
    foreach $line (@data)
    {
    chomp $line;
    }
    # extract the header - the first six lines
    @header = splice(@data, 0, 6);
    # join the remaining data with HTML line breaks
    $review = join("<BR>", @data);
    # print output
    print "<HTML>\n<HEAD>\n</HEAD>\n<BODY>\n";
    print "\"$header[0]\" stars $header[2] and was directed by $header[3].\n";
    print "<P>\n";
    print "Here's what we thought:\n<P>\n";
    print $review;
    print "Length: $header[4] minutes\n<BR>\n";
    print "Our rating: $header[5]\n<BR>\n";
    print "</BODY>\n</HTML>\n";

    And here's the output:
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    "The Insider" stars Al Pacino and Russell Crowe and was directed by Michael
    Mann.
    <P>
    Here's what we thought:
    <P>
    This is the body of the review.<BR><BR>It could consist of multiple
    paragraphs.<BR><BR>We're not including the entire review here...you've
    probably already seen the movie. If you haven't, you should!<BR>
    Length: 178.00 minutes
    <BR>
    Our rating: 5
    <BR>
    </BODY>
    </HTML>

    As you can see, Perl makes it easy to extract data from a file, massage it into the format you want, and use it to generate another file.

    This article copyright Melonfire 2000. All rights reserved.

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


     

       

    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 4 hosted by Hostway