Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hm, at first glance your reply sounds kinda messy, so, I guess providing some more details could help, still trying to avoid doing your homework at the same time (you know, the rules) ;-)

First, let's drop some lines from your second record, so we could see what happens when fields are missing:

1/3/2007 12:20:01 AM
Login,12.588309
SearchLoad,9.432586
SearchCount,20:0.196329
SearchResults,7.418672
SearchSave,3.616305
SearchDelete,2.066482
SearchDetails,6.873061
ClientAdd,0.784989
CMALoad,1.859894
CMASave,3.249620
CMADelete,0.450952
ClientDelete,0.305768
Logout,0.823402
1/3/2007 12:49:22 AM
Login,10.958312
SearchCount,41:0.483233

Now let's rewrite my previous HoH solution and move the "key" (time stamp) *inside* the hash, using an AoH:

$ perl -MData::Dumper -F, -lane '@F % 2 and push @D, {q{Stamp},@F} or +$D[-1] = { %{$D[-1]}, @F } }{ print Dumper @D' input.txt
$VAR1 = {
          'Stamp' => '1/3/2007 12:20:01 AM',
          'ClientAdd' => '0.784989',
          'CMALoad' => '1.859894',
          'SearchDelete' => '2.066482',
          'CMASave' => '3.249620',
          'CMADelete' => '0.450952',
          'Login' => '12.588309',
          'ClientDelete' => '0.305768',
          'SearchCount' => '20:0.196329',
          'SearchDetails' => '6.873061',
          'Logout' => '0.823402',
          'SearchResults' => '7.418672',
          'SearchSave' => '3.616305',
          'SearchLoad' => '9.432586'
        };
$VAR2 = {
          'Stamp' => '1/3/2007 12:49:22 AM',
          'Login' => '10.958312',
          'SearchCount' => '41:0.483233'
        };

And now, let's print the fields we need from this data as CSV.

$ perl -F, -lane '@F % 2 and push @D, {q{Stamp},@F} or $D[-1] = { %{$D +[-1]}, @F } }{ $,=","; print @{$_}{qw(Stamp Login SearchResults Searc +hLoad SearchCount Logout)} for @D' input.txt
1/3/2007 12:20:01 AM,12.588309,7.418672,9.432586,20:0.196329,0.823402
1/3/2007 12:49:22 AM,10.958312,,,41:0.483233,

As you may notice, the values that are missing from any records generate empty fields, which should be just fine for CSV

Obviously, this lazy toy will trigger "undefined" warnings, but I'm sure you'll know how to handle them in your real/production code. ;-)

My apologies if this code looked too messy for you, I'll try adding some spoilers...

Finally, I have to warn you again: DON'T use this code in production, this is just a proof of concept :)

'HTH

--
altblue.


In reply to Re^3: parsing text files continued by AltBlue
in thread parsing text files continued by grashoper

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others exploiting the Monastery: (3)
    As of 2024-07-17 23:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.