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

I'm working on a program that will make and keep track of notes. That's beside the point though, my reall problem is that I'm not sure how to parse my note file.
The file would look something like this:

04/01/2002 19:57 FOOBARS >>my favorite candy by far! yummy! ----- 04/03/2002 07:34 Ooh, it looks as if it might rain tomorrow. note to self {buy buy buy!} ----- 04/12/2002 12:02 Energy X invasion point++ ----- 04/12/2002 12:56 Crisis resolved (that was fun!) -----
With each not starting with a timestamp, and ending with "-----." I want is parsed so all the formatting (tabs and newlines) are kept. I'd like it parsed into $month, $day, $year, $time, and $body variables. I'm not exactly sure how I could use regexps and splits (or something I'm not thinking of now) to parse it.

> munchie, the number munchin newb
Llama: The other other white meat!
(you had to be there :-P)

Edit kudra, 2002-05-03 Changed title

Replies are listed 'Best First'.
Re: I need help parsing this text file!
by mla (Beadle) on Apr 02, 2002 at 01:20 UTC
    How about this?
    #!/usr/bin/perl { local $/ = "-----\n"; while(my $note = <DATA>) { my($month, $day, $year, $time, $body) = $note =~ m#^(\d{2})/(\d{2})/(\d{4})\s+(\d{2}:\d{2})\s*\n(.*)#s; } } __DATA__ 04/01/2002 19:57 FOOBARS >>my favorite candy by far! yummy! ----- 04/03/2002 07:34 Ooh, it looks as if it might rain tomorrow. note to self {buy buy buy!} ----- 04/12/2002 12:02 Energy X invasion point++ ----- 04/12/2002 12:56 Crisis resolved (that was fun!) -----

      If you (ab)use split's ability to limit the number of fields it splits something into, you can do away with the regexp for something a little cleaner...

      my($m,$d,$y,$t,$b) = split /[ \/\n]/ => $note, 5;

          --k.


      good use of $/ {slaps self with trout}

      --
      perl -pe "s/\b;([st])/'\1/mg"

Re: I need help parsing this text file!
by belg4mit (Prior) on Apr 02, 2002 at 01:21 UTC
    Well you give no code, so it's kind of hard to see what your problem is.... You may want to reconsider your format and use something a la Text::xSV, or a DBM. If you like this format I suggest looking at .. and ... in perlop

    PS> As a general rule "I need help" and "!" aren't very meaningful in node titles.

    --
    perl -pe "s/\b;([st])/'\1/mg"

      There's no "problem" I just don't know how to do it. Here's the code I used to make the note files so far for reference.

      #! Perl use strict; my $notefile = 'notes'; #Define file for notes here sub new_notes { my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime( +time); print 'Type {e} on a blank line to end the note'."\n"; my $string = sprintf "%02d/%02d/%4d %02d:%02d\n", $mon+1, $mday, $ +year+1900, $hour, $min; while (<STDIN>) { last if m!\{e\}!; $string .= $_; } $string .= "-----\n"; open(NOTE, ">>$notefile"); print NOTE $string; close(NOTE); return 1; } sub get_notes { } print "Welcome to MyNotes, a personal note taking system.\n"; print "\tPress 'h' for help\n"; new_notes();

      > munchie, the number munchin newb
      Llama: The other other white meat!
      (you had to be there :-P)

        Well not knowing how does constitute a problem. Which is fine, nothing wrong with that. We've all been there, and still are. But having an example of what's been tried and why (what you think you are butting your head against) is always good. Of course there is nothing *for* getnotes here... Sometimes a monk (myself included) will rise to the occasion and write your code for you, but you'll have more fun if you provide an appetizer.

        --
        perl -pe "s/\b;([st])/'\1/mg"