Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Search and replace

by kulua (Initiate)
on May 23, 2021 at 13:01 UTC ( [id://11132924]=perlquestion: print w/replies, xml ) Need Help??

kulua has asked for the wisdom of the Perl Monks concerning the following question:

I want to replace below string

input bist_g6bb_prn_clk_bistctlr;

to

output bist_g6bb_prn_clk_bistctlr;

and then change the output bist_g6bb_prn_clk_bistctlr; string to

assign vif.bist_g6bb_prn_clk_bistctlr = bist_g6bb_prn_clk_bistctlr;

use strict; use warnings; use autodie; my $data = <<'EOD'; input bist_g6bb_prn_clk_m; EOD open my $fh, '<', \$data; while (my $line = <$fh>) { $line =~ s/input .*clk/output .*clk/g; print $line; } close $fh;

I am new to perl, Can you point me my mistake ?

Replies are listed 'Best First'.
Re: Search and replace
by hippo (Bishop) on May 23, 2021 at 13:46 UTC
        $line =~ s/input .*clk/output .*clk/g;

    The first part of s/// is a regular expression but the second part is a string. It is essentially s/REGEX/STRING/. Since (in the first part of your task, which is the only part your code attempts to address) there are no complications, you can ignore the rest of the pattern and just do this:

    $line =~ s/input/output/;

    That should get you farther along. For the general case you would need to use capture groups.


    🦛

      Hi Hippo ,

      I want to change the string to output when the string clk matches not the input to output

      Can you provide me good tutorial for reg expression ?

        $line =~ s/input(?= .*clk)/output/g;

        Untested since no test cases were provided.

Re: Search and replace
by 1nickt (Canon) on May 23, 2021 at 13:35 UTC

    Please answer this question: have you read and studied the examples in perlrequick? There is a section entitled exactly "Search and replace".


    The way forward always starts with a minimal test.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-16 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found