Hi monks,

I'd like to split a file to three parts and search different strings.As GrandFather's reply and Super Search,Tie::File seems the best way.Below is my code:
use strict; use warnings; use Tie::File; my $file_name = 'test.txt'; my @name; tie @name, 'Tie::File' ,$file_name; die "error!!!" unless defined (my $pid_1 = fork()); exit search_string(0,9999,"8888") unless ($pid_1); die "error!!!" unless defined (my $pid_2 = fork()); exit search_string(10000,19999,"18888") unless ($pid_2); die "error!!!" unless defined (my $pid_3 = fork()); exit search_string(20000,29999,"28888") unless ($pid_3); wait; wait; wait; untie @name; sub search_string { my ($from,$to,$string) = @_; for ($from .. $to) { print "the $_ th line of test.txt is $string\n" if $name[$_] = +~ /^$string$/x; } } __test.txt__ 1 2 3 ... 29998 29999 30000
It works quite well for the FIRST time,but....Look:
# first time the 8887 th line of test.txt is 8888 the 18887 th line of test.txt is 18888 the 28888 th line of test.txt is 28888 #second time the 8888 th line of test.txt is 8888 the 18888 th line of test.txt is 18888 the 28889 th line of test.txt is 28888 #third time the 8887 th line of test.txt is 8888 the 28887 th line of test.txt is 28888 Use of uninitialized value in pattern match (m//) at serarch_file.pl l +ine 42, <$ fh> line 29884. Use of uninitialized value in pattern match (m//) at serarch_file.pl l +ine 42, <$ fh> line 29884. Use of uninitialized value in pattern match (m//) at serarch_file.pl l +ine 42, <$ fh> line 29884. Use of uninitialized value in pattern match (m//) at serarch_file.pl l +ine 42, <$ fh> line 29884. Use of uninitialized value in pattern match (m//) at serarch_file.pl l +ine 42, <$ fh> line 29884.
Please advice!

I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

In reply to regarding Tie::File by xiaoyafeng

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.