G'day proxie,

Welcome to the Monastery.

"Can someone help me get started on the most efficient reg exps to do this?"

When Perl's string handling functions can do the job, you'll generally find that they'll be measurably, more efficient than regular expressions. Here's a solution that doesn't use regular expressions. Use Benchmark to compare the efficiency of this code against other posted solutions.

#!/usr/bin/env perl use strict; use warnings; my $cmd = q{execute test_number_1 -tex -tex_args -sub_args +debug_dir= +./ -sub_args +debug_dir=./ -constraint parity_en,random_en -sub_args +'"' ruck=1 '"' -constraint dual_en -sub_args -cd -sub_args 2596.slow +-sub_args test -seed 1 -tex_args- -opt 1 -tag 2}; my $TAB = ' ' x 4; my $depth = 0; my @tokens = split ' ', $cmd; my @parts; cmd_format(\@tokens, \@parts, $depth); print "$_\n" for @parts; sub cmd_format { my ($tokens, $parts, $depth) = @_; for (my $i = 0; $i <= $#$tokens; ++$i) { my $token = $tokens->[$i]; if (-1 == index $token, '-', 0) { my @starts = ($token); while (-1 == index $tokens[$i + 1], '-', 0) { push @starts, $tokens[++$i]; } push @$parts, $TAB x $depth . "@starts"; } elsif ($token eq '-tex') { push @$parts, $TAB x ($depth + 1) . $token; cmd_format([@{$tokens}[$i + 1 .. $#$tokens]], $parts, $dep +th + 2); last; } elsif ($token eq '-tex_args') { push @$parts, $TAB x $depth . $token; my $args = []; while ($tokens->[$i + 1] ne '-tex_args-') { push @$args, $tokens->[++$i]; } cmd_format($args, $parts, $depth + 1); push @$parts, $TAB x $depth . $tokens->[++$i]; } else { my @opts = ($token, $tokens->[++$i]); for ($i + 1 .. $#$tokens) { last unless -1 == index $tokens->[$i + 1], '-', 0; push @opts, $tokens->[++$i]; } push @$parts, $TAB x $depth . "@opts"; } } }

Output:

execute test_number_1 -tex -tex_args -sub_args +debug_dir=./ -sub_args +debug_dir=./ -constraint parity_en,random_en -sub_args '"' ruck=1 '"' -constraint dual_en -sub_args -cd -sub_args 2596.slow -sub_args test -seed 1 -tex_args- -opt 1 -tag 2

— Ken


In reply to Re: Help with parsing command line to make more readable by kcott
in thread Help with parsing command line to make more readable by proxie

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.