Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^6: Substitute in a subparagraph

by blazar (Canon)
on Oct 19, 2008 at 10:19 UTC ( #718039=note: print w/replies, xml ) Need Help??


in reply to Re^5: Substitute in a subparagraph
in thread Substitute in a subparagraph

I personally believe that since the text you're operating on seems highly structured, and more precisely a proper language, then a fully reliable solution would comprise to write a parser. Perhaps one exists already. However, if you're fairly sure that the your data is regular enough then you may want to slurp it all at once and process it with somewhat naive regexen. The following program follows such an approach does work as expected on your sample, but be warned that it may fail on the full data.

#!/usr/bin/perl use strict; use warnings; use 5.010; sub doit { my ($head, $body) = @_; $body =~ s/QUALIFIED// unless $head ~~ /^\w+?clk\[\d\]/; $head . $body; } local $_ = do { local $/; scalar <DATA> }; s/ ^ \b ( [ \w \s \[ \] ]+ \s+ \( ) $ ( .*? ^\)$ ) /doit $1, $2/gemsx; print; __END__ qclk[6] INPUT ( ! "asdk fd sasd" VALID ( late_lead 3 ar qclk slope 20 late_lead 3 af qclk slope 04 early_dn 8 ar qclk slope 6 early_up 6 af qclk slope 6 ) cext %0.00394757 cmax %0.005504 QUALIFIED ) clkout_qclk_61[3] OUTPUT ( ) clkout_qclk_61[2] OUTPUT ( REQUIRED ( earlyp 0.5 br qclk clm(2) latel_up 5 bf qclk clk(2) ) REQUIRED ( early_lead_dn 0.004 bf qclk clkdom(2) late_trail_dn 0.005 br qclk clkdom(2) ) cext %0.0647336 max_ceff_up %0.187 QUALIFIED )
--
If you can't understand the incipit, then please check the IPB Campaign.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://718039]
help
Chatterbox?
[Dumu]: got it. Anyway. Perl question. Can you use binmode more than once with an open filehandle to read a file first in binary mode, then in utf8 mode?
[ambrus]: Dumu: you can use binmode multiple times, but mostly at the start before you read things. it would be strange to change the io layers after you've already read things, and might not work too well.
[ambrus]: Dumu: why would you want to read the same file both in binary mode and utf8 mode?
[Corion]: Dumu: Yes, but switching needs to be done with caution, as you might read half an UTF-8 character in binary mode, and then switch to utf8 mode...
[Corion]: Personally, I would do the decoding of data in the program and not in the filehandle
[Corion]: ambrus: Maybe a record-based file where some data is strings encoded as utf8
[Corion]: But again, I would read all data as binary and then decode from utf8
[ambrus]: Corion: that wouldn't be my guess, but I'll wait for Dumu
[Corion]: Meh. I'm now on Firefox 57 ("Quantum") and again have to remove Pocket, and reinstall the adblocker, uMatrix (because NoScript doesn't work) and silence the about:blank page to not download crap from the internet
[Corion]: Maybe I shouldn't update software ...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2017-11-20 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (290 votes). Check out past polls.

    Notices?