... how to go to directly that line ...
Inspired this seek-by-key approach:

You could create a simple index and then seek to the beginning of a line in order to re-read it. It might be suitable in a situation where you can keep the index in memory but not the whole file. But since the ratio of key-length to line-length is only approx. 1:5, (the smaller the ratio the better), there is probably no real gain for the given example data...

use strict; my %idx; # create index... while (<DATA>) { last if /# Prints/; # for this demo $idx{$1} = (tell(DATA) - length) if /^\s*(\S+)/; } # E.g., access each line in "natural" hash order (more/less random)... print "offset: <key> <line>\n"; foreach (keys %idx) { seek(DATA, $idx{$_}, 0); chomp (my $line = <DATA>); printf("%6d: %-7s <%s>\n", $idx{$_}, "<$_>", $line); } __DATA__ data1 122 1223 12223 12223 data2 12122 12223 122223 122223 data3 13422 134223 4512223 982223 data4 23432 3432 234234 789879 data5 5635 9786 23423 2323423 # Prints offset: <key> <line> 486: <data4> < data4 23432 3432 234234 789879> 418: <data2> < data2 12122 12223 122223 122223> 451: <data3> < data3 13422 134223 4512223 982223> 518: <data5> < data5 5635 9786 23423 2323423> 390: <data1> < data1 122 1223 12223 12223>


In reply to Re: random search in file by Perlbotics
in thread random search in file by Anonymous Monk

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.