At work, I'm in the middle of multi-threading a script that parses a lot of very large text files, in addition to various other things. The script usually takes over an hour to run, which is annoying, so I decided to multi-thread the parts of the script that taking forever. Anyway, originally I was only needing a small subset of data out of the text files. The code looked something like this...

use Threads;
use Threads::Shared;

my %hash : shared;
.
.
.
while (my $line = <FH>) {
chomp($line);
lock(%hash);
if($line =~ /regex/) {
my @parts = split('|', $line);
$hash{$file} = &share({});
share ( $hash{$file}{start} ); # for pulling start time from file
share ( $hash{$file}{stop} ); # for pulling stop time from file
$hash{$file}{start} = $parts[ 11]
$hash{$file}{stop} = $parts[ 13]
}
}
.
.
.
... This is a short simplified generic snippet from what I can remember off of the top of my head. Doing the above worked perfectly fine; however I realized that I needed additional information out of the text file. Basically, I'm trying to figure out how to correctly share a two-dimensional hash that references (i think) an array. I tried something like this, but it didn't work...

use Threads;
use Threads::Shared;

my %hash : shared;
.
.
.
my $i : shared = 0;
while (my $line = <FH>) {
chomp($line);
lock(%hash);
if($line =~ /regex/) {
$hash{$file} = &share({});
share ( $hash{$file}{certain_row_type}->[$i] );
$hash{$file}{certain_row_type}->[$i] = $line;
$i++;
}
}
.
.
.
So, basically every time we pattern match a certain row type we update the hash. I probably left some details out, but like I said I'm trying to recreate this from memory. I hope what I'm asking makes sense. I have no idea if what I'm trying to do above is even legal; which I guess it's not because I always get the, "can't start threads" message. I actually tried a bunch of other ways to share this hash, but nothing works. Any help would certainly be appreciated.

Thanks!


In reply to Sharing Hash Question by jmmach80

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.