Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Parse chat & File::find

by 4perl (Initiate)
on Jan 31, 2013 at 14:50 UTC ( [id://1016330]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

Kind request for help on a chat parsing matter. My problem is that the chat files sometimes not follow the structure "date user1 user2 chat content" on one line and sometimes the chat content moves to the next line.

i.e:

date user1 user2 chat content\n

continued chat content

because I have many files like that and need to database the records, I want to put the Filename in front of every chat line. I managed to do that with File::find but my problem still remains with the continued chats on the next line.

$lines =~ s/\n/ /g; $lines =~s /(\d{4}-\d{2}-\d{2})/\n$1/g; $newline = "$File::Find::name"."$lines"; print IN "$newline";

I appreciate any suggestions

Million thanks

Replies are listed 'Best First'.
Re: Parse chat & File::find
by wfsp (Abbot) on Jan 31, 2013 at 16:29 UTC
    You often need to do something to a line depending on what is on the next line. One way to go is to store a line, move to the next line, decide what to do and then store the current line as appropriate.
    #! /usr/perl/bin use warnings; use strict; my $skip = 0; my $last_line = <DATA>; chomp($last_line); while (my $current_line = <DATA>){ chomp($current_line); if ($current_line =~ /^\d{4}-\d{2}-\d{2}/){ if (not $skip){ print $last_line, qq{\n}; } else{ $skip = 0; } $last_line = $current_line; } else{ print $last_line, qq{ }, $current_line, qq{\n}; $skip = 1; } } print $last_line, qq{\n}; __DATA__ 2013-01-01 user1 user2 chat chat 2013-01-02 user3 user4 more chat more chat 2013-01-03 user5 user6 nazi nazi chat chat nazi 2013-01-04 user 7 user8 see you in the pub
    2013-01-01 user1 user2 chat chat 2013-01-02 user3 user4 more chat more chat 2013-01-03 user5 user6 nazi nazi chat chat nazi 2013-01-04 user 7 user8 see you in the pub
    Boy, am I rusty. It hurt my head getting that to compile and run. :-)

    Updated: Reset the $skip flag.

Re: Parse chat & File::find
by kennethk (Abbot) on Jan 31, 2013 at 15:18 UTC
    Welcome to the monastary, 4perl. It looks like you have the rough idea in place -- what about the above code doesn't work? Please see How do I post a question effectively?. In particular, please post a chunk of your literal input, wrapped in <code> tags, as well as your expected output. Also, what is in $lines? Did you slurp in the entire file ($/), which is what I'd expect from your code, or are you parsing line-by-line? A little more context would be helpful.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1016330]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-29 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found