I'm working on a script to parse a Rational Rose design file (.cat/.mdl/.sub) and this pretty much does the job for me, However I had to resort to using a temporary file-handle for the "pre-processing", because when I read a line in from the original file, I may split it into three separate new lines. Here's an example of what the script does to the input (basically it pretty-prints the style so that the braces show up one per line, which makes other scripts reading this format easier to write)

BEFORE

(object (name1 val1 name2 val2))
AFTER
( object ( name1 val1 name2 val2 ) )
My question is: Is there a way to do this without resorting to a secondary file? I have another script that will use this code recursively against a whole baseline of design files, and using temporary files may make it too slow.

The Script:

$tempfile = "jojowashere.jbc"; if ($#ARGV < 0) { print "\nUSAGE: $0 filename.cat"; exit(0); } open (fp,$ARGV[0]) || die ("Can't open $ARGV[0]! Exiting."); open (tempfp,"> $tempfile") || die ("Can't create $tempfile! Exiting." +); while(<fp>) { if (m/(^.*)(".+")(.*)/) { $a = $1; $b = $2; $c = $3; $a =~ s/\(/\n\(\n/g; $a =~ s/\)/\n\)\n/g; $c =~ s/\(/\n\(\n/g; $c =~ s/\)/\n\)\n/g; $_ = $a . $b . $c; } else { s/\(/\n\(\n/g; s/\)/\n\)\n/g; } print tempfp "\n$_"; } close fp; close tempfp; $num_indents = 0; open (fp,$tempfile) || die ("Can't open $tempfile!"); while (<fp>) { s/^\s+//; s/\s+$//; chomp; if (length) { if (m/^\)$/) { --$num_indents; } $spacing = ""; for ($j=0;$j<$num_indents;$j++) { $spacing .= " " }; print "\n$spacing$_"; if (m/^\($/) { ++$num_indents; } } } close fp; unlink $tempfile;
Thanx, jojo

In reply to Parsing a file, splitting current line into multiple ones, to be parsed separately by Anonymous Monk

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.