dwhite20899 has asked for the wisdom of the Perl Monks concerning the following question:
The title almost gets at what I want to do. I have some code at the top of my scratchpad that almost does what I'd like. If I can use the perl metachars, that would be okay, I'm not wedded to the chars I picked for the mapping.
I'd like to express runs of character classes as one char. For example, all contiguous A-Za-z0-9 would be replaced by an 'A', all contiguous \000 replaced by an 'E', all contiguous \377 replaced by an 'F'. I've almost got it, with the code on my scratchpad, but I'm wondering if there's a more efficient way, or a method that will let me get the last coverage I need.
Now, I'm doing this:
# main while(read(FIN,$data,$bsize)) { while (length($data)) { if ($data =~ /^([\d\w]+)/ ) { notate('A', length($1)); } # more if's } } # sub notate { my $c = shift; my $n = shift; if ((! defined $c) || (! defined $n)) { close(FIN); if ($opt_M) { close(FOUT); } print STDERR "$0 : FATAL ERROR in sub notate\n"; exit; } $count{$c} += $n; substr($data,0,$n) = ''; # dangerous if (! $opt_M) { $mstr .= $c; } else { print FOUT "$c"; } return(0); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How to express contents of a file as regex metachars?
by jwkrahn (Abbot) on Jun 16, 2012 at 05:54 UTC | |
by dwhite20899 (Friar) on Jun 16, 2012 at 19:49 UTC | |
Re: How to express contents of a file as regex metachars?
by SuicideJunkie (Vicar) on Jun 15, 2012 at 19:18 UTC | |
by dwhite20899 (Friar) on Jun 16, 2012 at 19:54 UTC | |
Re: How to express contents of a file as regex metachars?
by cavac (Parson) on Jun 15, 2012 at 21:50 UTC | |
by dwhite20899 (Friar) on Jun 16, 2012 at 19:46 UTC |
Back to
Seekers of Perl Wisdom