Parsing a text file into an array

Discussion in 'Perl' started by Scott, Aug 16, 2004.

  1. Scott

    Scott Guest

    I am new to perl, and have not found any good examples of parsing to
    help me out. I have a text file that I am reading into an array that
    has to be parsed out and put into another file. I have not found any
    good examples of this. Does anyone know of any?

    For example the text file looks like this...
    900002 "Test,Test" 1/1/2004 F 21

    with tabs inbetween each of the colums of the text file.
    Scott, Aug 16, 2004
    #1
    1. Advertising

  2. Scott

    Guest

    (Scott) wrote in message news:<>...
    > I am new to perl, and have not found any good examples of parsing to
    > help me out.


    Rather then asking outright you might want to try the code
    yourself and then post problems you are having. Sample code
    will help us help you. Also try some basic problem solving
    using pseudo code...

    i.e.

    You know the format of the file so...

    Open the file
    While <more lines to read>
    Read line
    Tokenize the string based on tab
    Store results into array
    End While

    Ask yourself the following questions;
    1. How do I open a file for reading in PERL.
    2. How do I read a line from the file using PERL.
    3. You have the line, does PERL have a built in tokenizer (Parser!)

    I'll give you some hints...

    -open(FILEHANDLE, "filename");
    -while(<FILEHANDLE>)
    -chomp()
    -split()

    and one other function you should look up...
    die()

    There are lots of examples of the above functions on the newgroups.

    Cheers!
    , Aug 17, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. saltedcoffee
    Replies:
    2
    Views:
    546
    Daniel T.
    Apr 5, 2006
  2. Replies:
    5
    Views:
    246
  3. Scott

    Parsing a text file into an array

    Scott, Aug 16, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    125
  4. Domenico Discepola

    Assistance parsing text file using Text::CSV_XS

    Domenico Discepola, Sep 1, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    441
    Domenico Discepola
    Sep 2, 2004
  5. Hamanjam

    parsing text file into array

    Hamanjam, Apr 3, 2007, in forum: Perl Misc
    Replies:
    3
    Views:
    81
    Mirco Wahab
    Apr 3, 2007
Loading...

Share This Page