Printing certain number of lines 
Author Message
 Printing certain number of lines

I have a script that update a webpage from a web based form.  It reads

<!--updates-->.  When it finds that it insearts the new update on top
and then prints the rest of the page.  Somthing like:

-------------------------
open (FILE,"$pagepath") || die "Can't Open $pagepath: $!\n";

close(FILE);

for ($i=0;$i<=$SIZE;$i++) {
    $_=$LINES[$i];
    if (/<!--updates-->/) {

        print MAIN "<!--updates-->\n";
        print MAIN "<a name=\"$stamp\"></a>\n";
        rest of update .....
    }
    elsif (/<!--images-->/) {

        print MAIN "<!--links-->\n";
        print the new addition to the list of links to the top

    }
    else {
        print MAIN $_;

--------------------------

But I need to add a section that looks for a marker called
<!--links-->.  If it finds the marker it will only allow the first 6 or
so to be displayed.  The rest will be substituted for nothing.  It needs
to do this on a line-to-line basis.  I'm guessing it needs to be done at
the end where it prints $_.

Any ideas on how I can limit the number of entries it prints out after
it adds the new entry to the top?  If anybody can tell me how to do this
I'd really appreciate it.

Thanks



Wed, 18 Jun 1902 08:00:00 GMT  
 Printing certain number of lines
I will have to make several asumptions and then answer the question I think
you need
answered.

- I assume that you have a marker to end the list of links. (call it
<!--/links-->)
- The print of the "links" marker was supposed to be an "images" marker.
- The links that do follow the marker are clean regular format that can be
easily read

if you meet these need then you can add a another elsif block that test for
the links
marker.  Then you would scan through the line array pulling out the links
and saving them.
Then you could print out the new marker,new links and then some of the links
you read in
to the max of 6 link in total.

You will have to deal with the fact that this is happening in a for loop on
all the lines.
You could  split the loop into two loops with the code to handle the links
in the middle.

You will have to brush up on some basic programming skill and some of the
perl idoms as well.
for example the lines

for ($i=0;$i<=$SIZE;$i++) {
    $_=$LINES[$i];

could have been more simply written as


This line would have set $_ to each element of the array in turn just like
the code
you have but with a lot less clutter.  This is not to say you never use for
loops the way
you did but if you never need the index of the elements in the loop the
short way is clearer.

I would also add that the changes you want to do may be handled more easily
if you data
(updates, images,links) where not stored in the html file but in another
file.  This file can be read to generate the web page either statically or
on the fly.



Wed, 18 Jun 1902 08:00:00 GMT  
 Printing certain number of lines

Quote:
> I have a script that update a webpage from a web based form.  It reads

> <!--updates-->.  When it finds that it insearts the new update on top
> and then prints the rest of the page.  Somthing like:

> -------------------------
> open (FILE,"$pagepath") || die "Can't Open $pagepath: $!\n";
> &lock(FILE);

> close(FILE);

> for ($i=0;$i<=$SIZE;$i++) {

wouldnt :


be so much nicer ?  Also by convention upper case identifiers are reserved
for filehandles and constants.

Quote:
>     $_=$LINES[$i];

If you do the above you can lose that.

Quote:
>     if (/<!--updates-->/) {

>         print MAIN "<!--updates-->\n";
>         print MAIN "<a name=\"$stamp\"></a>\n";
>         rest of update .....
>     }
>     elsif (/<!--images-->/) {

>         print MAIN "<!--links-->\n";
>         print the new addition to the list of links to the top

>     }
>     else {
>         print MAIN $_;

> --------------------------

> But I need to add a section that looks for a marker called
> <!--links-->.  If it finds the marker it will only allow the first 6 or
> so to be displayed.  The rest will be substituted for nothing.  It needs
> to do this on a line-to-line basis.  I'm guessing it needs to be done at
> the end where it prints $_.

I'm not sure because your code seems to end rather abrubtly at that point
not that I want you to post the whole program of course.

Quote:
> Any ideas on how I can limit the number of entries it prints out after
> it adds the new entry to the top?  If anybody can tell me how to do this
> I'd really appreciate it.

Well if you keep your code as it is you could do something like :

  if (/<!--links-->/)
    {

    }

You almost certainly will be using an array slice of some sort.

/J\
--
Jonathan Stowe
http://www.gellyfish.com
http://www.tackleway.co.uk



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Print lines of file until certain string found?

2. perl script to print out text after a certain line

3. Print line numbers

4. Printing Line Numbers

5. perl 5.002 warning message prints wrong line number?

6. Help me ===> How to print certain line on DB at certain html file

7. How to print the certain line on DB to certain html file.?

8. How to print the certain line on DB to certain html file.

9. regex to check if number is in a certain range

10. word wrap after certain column number + bibtex

11. how can i delete files older than a certain number of days

12. printf does not round certain numbers correctly

 

 
Powered by phpBB® Forum Software