in reply to Parsing and \G and /g and Stupid User Tricks

Okay, I must be missing something big. You appear to be using $dpf alternately as a file handle (to read()) and as a string (something that you can match regular expressions against). You can't match a filehandle against a regular expression (unless I've missed a major added feature). If you don't pass an argument to pos(), then it works on $_, which you never use anywhere else in your program. I don't see how this can work at all!

I suggest you read the entire contents of the file into a single string and do your processing on the string. Then you won't need \G nor .*.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Parsing and \G and /g and Stupid User Tricks: $fileHandle =~ m//g;: v2
by THuG (Beadle) on Aug 08, 2000 at 20:10 UTC

    I want to avoid reading in the entire file, since I am under the impression that I can save memory that way, and it is very easy to let Perl take care of where in the string I am (tell), let me step through it (read) and jump to where I need to be (seek). All of this is possible, I'm sure, in a string, but I'm guessing not as easy.

    Whether or not you are allowed to match via a pointer, I don't know. It isn't breaking anything, it just isn't working. I am not sure why it is not working, but I am under the impression that it is matching on the first record ID over and over again, and never moving on.

    I could be wrong. I'll play some more and see.

    -Travis

    v2:

    Okay, no, I can't match via the file handle. I can do <FIN>=~m//g;, but I imagine that is because it reads <FIN> into $_ and then does the m//g, which means I am reading in the entire file anyway! Nothing saved there, might as well read it all into $str.

    -Travis

      Yes, you can use tell, seek, and read easily on a file handle. But you can't use a regular expression on a file handle so you need a major change to your approach.

      How big are the files? Perl uses lots of memory for most things, so reading even a moderately large file into memory probably isn't go to make much of a difference on memory usage.

      In any case, you're going to have to read in a big chunk of the file into a string (say $str) so that you can match a regular expression against that chunk. If you use ($str =~ m/.../g), then you can use $pos= pos($str) to see where you currently are in the string and pos($str)= $newpos to change where in the string to start matching against.

              - tye (but my friends call me "Tye")