Hi, I need guidance with this perl script . I am trying to print a block of lines (say 5 lines at a time). I do have a start line and an end line. I have to look into multiple flat files from which I will be accessing that block. my block of code that I have written is as follows:

open(MYINPUTFIL3, <DATA>); MYINPUTFIL3: my $spool1 = 0; my @a; while (<MYINPUTFIL3>) { if (/start line:/i) { $spool1 = 1; next; } elsif (/End Line:/i) { print map { "$_" } @a; $SYS=$_; $SYS =~ s/\r|\n//g; $SYS=~ s/^\s+//; $SYS=~ s/\s+$//; $SYS1 = $SYS; $SYS2 = $SYS; $SYS2 = s/[^a-zA-Z]*//g; $SYS1 =~ s/[^0-9.]*//g; @a = (); } if ($spool1) { push (@a, $SYS); $SYS=$_; $SYS =~ s/\r|\n//g; $SYS=~ s/^\s+//; $SYS=~ s/\s+$//; $SYS1 = $SYS; SYS2 = $SYS; $SYS2 = s/[^a-zA-Z]*//g; $SYS1 =~ s/[^0-9.]*//g; last; } } close DATA;

Let me give example of input and output data here:

Input Data:

id1 : /a text lies here/

add name: new add1

ph no: new phone1

country: new country1

id2 : /some other text lies here/

add name: new add2

ph no: new phone2

country: new country2

Desired output:

id2:/some other text lies here/ add name: new add2 ph no: new phone2 country: new country2

So my question is as follows.

1. it prints the details for the Id1 and not Id2 even though I am giving id2 in the if statement (for start Line)

2. I want next 4 lines to be printed. But it prints only one line after the start line.

3. I get output which is distorted(with tabs and new line in it)

Please guide...Stuck....


In reply to printing alternate lines from a file by perl_req

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.