Here is one approach. It is not particularly elegent, but it gets the job done.

#!/user/bin/perl my $in_str = "[DATA]\n" . "data1\n" . "[ENTRY]\n" . "entrying here\n" . "[STORY]\n" . "story1\n"; my $out_str; print "\n"; print "IN STRING:\n"; print "$in_str"; print "\n"; my @decomposed = split("\n",$in_str); print "\n"; print "DECOMPOSED STRING:\n"; print join(", ",@decomposed); print "\n"; print "\n"; my @recomposed; my $start; my $stop; my $out_token; foreach my $token (@decomposed) { if($token =~ /\[(\w+)\]/){ $start = "<$1>"; $stop = "</$1>"; } else { $out_token = "$start"."$token"."$stop\n"; push(@recomposed, $out_token); } } $out_str = join("",@recomposed); print "\n"; print "OUT STRING:\n"; print "$out_str"; print "\n"; exit(0);

I presume that you're getting the data through some input stream (file reads or something similar). Note that in the above code I have not modeled that. Instead I set up a simple string variable $in_str to duplicate the specific input that you specified. That part of the challenge is up to you to work out. But the heart of the code below should take care of the conversion for you.

Note also that I have put in several print statements so that you can see what's going on along the way and to confirm that the code, in execution, is progressing properly. You, obviously, wouldn't need them in your code (though they represent a strategy to help be sure that an algorithm is behaving the way one expects/desires).

The other responders offer very good and sage advice and it would behoove you to head their suggestions and advice. Also, as throughout Perl, TMTOWDI; my approach is neither elegent nor, by any means, the only way to get the job done.

ack Albuquerque, NM

In reply to Re: convert to XML by ack
in thread convert to XML 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.