Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I'd say from your code that you are trying to build for every sentence Si a list which contains a all sentences Si1, Si2, ..., Sik that start with any of the words belonging to Si. However, your code keeps overwriting this list with the sentence which start with the last word of every sentence -- making the code if not incorrect, at least suspicious and inefficient. If you are trying to do what I think you are, here is how I'd do it for you:
use strict; use warnings; my $dat0 = 'a.txt'; open (DAT, "$dat0") or die "Could not open file `$dat0'.\n"; my @all=<DAT>; close (DAT); my @words = (); my $sentences = {}; # 'word' => [ sentences that start with `word' ] foreach my $sentence (@all) { chomp ($sentence); push (@words, [ split (/[ \t]+/, $sentence) ]); my $firstWord = $words[-1]->[0]; $sentences->{$firstWord} = [] if not exists $sentences->{$firstWor +d}; push (@{$sentences->{$firstWord}}, $#words); } my @temp = ('', ''); for (my $i = 0; $i <= $#words; $i++) { push (@temp, $all[$i]); my @referencedSentences = (); foreach my $j (@{$words[$i]}) { if (($j ne "$j") || ($j ne "v")) { # I don't get this so I lea +ve it intact if (exists $sentences->{$j}) { push (@referencedSentences, $sentences->{$j}); } } } push (@temp, \@referencedSentences); } print "Done.\n"; # ...

As you can see, I first make a hash indexed by the first words in every sentence, where the values are references to arrays holding the indices to the sentences whith start with the word. Then, for every sentence I make an array of these hash values, for every word which happens to start any of the sentences (including the one under scrutiny). I believe this code is more fit to start working on optimization -- my code ran within a second on a 3 thousand line file.

A few other remarks:
  • Initialize an empty list with my @list = ();, and not @list = '', which initializes the first element to an empty string.
  • Always use my, always stick to warnings and strict.
  • Avoid recalculating the same values more than a couple of times by caching them (like split in your code).
  • Use descriptive names and comments, especially when asking for assistance :)

In reply to Re: makeing refering faster ? by perldeveloper
in thread makeing refering faster ? by chiburashka

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? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2022-11-27 15:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?