Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

regex statement to call variable value is not working??

by sumathigokul (Acolyte)
on May 13, 2015 at 07:40 UTC ( #1126512=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks..

Good day to all.....

Following perl code with regex statement is not working..i.e. regex statement written to call value from file inside file is not working..why is it so???

Have any one you also encountered the same problem??? Both of the following codes are not working....i.e. it is not printing the word "matched"...

use strict; use warnings; open (IN1, "<design_modify1.vhd") or die; open (IN2, "<nets.txt") or die; my @nets = <IN2>; while (<IN1>) { foreach my $i (0..$#nets){ print "matched" if m/$nets[$i]/; } }
use strict; use warnings; open (IN1, "<design_modify1.vhd") or die; open (IN2, "<nets.txt") or die; my @nets = <IN2>; while (<IN1>) { foreach my $i (0..$#nets){ my $var1= $nets[$i]; print "matched" if m/$var1/; } }

For your reference, i am attaching other two files also here........

design_modify1.vhd has following lines of codes enable_pad : INBUF port map (PAD => enable, Y => enable_c); c_pad : INBUF port map(PAD => c, Y => c_c); b_pad : INBUF port map(PAD => b, Y => b_c); sum_pad : OUTBUF port map(D => sum_c, PAD => sum); VCC_i_0 : VCC port map(Y => VCC_0); VCC_i : VCC port map(Y => \VCC\); sum_1_SUM0_0 : XOR3 port map(A => b_c, B => a_c, C => c_c, Y => su +m_c); carry_pad : OUTBUF port map(D => N_5, PAD => carry); a_pad : INBUF port map(PAD => a, Y => a_c); GND_i_0 : GND port map(Y => GND_0); GND_i : GND port map(Y => \GND\); sum_1_CO0_i : MAJ3 port map(A => a_c, B => c_c, C => b_c, Y => N_5 +);
nets.txt file has following codes... a_c b_c c_c

Replies are listed 'Best First'.
Re: regex statement to call variable value is not working??
by Corion (Pope) on May 13, 2015 at 07:42 UTC

    Each element of @nets contains a newline character, while your target strings don't contain the newline character. See chomp.

      Thank you... Its working now....

        You've been told about chomp several times now, in relation to this one problem (as noted here). The purpose of this site is to help people learn, not to do work for them. Before posting again review the your existing threads.

        Hi Corion..

        The code is working fine, but not does my intention function...i modified the code for my need based on your comments as followed...

        use strict; use warnings; open (IN1, "<design_modify1.vhd") or die; open (OUT, ">output_file.vhd") or die; open (IN2, "<nets.txt") or die; open (IN3, "<enabled_nets.txt") or die; my @nets = <IN2>; my @enabled_nets = <IN3>; chomp @nets; chomp @enabled_nets; while (<IN1>) { print OUT; foreach my $i (0..$#nets){ print OUT if (s/\=\>\s+$nets[$i]\,/\=\> $enabled_nets[$i]\,/); #} } close (IN1); close (OUT); close (IN2); close (IN3);

        I want the output file as follows.. (shown only the modifiable lines)

        VCC_i : VCC port map(Y => \VCC\); sum_1_SUM0_0 : XOR3 port map(A => b_e, B => a_e, C => c_e, Y => su +m_c); carry_pad : OUTBUF port map(D => N_5, PAD => carry); a_pad : INBUF port map(PAD => a, Y => a_c);

        but it is printing as follows.. I understood the mistake in the code that since i wrote the print statement inside for loop, it gets only one element of array at a time.. So, do you have any suggestions for this???

        VCC_i : VCC port map(Y => \VCC\); sum_1_SUM0_0 : XOR3 port map(A => b_c, B => a_c, C => c_c, Y => su +m_c); sum_1_SUM0_0 : XOR3 port map(A => b_c, B => a_e, C => c_c, Y => su +m_c); sum_1_SUM0_0 : XOR3 port map(A => b_e, B => a_e, C => c_c, Y => su +m_c); sum_1_SUM0_0 : XOR3 port map(A => b_e, B => a_e, C => c_e, Y => su +m_c); carry_pad : OUTBUF port map(D => N_5, PAD => carry); a_pad : INBUF port map(PAD => a, Y => a_c);
Re: regex statement to call variable value is not working??
by vinoth.ree (Monsignor) on May 13, 2015 at 08:43 UTC
    Hi sumathigokil,

    As corion pointed out the problem, here I am just adding extra information for you.

    Three-argument open()

    There are two forms of the open() function in Perl 5. The modern version takes three arguments: the filehandle to open or vivify, the mode of the filehandle, and the name of the file.

    The legacy version has two arguments, only the filehandle and the name of the file. The mode of the file comes from the filename; if the filename starts (or ends) with any of several special characters, open() parses them off and uses them.

    If you accidentally use a filename with those special characters with the two-arg form of open(), your code will not behave as you expect. This is especially a problem if you're not careful about sanitizing user input, and if any user input ever becomes part of a filename. Consider:

    open my $fh, ">$filename" # INSECURE CODE; do not use or die "Can't write to '$filename': $!\n";

    While this code appears to open $filename for writing, an insecure $filename could start with > to force appending mode, or - to open STDOUT (though I suspect you have to work really hard to force this). Likewise, code without any explicit mode in the second and final parameter is susceptible to any special mode characters.

    Extracting file modes into a separate parameter to this function prevents Perl from parsing the filename at all and removes the possibility for this unintentional behavior. As Damian Conway has mentioned, using a separate file mode parameter also makes the intention of the code clearer:

    open my $fh, '>', $filename # safer and clearer or die "Can't write to '$filename': $!\n";

    The modern version of this code is safer and clearer, and it's been available since Perl 5.6.0

    sourced from http://modernperlbooks.com/mt/2010/04/three-arg-open-migrating-to-modern-perl.html

    change the line my @nets = <IN2>; to chomp (@nets = (<IN2>));


    All is well. I learn by answering your questions...
Re: regex statement to call variable value is not working??
by Anonymous Monk on May 13, 2015 at 08:35 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2020-11-26 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?