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

i have a file which contains statement as below
cfg_a cfg_b[2:0] cfg_c[4:0] cfg_d
I want to replace them like this using perl
logic cfg_a logic [2:0] cfg_b logic [4:0] cfg_c logic cfg_d
How to do them ? Please give me a hint

Replies are listed 'Best First'.
Re: To find and replace in a file
by AnomalousMonk (Archbishop) on Feb 25, 2017 at 05:14 UTC

    One way:

    c:\@Work\Perl\monks>perl -wMstrict -e "my $data = qq{cfg_a\ncfg_b[2:0]\ncfg_c[4:0]\ncfg_d\n}; print qq{[[$data]] \n\n}; ;; open my $fh, '<', \$data or die qq{opening string by reference: $!}; ;; my $identifier = qr{ [[:alpha:]] [_[:alpha:]]* }xms; my $bracket = qr{ \[ \d+ : \d+ \] }xms; ;; while (my $line = <$fh>) { my $matched = my ($id, $br) = $line =~ m{ \A ($identifier) ($bracket?) \Z }xmsg; die qq{bad line: '$line'} unless $matched; printf qq{logic%s%s\n}, map { $_ ? qq{ $_} : '' } $br, $id; } " [[cfg_a cfg_b[2:0] cfg_c[4:0] cfg_d ]] logic cfg_a logic [2:0] cfg_b logic [4:0] cfg_c logic cfg_d

    Update: Don't worry about the odd looking

    my $data= qq{cfg_a\ncfg_b[2:0]\ncfg_c[4:0]\ncfg_d\n}; ... ... open my $fh, '<', \$data or die qq{opening string by reference: $!};
    at the start. That's just a quick way to generate an example. I assume you know how to open and read a file in the more usual way. Also, please see perlre, perlretut, and perlrequick.

    Update 2: Actually, the  /g modifier in the  m// above is redundant (but harmless). This
        my $matched = my ($id, $br) = $line =~ m{ ... }xms;
    works just as well.


    Give a man a fish:  <%-{-{-{-<

Re: To find and replace in a file
by Marshall (Canon) on Feb 25, 2017 at 06:29 UTC
    I had replied earlier, but I think my post got reaped along with some other stuff as a result of a duplicate thread being started. Here again,
    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { chomp; $_ = "$2 $1" if ( /^(.+)(\[.+)/ ); #swap 2 parts if both exist print "logic $_\n"; } =Prints logic cfg_a logic [2:0] cfg_b logic [4:0] cfg_c logic cfg_d =cut __DATA__ cfg_a cfg_b[2:0] cfg_c[4:0] cfg_d
    For more study, I recommend: http://www.perlmonks.org/index.pl/Tutorials#Pattern-Matching-Regular-Expressions-and-Parsing
Re: To find and replace in a file
by hippo (Archbishop) on Feb 25, 2017 at 09:47 UTC
    How to do them ? Please give me a hint

    Do you want a hint with the algorithm or do you want a hint with the code? What have you already tried?

    Generic Hint: Start with a smaller problem and work up. eg. suppose instead of changing the order of the contents you just wanted to do the prepending with "logic " - do that first as it should be pretty easy given the resources of perlintro, perlrun and print. Can you construct an algorithm to do this? Can you convert the algorithm into code?

    We cannot help you if we don't know what's holding you back.

Re: To find and replace in a file
by Laurent_R (Canon) on Feb 25, 2017 at 09:44 UTC
    Cross posted on the Perl Guru forum: http://perlguru.com/gforum.cgi?post=83831;sb=post_latest_reply;so=ASC;forum_view=forum_view_collapsed;;page=unread#unread.

    There is nothing wrong with cross-posting questions, but it is considered polite to inform the reader (and provide the link) to prevent duplication of work in various location of the Internet.

    For reference, here's the one-liner solution I had posted on that other site before seeing this post here:

    $ echo 'cfg_a cfg_b[2:0] cfg_c[4:0] cfg_d ' | perl -ne '($cf, $num) = ($1, $2) if /(cfg_\w+)(\[\d:\d\]) +*/; print "logic $num $cf\n"; ' logic cfg_a logic [2:0] cfg_b logic [4:0] cfg_c logic cfg_d
Re: To find and replace in a file
by Anonymous Monk on Feb 25, 2017 at 03:21 UTC

    Hi

    I have two hints

    Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!

    Use Path::Tiny for convenience , perlintro for basic substitution

      Advocacy is one thing, but you've got an issue in your response me thinks...

        Advocacy is one thing, but you've got an issue in your response me thinks...

        Hi,

        What do you mean stevieb?

        OP Asked for hints I gave exactly such

        When I hit reply there were no code tags used, OP appears to have figured that on his own real quick