develooper Front page | perl.beginners | Postings from March 2002

Lines count...how to..

From:
Vitali
Date:
March 4, 2002 03:50
Subject:
Lines count...how to..
Message ID:
0aba01c1c373$e0f51ce0$160101c8@PDFCLIENT.local
Dear friends,
can anybody correct me...I have a text file.Data look like this:
10
20
20
20
20
10
20
20
10
20
20
20
and so one
-------------------------------------------------
while (<STDIN>) {
if  (substr($_,0,2) eq "10")  {           #If left 2 characters is 10,then
.....
  ++$count;
  $lines = 0;
  open  OUT, ">$count.out";
  print OUT $_;                   };

if  (substr($_,0,2) eq "20")    {          #If left 2 characters is 20,then
.....
    ++$lines;                                     #Counts he lines
    if ($lines == 4){ print OUT "Someinfo\n";$lines = 0;};
    print OUT $_;};

  }
 __EOF__

Now is: if lines count of type "20" is 4, print "Someinfo" after 4 line.
How I can fix the code, if lines count of type "20" is less that 4(for exp
1,or 2,or 3), and I need print "Something" after last line of "20"?
Here is example of how to output must be look.

10
20
20
20
20
Someinfo
10
20
20
Someinfo
10
20
20
20
Someinfo
10
20
20
20
20
Someinfo
20
20
20
20
Someinfo
20
20
20
20
Someinfo




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About