JayBee has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to read from a file that gets written after users input their information. Some users press enter to seperate their comments into paragraphs so I can't really use the "$_" to get the data back properly or even asigning it to an array. Atleast not a way I know yet. I have delimited it using "\t::\t". Is there a way I can retrieve these type of files so I can output them into tables? Here's the attempt that works if no one uses the "enter" button.
open (FILE, "<$item") or nonexist(); @file = <FILE>; close (FILE); into_table($file[2], $file[0], $file[3], $file[9]);

The one below is what I am trying to get working but so far no luck.

open (FILE, "<$item" ) or die "Not opening file: $!"; while (<FILE>) { ($uname, $categ, $title, $locat, $desc, $ship, $paym, $pic, $price, $itnum)=split /\t::\t/;
Note: There are 3 comments by the same person, split by \t::\t and each is stored in a seperate file.

Thanks in advance...JayBee

Replies are listed 'Best First'.
Re: split a delimited file
by jweed (Chaplain) on Feb 08, 2004 at 02:36 UTC
    Are the fields for each comment separated by "\t::\t" or are the separate comment entries (with 10 fields each) separated by "\t::\t" with individual fields separate by "\n::\n" or something else(as code #2 semi-suggests)?

    If 2, use local $/ = "\t::\t"; to read in entries at a time, and split the fields from there.



    Code is (almost) always untested.
    http://www.justicepoetic.net/
      Your second assumption is correct. I just looked up local, but I am not familiar with "$/". What does it do/mean? and where do I use it, within a {while (<open>)} statement, before/after the open (FILE, '$file') line? or do you mean to put my ($item1, $item2, $item3) in place of the "$/" ? Thaks for your help
        Start your program with
        local $/ = "\t::\t";
        Then, when you use <FH>, it won't give you the next line but the next record, that us, up to the next occurance of "/t::/t". Then, within your while loop, your $_ is now something like (if "\n::\n" is the field separator)
        uname :: categ :: title :: locat :: $desc etc.
        Then, just split that bad boy into individual fields like you do in your second code sample and you'll be set.

        HTH.



        Code is (almost) always untested.
        http://www.justicepoetic.net/
        You can read about $/ and its effect on the readline (aka <>) operator in perldoc perlvar.
Re: split a delimited file
by Abigail-II (Bishop) on Feb 08, 2004 at 02:30 UTC
    Uhm, if the file uses "\t::\t" as delimiters, why are you using "\n::\n" to split on?

    Abigail