Hi, I'm trying to parse some text and put the data into a CSV format. I thought it would be simple, but I still can't figure out what I'm missing. The text contains repeating lines, alternating between username information and last login times, like this:
Username: JANDERSON Owner: JOE ANDERSON Last Login: 14-JAN-2002 08:14 (interactive), 4-MAR-2002 17:09 (non-in +teractive) Username: PBARRETT Owner: PAUL BARRETT Last Login: 18-JAN-2006 08:14 (interactive), 24-MAR-2005 17:09 (non-i +nteractive)
... and so on. I want the output to look like
JANDERSON,JOE ANDERSON,14-JAN-2002 08:14,4-MAR-2002 17:09 PBARRETT,PAUL BARRETT,18-JAN-2006 08:14,24-MAR-2005 17:09

So I thought the following code would be a good start -

while (<IN>) { next if (/^\s*$/); chop; if (/^Username\:\s+([\w\s]+)\s+Owner\:\s+(.*)$/) { $username = $1; $desc = $2; print "$username,$desc,"; } elsif (s/^(Last Login\:\s*)//) { ($login_times{"i"}, $login_times{"ni"}) = split /,/, $ +_; print "$login_times{\"i\"}, $login_times{\"ni\"}\n"; } else { die "line invalid - $_, $!\n"; } } close (IN);

But when I run the code, it doesn't print anything for $username or $desc, even though I know it captures $1 and $2 correctly, as I put in debug statements, showing what they were.

Anybody know what I'm missing here?


In reply to Parsing Text into CSV by bowei_99

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.