[Phoenix-pm] Newbie Code

Scott Walters scott at illogics.org
Fri Sep 16 11:44:49 PDT 2005


No, absolutely not. As I explained in the last email, trying to tie it
sucks it all into memory, and then you're beating the VM system 3-4
times as hard as going through line by line. You don't want to suck 
everything into memory when "everything" is a lot fo stuff.

Call routines from the loops. Call one to split the lines. Call another to
record how long the longest field was. Call another to output values.
The main loop shouldn't be much more than reading, calling things, and
deciding whether to 'next' and skip the line because the current line
number is out of range.

Here's a sample closure to remember the field lengths:

sub make_field_lengths_counter {
    my @field_lengths;
    sub {
        for my $i (0.. at _) {
            $field_lengths[$i] = length $_[$i] if length $_[$i] > $field_lengths[$i];
        }
        return @field_lengths; 
    };
};

*field_lengths_counter = make_field_lengths_counter();

Then call field_lengths_counter() with a list of fields and get back a list
of the largest size it's seen for each field so far.

This routine also makes the point that it's never necessary to use global
variables. And of course it makes the point that useful features can be
moved to subroutines.

-scott



On  0, "Jonathan K. Smith" <jksmith at lexsolutio.com> wrote:
>  
> I thought I was saving some time using tie instead of reading each line?
> And while I'm thinking this might be a really dumb question, do you mean
> embbed while loops? Wouldn't that make for a very long embedded while
> condition?  I'm thinking you mean something else entirely, but I'm just
> can't figure out what you meant.  I'm completely open to trying it your
> way, I just don't see what you mean by the structure of it.
> 
> Jonathan Smith
> Encore Lex Solutio
> www.lexsolutio.com
> 1-888-389-1658
> 
> -----Original Message-----
> From: Scott Walters [mailto:scott at illogics.org] 
> Sent: Friday, September 16, 2005 11:28 AM
> To: Jonathan K. Smith
> Subject: Re: Newbie Code
> 
> Hi Johnathan,
> 
> If you mail the list, lurkers might benefit from the Q&A, and other
> people might have other helpful comments.
> 
> You really, really should read the file line-by-line twice, using just
> two while() loops, then call other routines with those lines and split
> apart arrays. You'll have two while loops instead of a dozen for loops.
> Trust me on this one.
> 
> -scott
> 
> On  0, "Jonathan K. Smith" <jksmith at lexsolutio.com> wrote:
> > 
> >    # Here is a portion of the newer code that I'm doing...
> >    # So at the start of my script I'll do this....
> >    
> >    
> >    
> >    tie(my @input_file_lines, "Tie::File", $input_file, mode => 0)
> >            or die "Can't tie $input_file: $!";
> >    
> >    
> >    
> >    # Then I am still taking three slices from beginning, middle and
> end
> >    # The @chunk_nums define the starting and ending line of
> >    # each slice I'm going to take out of the file to view
> >    
> >    
> >    
> >    my @chunk_nums = ($beg_start_line,  $beg_end_line,
> >                      $mid_start_line,  $mid_end_line,
> >                      $last_start_line, $last_end_line);
> >    for my $ctr (0..2) {
> >        # I am multiplying the $ctr by 2 to get the even number or
> >        # in turn the first of each two element pair
> >        for my $line_num ($chunk_nums[2*$ctr]..$chunk_nums[2*$ctr + 1])
> {
> >            push @return _lines, $input_file_lines[$number];
> >        }
> >    }
> >    return @return_lines;
> >    
> >    
> >    
> >    What I'm wondering while I ponder this, isn't there a method to
> >    accomplish this without embedding for loops?
> >    
> >    Oh and BTW am I supposed to send mail directly to you or to the
> list?
> >    
> >    
> >    
> >    Jonathan Smith
> >    
> >    Encore Lex Solutio
> >    
> >    [1]www.lexsolutio.com
> >    
> >    1-888-389-1658
> >    
> >    
> >      _________________________________________________________________
> >    
> >    From: Jonathan K. Smith
> >    Sent: Wednesday, September 14, 2005 5:46 PM
> >    To: 'phoenix-pm at pm.org'
> >    Subject: Newbie Code
> >    
> >    Well here's something that I've been working on, for entirely too
> >    long.  It is supposed format some sampling of another text file to
> >    easily spot problems.  The sections that don't work at all are the
> one
> >    with ############ comments above them, #This is just standard
> >    notation.  For those who actually dig through this, drop me a line
> if
> >    you have any questions.
> >    
> >    
> >    
> >    Jonathan Smith
> >    
> >    Encore Lex Solutio
> >    
> >    [2]www.lexsolutio.com
> >    
> >    1-888-389-1658
> > 
> > References
> > 
> >    1. http://www.lexsolutio.com/
> >    2. http://www.lexsolutio.com/
> 
> 


More information about the Phoenix-pm mailing list