Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day ansh007,

Welcome to the Monastery.

Take a look at "perlop: Range Operators" and the eof function. Your "from line number 100 till end" can be written in Perl as "100 .. eof".

Given this file:

$ cat XXX A B C D E

And an alias I use frequently:

$ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -E'

You can read from line 3 to the end like this:

$ perle 'open my $fh, "<", "XXX"; while (<$fh>) { print if 3 .. eof }' C D E

That also works with a literal line number instead of eof. For example, to print lines at the start, or in the middle:

$ perle 'open my $fh, "<", "XXX"; while (<$fh>) { print if 1 .. 3 }' A B C
$ perle 'open my $fh, "<", "XXX"; while (<$fh>) { print if 2 .. 4 }' B C D

For those last two, once you've read all the wanted lines, you can exit the while loop early with the last function.

See also: open for a better way to open files; the pragma index for links to strict, warnings and autodie (you should always use the first two; I highly recommend the third for simple I/O error checking); and, if you're unfamiliar with the "-M" and "-E" switches I've used, perlrun.

— Ken


In reply to Re: Loading a part of the file to array using Tie::File by kcott
in thread Loading a part of the file to array using Tie::File by ansh007

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-03-28 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found