Re: Looping through file for regex

From: James Edward Gray II (james_at_grayproductions.net)
Date: 01/02/04


Date: Fri, 2 Jan 2004 11:09:37 -0600
To: "Ed Christian" <edc@corp.ptd.net>

On Jan 2, 2004, at 10:46 AM, Ed Christian wrote:

> James Edward Gray II wrote:
>> On Jan 2, 2004, at 10:10 AM, Paul Kraus wrote:
>>
>>>> Don't do that. The foreach reads the whole file into memory and
>>>> then walks it line by line. If we just want one at a time, let's
>>>> fetch them that way
>>>
>>> I don't agree with this.
>>
>> And I don't understand this. ;)
>>
>>> If you are working with a file that is as small as
>>> the one included seems to be easier to just load up some data
>>> structures. I understand writing efficient code is always best but
>>> for a quickie script like this I wouldn't be to concerned with the
>>> 1/100th of a ms that your going to shave off but not dumping the
>>> contents to memory.
>>
>> If you're going to work with the lines one at a time anyway, what
>> exactly is the advantage of using the probably slower and definitely
>> more memory wasteful foreach()?
>
> While the foreach is certainly more wasteful, why in the world would
> you
> re-initialize and re-open the file multiple times? Why not just open
> the
> file once and iterate over the file, comparing each line of the file to
> each of the keys in the %input hash?

Your way is certainly better, if output order doesn't matter. The
original code checked the first key against the file, then second, then
third. Your suggestion could find the sixth index before it finds the
first. If the order doesn't matter though, I agree that your
suggestion would be boatloads more efficient.

James



Relevant Pages

  • Re: Looping through file for regex
    ... > Your way is certainly better, if output order doesn't matter. ... > original code checked the first key against the file, then second, then ... Your suggestion could find the sixth index before it finds the ...
    (perl.beginners)
  • Re: Carephone Warehouse Customer Care ?!!!
    ... >> people resolve such queries..so why should they care what you think? ... >other people's problems as seemed to be the suggestion. ... to criticise the actions of people at the end of their tether for actions ... dealing with this matter. ...
    (uk.telecom.mobile)
  • Re: MS Project opens Project Central
    ... Thanks for the suggestion however, it doesn't matter what ... Try opening the ... >Gantt Chart view then save the file while that view's ...
    (microsoft.public.project.pro_and_server)
  • Re: Comparing different scales
    ... > I like Rich's suggestion about transforming the reaction time variable. ... going to result, which might matter. ... in measuring the latent entity is always less than the ... You'd lose that information with z-score subtraction. ...
    (sci.stat.edu)
  • Re: Sorting and searching
    ... Well, on second thought, it looks like you could forget the concatenation ... part of the suggestion, and just use a fifth column, with something like ... with the "blah blah NNNN".) ... and then sort on that column as the first key and the Escrow# column as the ...
    (microsoft.public.excel)