in reply to Is this the best regex?

Perhaps this will work better for you:

#!/usr/bin/perl use warnings; use strict; open my $PIPE, '-|', 'system_profiler SPApplicationsDataType 2> /dev/n +ull' or die "Cannot open pipe from 'system_profiler' $!"; my ( $i, $p ); while ( <$PIPE> ) { $i += /\A\s{4}\S.*:\Z/; $p += /\A\s+Kind: PowerPC\Z/; } close $PIPE or warn $! ? "Error closing 'system_profiler' pipe: $!" : "Exit status $? from 'system_profiler'"; print "$i applications, $p PowerPC applications\n\n";

Replies are listed 'Best First'.
Re^2: Is this the best regex?
by Anonymous Monk on Sep 09, 2009 at 04:51 UTC

    Yes -- better!

    It is faster and seems easier to understand. I changed it to:

    #!/usr/bin/perl use warnings; use strict; open my $PIPE, '-|', 'system_profiler SPApplicationsDataType 2> /dev/n +ull' or die "Cannot open pipe from 'system_profiler' $!"; my ( $i, $p, @apps ); while ( <$PIPE> ) { $apps[$i] .= $_; $i += /\A\s{4}\S.*:\Z/; $p += /\A\s+Kind: PowerPC\Z/; } close $PIPE or warn $! ? "Error closing 'system_profiler' pipe: $!" : "Exit status $? from 'system_profiler'"; foreach (@apps) { print if /Kind: PowerPC/s; } print "$i applications, $p PowerPC applications\n\n";
    Question: is there a difference between /\A\s{4}\S.*:\Z/ and /^\s{4}\S.*:$/?