in reply to Recursive substitution difficulties

If it helps, the original data is coming in from a tab-delimited file, and I have control over getting that data from

[tab]test|431|alpha|123|bravo|542|charlie|412[tab]
to any intermediate step necessary.

Right now, the field above is being dumped into my input XML file as:

<name>test</name><value>431|alpha|123|bravo|542|charlie|412</value>

Replies are listed 'Best First'.
Re^2: Recursive substitution difficulties
by ikegami (Patriarch) on Mar 15, 2010 at 20:59 UTC
    My earlier solution can easily be adapted:
    s{\t([^\t]+)\t}{ my $s = "|$1"; $s =~ s{\|([^|]*)\|([^|]*)}{<name>$1</name><value>$2</value>}sg; $s }eg;

      Thanks. That looks pretty straightforward. Is it possible to use that all on a command line invocation? Right now I have something like:

      perl -pe "s/^(.*?)\t(.*?)\|(.*?)\t(.*?)(?=\n)/<wrapper1>$1</wrapper1>< +wrapper2>$2\|$3</wrapper2><wrapper3>$4</wrapper3>/g;" inputfile
      and then a follow-up to expand wrapper2 and the delimited fields within.

        I was humouring your weird way of doing things, but it doesn't scale. Split the input into fields, edit the fields you want

        perl -lape' $F[1] =~ s{\|([^|]*)\|([^|]*)}{<name>$1</name><value>$2</value>}; $F[2] =~ s{\|([^|]*)\|([^|]*)}{<foo>$1</foo><bar>$2</bar>}; $_ = join("\t", @F); ' inputfile