This was easy enough, I went ahead and wrote the code for you. In situations like this, use a combination of regex and split. It takes some experience to decide which to use. In general use regex when you know what to keep and use split when you know what to throw away. Below I started with a regex to make sure that I'm looking at something that looks like a valid line. Then successive splits are applied which throw away various stuff. You will have to consider if there are hyphenated names, maybe search your input for lines that contain more than the usual number of hyphens? There is always a bit of trial and error when parsing something without at formal spec that covers all cases. Good luck.
#!/usr/bin/perl use strict; use warnings; while (my $line = <DATA>) { if (my( $num, $root_name, $rest)=$line =~ /^\^(\d+)\s*%\s*(\w+)[. | +]+(.+)\+$/) { my @sub_names = split /;\s*/,$rest; foreach my $sub_name (@sub_names) { my ($name1, $name2) = split /\s*-\s*/,$sub_name; print "\"$num\",\"$root_name\",\"$name1\",\"$name2\"\n"; } } } =Prints "46004","Tamerlane","Tamerlane","Sheridan" "46004","Tamerlane","Bajazet","Barry" "46004","Tamerlane","Moneses","A Gentleman" "46004","Tamerlane","Arpasia","Mrs. Furnival" "46004","Tamerlane","Selima","Mrs. Elmy" "46005","Hamlet","Hamlet","Sheridan" "46005","Hamlet","Polonius","J. Morris" "46005","Hamlet","Laertes","Lacy" "46005","Hamlet","Ophelia","Mrs. Storer" "46005","Hamlet","Queen","Mrs. Furnival" =cut __DATA__ ^46004 % Tamerlane.| Tamerlane - Sheridan; Bajazet - Barry; Moneses - +A Gentleman; Arpasia - Mrs. Furnival; Selima - Mrs. Elmy; + ^46005 % Hamlet.| Hamlet - Sheridan; Polonius - J. Morris; Laertes- La +cy; Ophelia- Mrs. Storer; Queen - Mrs. Furnival; +

In reply to Re: Help formatting text to delimited text in file by Marshall
in thread Help formatting text to delimited text in file by jcg3525

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.