iLTC Home
Bookmark iLTC!
iLTC Google Boards

Google Groups
Receive The Code in Email!



Categories

Perl
  • Perl Tutorials Beginners

    Javascript
  • Javascript Tutorials
  • Javascript Tutorials Beginners

    HTML
  • HTML Tutorials



  • Perl with cheese...
    142,988

    iLoveTheCode.com
    Perl > Perl Tutorials How To Easy > OPEN and READ a Data File in Perl

    OPEN and READ a Data File in Perl



    You need to get data from the file "cities.txt"

    Example Unix Data File (cities.txt):

    London
    Paris
    Tokyo


    open(DA, "<cities.txt");
    @huhu=<DA>;
    close(DA);

    print $huhu[2];

    Which Prints:

    Tokyo


    -The left arrow before cities.txt means READ file. (full or relative path if in different directory)
    -The contents of cities.txt is now stored in the array @huhu
    -The words DA are called a Filehandle. Always use ALL CAPS for filehandles. The DA could be pretty much anything you like except STDIN and STDOUT, which are reserved for other things.


    NOTE on How Data is Read:

    Depending on your platform, the data may already be pieced out. If the data file was stored with a UNIX text editor, Perl automatically splits @huhu into pieces by line returns.

    If the the file was stored another way, you can SPLIT the array @huhu into chunks of data.


    Start Copy and Paste Perl Code (you'll need to create a file named cities.txt in the same directory as script):

    #!/usr/bin/perl

    print "Content-type:text/html\n\n";

    open(DA, "<cities.txt");
    @huhu=<DA>;
    close(DA);

    print $huhu[2];

    exit;

    Open and Read Data File - See in Action (new window)


    Click HERE to comment or discuss at iLoveTheCode GOOGLE Group
    Previous Article: Use the Perl PRINT FUNCTION
    Next Article: Matching with a Regular Expression, REGEX, in Perl





    Information Architecture...Information Architecture for the Wor...

    Louis Rosenfeld, P...

    New $26.39

    CSSCSS

    Eric Meyer

    New $29.69

    CSSCSS

    David McFarland

    New $23.09

    CSS MasteryCSS Mastery

    Andy Budd, Simon C...

    New $23.85

    HTML, XHTML, and CSS, Si...HTML, XHTML, and CSS, Sixth Edition ...

    Elizabeth Castro

    New $23.09

    jQuery in ActionjQuery in Action

    Bear Bibeault, Yeh...

    New $26.39

    Learning Web DesignLearning Web Design

    Jennifer Niederst ...

    New $26.99

    Head First HTML with CSS...Head First HTML with CSS & XHTML

    Eric Freeman, Elis...

    New $23.99


    Of Interest:
  • Passing a Variable or Value to a Subroutine in Perl
  • Matching with a Regular Expression, REGEX, in Perl
  • Write a FOREACH LOOP in Perl
  • To Reverse an Array in Perl
  • Javascript Shift - Use Shift to Remove Items from the Beginning of an Array in Javascript
  • Javascript Subscript Text
  • Create a Function in Javascript
  • Javascript Referrer
  • HTML Tables
  • HTML Superscript Text
  • HTML Forms
  • HTML Title Tag

  • More Articles:
  • To See if a Directory Exists in Perl
  • Create an Array of Specific Numbers in Perl
  • Putting Two Strings Together, Concatenate
  • onClick Javascript
  • Opening an Alert Box in Javascript
  • If, Else If, and Else in Javascript
  • Create a Function in Javascript
  • Change a Form Field with Javascript
  • HTML Body Tag
  • Make Strikethrough Text Using HTML
  • Make a Line Return Using HTML
  • HTML Background

  • Search iLTC w/ Google





    Recent Articles

  • Javascript Focus Form Field
  • Javascript Lowercase Text
  • Javascript Uppercase Text
  • Javascript Superscript Text
  • Javascript Subscript Text
  • Make Text Blink Using Javascript
  • Make a String into a Link in Javascript
  • Javascript Italics Text
  • Javascript Bold Text
  • Javascript Redirect
  • HTML Frames
  • HTML Color Chart
  • HTML Link
  • Javascript Status Bar








  • Perl Programming - Javascript Programming - Maybe Some PHP - Free Scripts - ILoveTheCode
    All Content © 2006 iLoveTheCode.com


    Contact Chris | About this Site


    439,753
    Counter by iLoveTheCode.com


    Last Modified: Friday, 23-Jun-2006 07:23:16 EDT