I wrote some code today to solve a co-worker's problem. Apparently a client was concatenating a series of XML files into one big file to send via email (or some other numbskull reason) & then needed them split up afterwards for processing.

Here's what I came up with. Being that TIMTOWTDI, which would be the Better/Stronger/Faster/SHORTEST possible way to accomplish the same thing:

use strict; my @input_file = <>; my @temp; my $filecount = 0; my $filename_prefix = "outfile_"; my $file_is_open = 0; my $out_name; foreach my $in (@input_file) { if ($in =~ m/^<\?xml version/) { if ($file_is_open) { close(OF) or die "Couldn't close $out_name!!\n"; $file_is_open = 0; } ++$filecount; $out_name = $filename_prefix.$filecount.".xml"; open(OF,"> $out_name") or die "Couldnt open $out_name for writin +g!!\n"; print(STDOUT "Writing to $out_name.\n"); $file_is_open = 1; } if ($file_is_open) { print(OF $in); } } close(OF);

My first thought was to use the command flag to put perl into looping mode, but I haven't gone there yet... Any other brilliant ideas? I find I learn more quickly when other people try to tackle the same problem as I & we can share insights.



Wait! This isn't a Parachute, this is a Backpack!

In reply to XML File Slicing Golf(ish) by gregor42

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.