1842360 Members
2969 Online
110188 Solutions
New Discussion

Re: 1 more Perl ?

 
SOLVED
Go to solution
Robert True
Frequent Advisor

1 more Perl ?

How does one test a char read from a file
thus:

while (read(I_FILE, $buf, 100)) {
($mychar) = substr($buf, 0, 1);

}

to determine if $mychar is the 'null' (\0) char?

I need to make a choice based on it being null or not, and I can't seem to get the compare nailed down.

Thanks,

Rt.
13 REPLIES 13
Fred Ruffet
Honored Contributor
Solution

Re: 1 more Perl ?

if ( ord($mychar) eq 0 ) {
...
}

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
H.Merijn Brand (procura
Honored Contributor

Re: 1 more Perl ?

or simply

$mychar =~ m/^\0/

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Robert True
Frequent Advisor

Re: 1 more Perl ?

Thanks Guys! You're both right of course, they work identically. Now to drag out the perl book and find out why.

Thanks again.

Rt.
Fred Ruffet
Honored Contributor

Re: 1 more Perl ?

Procura's solution is better. "=~ m//" is more in the perl way. I'm still too near from shell and C++, but I'm doing efforts :)

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Robert True
Frequent Advisor

Re: 1 more Perl ?

Fred, I have to agree, although pattern matching reg expressions has always been a weekness of mine. I am having trouble determining the effect of the operator "=~", though, my perl book is really old. May have to look on-line.

Rt.
Fred Ruffet
Honored Contributor

Re: 1 more Perl ?

Let me tell you I consider the camel book (O'Reilly Perl Reference) as the better book on the subject. Notice that for learning there is also the good Lama Book (O'Reilly Learning Perl). The last editions are are up2date.

About matching expressions : =~ is an operator to parse the left operator through a regular expression onthe right. Most used are m// (wich search expression for the matching pattern) and s///g (which replaces first pattern by second).

Regards,

Fred


--

"Reality is just a point of view." (P. K. D.)
H.Merijn Brand (procura
Honored Contributor

Re: 1 more Perl ?

Even knowing that I am a *real* Perl fanatic, the best book for regular expressions is Jeffrey Friedl's "Mastering Regular Expressions" from O'Reilly

An absolute must for anyone using vi, perl, awk, grep, sh, sed, emacs, ...

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ralph Grothe
Honored Contributor

Re: 1 more Perl ?

Fred,

while procura's solution may be the more Perlish one, I wouldn't demean yours in saying his is "better".

I do have the feeling that a Regex match is more expensive than your function invocation.
But procura probably will prove me wrong with a profiler or Benchmark.pm result. ;-)
Madness, thy name is system administration
H.Merijn Brand (procura
Honored Contributor

Re: 1 more Perl ?

Ralph, as usual, it all depends. This small differences are neglectable:

--8<---
use strict;
use warnings;

use Benchmark;

my @txt = map {
chr (int rand 256) . "x" x 20;
} 0 .. 500;

timethese (-5, {
sub_str0 => 'for (@txt) { ord (substr ($_, 0, 1)) == 0 }',
sub_str1 => 'for (@txt) { (ord $_) == 0 }',
reg_expr => 'for (@txt) { m/^\0/ }',
});
-->8---

lt09:/tmp 115 > perl xx.pl
Benchmark: running reg_expr, sub_str0, sub_str1 for at least 5 CPU seconds...
reg_expr: 5 wallclock secs ( 5.20 usr + 0.00 sys = 5.20 CPU) @ 1837946.73/s (n=9557323)
sub_str0: 4 wallclock secs ( 5.17 usr + 0.01 sys = 5.18 CPU) @ 1859805.41/s (n=9633792)
sub_str1: 5 wallclock secs ( 5.23 usr + 0.01 sys = 5.24 CPU) @ 1838507.82/s (n=9633781)
lt09:/tmp 116 > perl xx.pl
Benchmark: running reg_expr, sub_str0, sub_str1 for at least 5 CPU seconds...
reg_expr: 7 wallclock secs ( 5.21 usr + 0.01 sys = 5.22 CPU) @ 1830904.79/s (n=9557323)
sub_str0: 5 wallclock secs ( 5.31 usr + 0.00 sys = 5.31 CPU) @ 1814271.37/s (n=9633781)
sub_str1: 5 wallclock secs ( 5.14 usr + 0.00 sys = 5.14 CPU) @ 1815065.95/s (n=9329439)
lt09:/tmp 117 >

In the second run thre regular expression is fastest.

If I change timethese to cmpthese,

lt09:/tmp 121 > perl xx.pl
Rate sub_str0 reg_expr sub_str1
sub_str0 1815759/s -- -1% -3%
reg_expr 1842023/s 1% -- -2%
sub_str1 1881363/s 4% 2% --
lt09:/tmp 122 > perl xx.pl
Rate sub_str1 reg_expr sub_str0
sub_str1 1849096/s -- -0% -2%
reg_expr 1855576/s 0% -- -2%
sub_str0 1890459/s 2% 2% --
lt09:/tmp 123 > perl xx.pl
Rate sub_str0 sub_str1 reg_expr
sub_str0 1833602/s -- -1% -2%
sub_str1 1859231/s 1% -- -1%
reg_expr 1876165/s 2% 1% --
lt09:/tmp 124 >

So the choice is yours: it realy doesn't matetr for simple things like this

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Fred Ruffet
Honored Contributor

Re: 1 more Perl ?

Ralph,

"Better" doesn't mean "less cpu costing". Some arguments may be :

First of all using "=~ m//" solution will update $_. This will probably make things easier for the continuation (and maybe make an other instruction use less CPU :)

Second is that perl stands for Practical Extracting and Reporting Language (If I remember correctly the official meaning). So parsing an expression is more "perl minded".

This are the only two that comes now, but Procura will maybe add some ;)

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
H.Merijn Brand (procura
Honored Contributor

Re: 1 more Perl ?

Fred, the matching operator '=~' does not _modify_ the string matched against (by default $_), unless you are evil minded and advanced and use embedded code like

$my_sting =~ m/(?{$my_string=''})/;
or
$my_sting =~ m/(?{{$my_string=''}})/;

in which case I hope you know - and intend - what you are doing

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Fred Ruffet
Honored Contributor

Re: 1 more Perl ?

How can I have written this ?

Sorry for this stupid saying.

Fred
--

"Reality is just a point of view." (P. K. D.)
Robert True
Frequent Advisor

Re: 1 more Perl ?

Lets not forget the other semi - original meaning of Perl: Pathologically Eclectic Rubbish Lister, which seems to be what this thread has deterioated into! :o)

I did manage to find an explanation of both original syntax's, so I (hopefully) learned something!

Rt.