G'day trummelbummel,

Welcome to the monastery.

It's much easier for us to help if you provide a short, representative example of your initial data and a clear indication of your expected output. A written description of the data is rarely, if ever, useful. The data should be shown in <code>...</code> tags. I realise this is your first post: please just keep this in mind for future reference.

I'm wondering if your question falls into the "XY Problem" category: you've focussed more on a specific solution rather than on the actual problem. Your problem seems to be that you have a text file and you need to extract some data from it. Your current solution involves manipulating the data with split(), reverse() and join(): you've said this is necessary — I'm not convinced that it is.

If you'd care to consider another solution, take a look at the following (then the Notes at the end).

#!/usr/bin/env perl -l use strict; use warnings; my $sep = '-' x 60 . "\n"; my ($start, $end) = ('workset((', 'dIonly'); my $all_to_end_incl; { local $/ = $end; $all_to_end_incl = <DATA>; } print $sep, "Up to and including first '$end':\n", $all_to_end_incl; my $start_end_incl = substr $all_to_end_incl, index $all_to_end_incl, +$start; print $sep, "From '$start' to '$end' inclusive:\n", $start_end_incl; my $start_end_excl = substr $start_end_incl, length($start), -length($ +end); print $sep, "From '$start' to '$end' exclusive:\n", $start_end_excl; my $paren_group_re; $paren_group_re = qr{ \( (?: (?> [^()]+ ) | (??{ $paren_group_re }) )* \) }x; my $workset_re = qr{ ( workset\(\( (?: [^(]+ (??{ $paren_group_re }) [, ]* )* \)\) ) }x; $start_end_excl =~ $workset_re; print $sep, "Wanted extract:\n", $1; __DATA__ ... other data before 'workset' found ... workset(( RiskCA(cA, 3) RiskCB(cB, 2)) c workset((RiskCA(cA, 3), RiskCB(cB, 2), totPaycA(cA, 7), totPaycB(c +B, 6))) *********** trial #682 ceq pAAr(rA, cA, P1) c pAAc(rA, cA, P2) c ineqAA(rA, cA, P3) = (pAAc(r +A, cA, ... rl dec c cognum(X2) c watch(X1) c worklist(L) c workset((S, maxtotIneq +C(cB, X))) => watch(X1 + 1) c cognum(X2 + 1) c worklist(nil) c workset(e +mpty) c playA c dIonly [label avoidMaxI] . X2 --> 3 X1 --> 3

That code has output at each stage: partly for demo purposes and partly because I'm not entirely sure which parts you want. Only the last part, which I'm certain you wanted, is displayed; the full output is in the spoiler.

Wanted extract: workset((RiskCA(cA, 3), RiskCB(cB, 2), totPaycA(cA, 7), totPaycB(cB, 6 +)))

Notes:

-- Ken


In reply to Re: regex - problem with the loop I believe or maybe the regex itself ? by kcott
in thread regex - problem with the loop I believe or maybe the regex itself ? by trummelbummel

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.