Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Open to suggestions on a search and replace..

by Rhodium (Scribe)
on Apr 12, 2002 at 03:19 UTC ( #158478=perlquestion: print w/ replies, xml ) Need Help??
Rhodium has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
Here is what I have. This isn't very clean and depends on the fact that there is only a single split. I'd like to clean this up and simplify it a bit. Note: It does work, but I think this can be simplified. Here is a sample of the code I want to modify
RR<9> vr<9> vr<8> 1.25126K $SUB=<*40>Vrefout $[PI] RR<10> vr<10> vr<9> 1.25126K $SUB=<*40>Vrefout $[PI] RR<11> vr<11> vr<10> 1.25126K $SUB=<*40>Vrefout $[PI] RR<12> vr<12> vr<11> 1.25126K $SUB=<*40>Vrefout $[PI]
and here is what it should clean up to..
RR<9> vr<9> vr<8> 1.25126K $SUB=Vrefout $[PI] RR<10> vr<10> vr<9> 1.25126K $SUB=Vrefout $[PI] RR<11> vr<11> vr<10> 1.25126K $SUB=Vrefout $[PI] RR<12> vr<12> vr<11> 1.25126K $SUB=Vrefout $[PI]
And here is my <it>kludgy</it> code
if ($line =~ /^R.*\$SUB\=\<\*\d+\>/){ my @resline = split /=/, $line; $resline[1] =~ s/\<\*\d+\>/=/; $line = $resline[0].$resline[1]; }
I am pretty open to suggestions. Thanks much
Rhodium

The <it>seeker</it> of perl wisdom.

Comment on Open to suggestions on a search and replace..
Select or Download Code
Re: Open to suggestions on a search and replace..
by JayBonci (Curate) on Apr 12, 2002 at 03:31 UTC
    It looks like a substituion regular expression might be what you need. (you look like you want to substitute "<*40>" for "").

    $line =~ s/\<\*40\>//g;
    The "s" before the regexp is for substitue, and the "g" tells it to apply to the statement more than just once. That might be simpler than finding, removing, splitting, and joining. Nothing is a valid substitution candidate.

    Update: There is a tutorial at String matching and Regular Expressions that discusses s/// and the intricacies therein.

        --jb
Re: Open to suggestions on a search and replace..
by I0 (Priest) on Apr 12, 2002 at 05:49 UTC
    $line =~ s/^(R.*\$SUB=)<\*\d+>/$1/;

      You can use a positive lookahead assertion to avoid having to reinsert $1.

      $line =~ s/^(?=R.*\$SUB=)<\*\d+>//;

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        You can use a positive lookahead assertion to avoid having to reinsert $1. $line =~ s/^(?=R.*\$SUB=)<\*\d+>//;

        aehhm, this won't work. The lookahead doesn't move the position in your regex, so you are effectively looking for /^<\*\d+>/ which isn't there. Instead you would need a look behind assertion but they are only available for constant length.

        As I understand the file format there are no other digits in angle brackets starting with a star. So why not simply write: s/<\*\d+>//;

        Update: As of Rhodium's clarification of the specs, I would now fully recommend IO's solution.

        -- Hofmator

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://158478]
Approved by mdillon
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-08-01 05:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls