Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: new file per line output

by Kenosis (Priest)
on Dec 31, 2013 at 19:07 UTC ( #1068815=note: print w/ replies, xml ) Need Help??


in reply to new file per line output

You've received excellent scripting suggestions. Still, perhaps the following minor modifications of your script will be helpful:

use strict; use warnings; use DateTime; use POSIX qw(strftime); use autodie; open my $logFH, '<', 'second.txt'; while (<$logFH>) { my ( $streamname, $streamid, $timedate ) = split /[@\s]/; my $time_t = POSIX::strftime( "%Y-%m-%d %r", localtime($timedate) +); open my $fh, '>', "$streamid/$streamname"; print $fh <<END; <event> <stream-id>$streamid</stream-id> <event-name>$streamname</event-name> <primary-event> <delete-time>$time_t</delete-time> </primary-event> </event> END }
  • Always: use strict; use warnings;
  • Used a single split
  • Used the three-argument open()
  • Used a here document for printing.
  • A close is absent above, since the currently-opened file will automatically close when a new handle is assigned to $fh

Edit: Added use autodie; to catch any silent close failures. Thank you, davido.


Comment on Re: new file per line output
Select or Download Code
Re^2: new file per line output
by davido (Archbishop) on Dec 31, 2013 at 19:16 UTC

    I don't mind implicit closes of input filehandles, but without the autodie pragma, the implicit close within a loop of output files could permit silent failure.


    Dave

      Thank you. Have updated the script.

Re^2: new file per line output
by AnomalousMonk (Abbot) on Dec 31, 2013 at 20:43 UTC
    open my $fh, '>', $streamid/$streamname;

    Open for writing a file that has a name that is the stringized quotient of $streamid divided by $streamname? Surely this is not what monteryjack intends!

      Appreciate you catching this. Accidentally removed the quotes when I updated the script. Fixed!

Re^2: new file per line output
by karlgoethebier (Curate) on Jan 01, 2014 at 14:22 UTC

    Hi Kenosis, by the way another question: How did you format the here document? By hand?

    One reason why i avoid using SQl/XML here documents is that it's sometimes hard to format them.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Hi Karl and Happy New Year!

      Yes, by hand--only for readability, since it was just a few tags.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1068815]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-09-21 16:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls