in reply to sysread and null characters

I'm trying to figure out why you want the "++$i" on your sysread line. Eliminate that - you'll probably be much closer to what you want.

What's happening is that you're reading into an offset of $digit. Perl sees that $digit is empty, and prepends all the nulls to that point.

Then you won't need to do any funkiness. Such as the next bit, which I'll pretend is still needed (it shouldn't be):

$_ = $digit; s/(\d)/$1/; $digit = $_;
You're assigning $digit to the global $_, then changing the digit in it to ... itself (and not changing anything else), then reassigning back. Choices:
#1 $_ = $digit; s/.*(\d).*/$1/; # change everything to the captured digit $digit = $_; #2 $_ = $digit; m/(\d)/ && $digit = $1 # change $digit to be the captured digit #3 $digit =~ m/(\d)/ && $digit = $1 #4 $digit =~ s/.*(\d).*/$1/;
Hope that helps!

Replies are listed 'Best First'.
Re^2: sysread and null characters
by ggg (Scribe) on Mar 24, 2005 at 15:58 UTC
    Your example #4 is what I had tried first, but without the ".*" parts. I'm still not sure why they're needed. What I thought I was asking for was a match with any digit no matter where it was in the string. My bad.

    At this point, after adding the ".*", it's doing just what I expected. Hurray!

    As to the need for ++$i; don't I need to change the offset each time I read the file in order to step further into the file to get the next digit? You seem to be implying that that's unneeded. Does sysread auto-increment it's own offset when it's used in a loop? If I just remove the ++, I get a runaway loop, so I guess not. What did you have in mind, please?

    I could leave it just the way it is, but I'd rather understand sysread a little better.

    ggg

      Ok, maybe I should have been a wee bit more explicit on the sysread part. What you want is:

      sysread DF, $digit, $c;
      The fourth parameter to sysread is called "offset". But it's not the offset into the file, it's the offset into the buffer (in your case, $digit). All file-reading functions read from the "current file position". Always. You can change the "current file position" on physical files (using seek), but not on all filehandles (e.g., a pipe from another process, or a socket). The act of reading from (or writing to) a file handle implicitly advances the position.

      The purpose of the offset in sysread, then, is to automatically concatenate multiple reads in a single buffer. This is not what you're doing.

      The reason for the .* parts in a substitution (s///) operator is to have the regular expression match the whole string. This way you're replacing the whole string rather than just replacing the digit (with itself). You may want to peruse the Regular expression tutorial and/or the regular expression reference for more info here.

        Congratulations on Sainthood, Tanktalus!

        Your explanation of sysread clarifies some other puzzeling behaviors I was seeing but didn't mention. Thanks!

        I've bookmarked your links to regex info - your explaination gave me a cross between Do'h and deja vu.

        One more thing, if you don't mind. The sysread info on perldoc.perl.org says Use sysread() and check for a return value for 0 to decide whether you're done. I'd like to use that feature to end the while loop rather than incrementing a counter, but is that return value zero? If so, how would I tell the difference from a zero in the data?

        Or is that talking about some test value the function itself returns? ($EndTest = systead( FH, $a, $b ))

        ggg