Dear monk I have a file similar to this: name1 TOM RAT\n name1.1 AND\n name1.1.1 JERRY\n name2 BAT MAN\n name2.1 CAN\n name2.1.1 FLY\n name2.1.2 ANYWHERE\n and I want the output like this name1 TOM RAT AND JERRY\n name2 BAT MAN CAN FLY ANYWHERE\n

I tried to make a script but it doesn't work . I am new to perl please let me know if possible how i can do this
#!/usr/bin/perl -w use strict; open(FILE,"test.txt"); my $line=<FILE>; my $prev_name; my $prev_id; my ($new_id,$new_name); do{ ($prev_id,$prev_name)=split (' ',$line); do{ $line=<FILE>; ($new_id,$new_name)=split (' ',$line); $new_id=~/^(n.+?)\.\d+/; my $check=$1; if ($check=~/$prev_id/){ print "$prev_id\n"; + $prev_name.=$new_name; } else{ print "$prev_id $prev_name" ; } }while ($prev_id=~/$new_id/); }until eof(FILE);

In reply to string related query by viktor

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.