Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Comparing values from two different files and flag error

by roboticus (Chancellor)
on Aug 29, 2019 at 14:56 UTC ( #11105244=note: print w/replies, xml ) Need Help??


in reply to Comparing values from two different files and flag error

anirbanphys:

Sorry, but this is turning out to be a longish post. Anyway, I've read your post, but don't know exactly what you're having trouble with. It's nice to see that you've provided some sample data and some code, but you didn't make it easy enough for (most) people to try to help. I happened to be in the right mood to slog through it, so you at least get a response from me, if no others. The easier you make it for people to try your code, the more likely it is that people will give it a go and respond. As it is, you have to slog through all the code to figure out how to build the file directory, and how to figure out what parameters to call it with. I'm not certain I guessed at everything correctly, but here goes!


OK, when I tried running your code with "perl -c pm_11105228.pl", it had a couple complaints:

$ perl -c pm_11105228.pl Unrecognized escape \v passed through at pm_11105228.pl line 47. Unrecognized escape \v passed through at pm_11105228.pl line 72. Scalar value @vol_temp[1] better written as $vol_temp[1] at pm_1110522 +8.pl line 38. Scalar value @vol_temp[0] better written as $vol_temp[0] at pm_1110522 +8.pl line 39. Scalar value @vol_temp[0] better written as $vol_temp[0] at pm_1110522 +8.pl line 41. Scalar value @vol_temp[0] better written as $vol_temp[0] at pm_1110522 +8.pl line 42. pm_11105228.pl syntax OK

Lines 47 and 72 have the same issue. Looking at line 47, we get:

my $file_name2 = "$in_macro/lib_pg/$in_macro\_$process_two$volt_1\v$te +mp\_pg.lib";

The problem is that you're using backslashes ('\') instead of forward slashes ('/') in your filename, so I just changed it to:

my $file_name2 = "$in_macro/lib_pg/$in_macro/_$process_two$volt_1/v$te +mp/_pg.lib";

I also made a similar fix to line 72. Perl generally "just works" if you use forward slashes to delimit the parts of path names. If you wanted to use backslashes, then you'd need to be sure you escaped your backslashes properly, which is a nuisance I avoid entirely by using forward slashes exclusively.

For the warnings on lines 38 through 42, it's telling you what to do to fix the problem, so I made those changes, too.

Since you don't use strict and warnings in your program, I was expecting more issues to crop up when I added them. However, I was pleasantly surprised to see that other than the issues listed, everything was fine. I left in strict and warnings, though, as it's an easy way to prevent errors.

Your directory structure and file naming was a bit much for me to try to reproduce, so I just made a subdirectory lib_pg and dumped your sample files in there. Then, to make it so that the code would find the files, I changed lines 46 and 47 to:

my $file_name1 = "lib_pg/$in_macro"; my $file_name2 = "lib_pg/$in_macro";

At this point, the code ran, discovered the files and printed some stuff. But I don't know if I structured everything correctly or not--this is where you could've done a little more work to make it easy on us. Anyway, it printed some stuff. However, I don't know (a) which lines in the code printed stuff, nor (b) what the correct content would be. To handle (a), I added a bit of info to the print statements, so I could see what was printing what, in an attempt to get a little further.

Your indentation was messy and inconsistent, so I cleaned that up a bit. I then noticed that the last section of your code was essentially duplicated, so I pulled it out into a subroutine to handle things.

I also noticed a few other things:

  • You use the three-argument form of open, which is good, but you should really switch to using $variables rather than FILEHANDLES, so they can automatically close when they go out of scope, and to make it easier to pass them around. Additionally, when perl gets a file open error, it puts additional information in the $! variable, so you should print that when you have an error, to make debugging easier. To that end, I changed:
    open(FILEHANDLE,"<",$file_name) || die "Can not open $file_name";

    to look like:

    open my $file_handle, "<", $file_name or die "Can not open $file_name: $!";
  • When using the three-argument form of open, you're using parenthesis. There's no harm in that, but it's more "perlish" to omit those parenthesis. The problem with omitting the parenthesis, though, is that you have to properly handle the failing case. You're sometimes using " or " and others you use " || ". If you always use the " or ", then you don't need the parenthesis. Otherwise you'll need them because the " || " would become part of the final argument. (The "||" operator has higher priority than "," so it would join the final argument, while the "or" operator has a low priority, so it would be applied to the open statement itself.)
    so I changed code like:
    open(FOO,"<",$file_name) || die "error opening $file_name";

    to be like:

    open my $FOO, "<", $file_name or die "error opening $file_name: $!";
  • You tend to read entire files into arrays, which is fine, but then you wind up processing them line-by-line. The problem is that if you don't need the entire file at once and you work with large files, you may encounter memory problems. To avoid this, when you're processing a file line-by-line, you can just read a line, process it, and then go to the next line. That way, if you *do* start working with large files, it'll simply work. You're also introducing a variable use *only* to fetch the line from your array, which you wouldn't need. So I changed code like this:
    my @data = <LIBFILE>; for (my $j=0; $j<=$#lib_file-1; $j++) { my $libline = $lib_file[$j]; ... do stuff with $libline ... }

    to code that looks more like this:

    while (my $libline = <$LIBFILE>) { ... do stuff with $libline ... }
  • You often wrap string variables with quotes when you use them, and you don't need to do that. For example:
    open(ALPHAFILE,"<",$alpha_config);

    works the same as:

    open(ALPHAFILE,"<","$alpha_config");

    It's not an error to do that, but it's a bit of mental overhead: I generally only add the quotes if I'm trying to add some data to the string, for example if I'm adding a path prefix or a file extension:

    open(ALPHAFILE,"<","./lib_pg/$alpha_config");
  • After matching some data against a regular expression, you then capture the values into variables. I'd suggest you do it all at once, though, to prevent bugs in the future. As an example, you have:
    if ($pvt_name =~m/^([a-z].*)([0-9]p.*)/) { my $process = $1; my $process_two = substr($process, 0, 2); #got ff from ffgp my $vol_temp = $2; my @vol_temp = split ('v',$vol_temp);
    While this is OK, if the code grows a bit and then you change the code for the first value, you may accidentally destroy your captured values:
    my $process_two = $1; $process_two =~ s/^(..).*/$1/; # destroys the old value of $2!
    so I'd suggest just capturing them immediately if you're going to do further processing on them, like:
    if ($pvt_name =~m/^([a-z].*)([0-9]p.*)/) { # We capture all the data we want my ($process, $vol_temp) = ($1, $2); # now we can just do the work without worrying about using another + regular expression my $process_two = substr($process, 0, 2); #got ff from ffgp my @vol_temp = split ('v',$vol_temp);

I stopped here, because I got the code in a nice enough state to work on. But I don't really know what you're asking for help with, at the moment. It looks like you've done most of the work in parsing the files and collecting the data. You're not doing anything with the data though. So I don't know if you're asking for help in storing the data in a structured form, or how to figure out what to compare with what, or exactly what the issue is.

I'll leave you with the code I ended up with:

use strict; use warnings; my $in_macro = $ARGV[0]; my $alpha_config = $ARGV[1]; if ($#ARGV!=1) { print "USAGE :: perl bias_vol_chk.pl <<CELL_NAME>> <<config_FILE> +> \n\n"; exit(1); } my $pvt_name; my $bias_voltage; my @bias_voltage; open my $ALPHAFILE, "<", $alpha_config or die "Can not open config_FIL +E"; while (my $line = <$ALPHAFILE>) { chomp $line; print "alpha[$.]: $line \n"; if ($line =~m/\s*cornerData\((.*)\)\s*\{(.*),TEMP\s*(.*)/g) { $pvt_name = $1; $bias_voltage = $2; print " PVT_NAME: $pvt_name\n"; print " BIAS_V: $bias_voltage\n"; @bias_voltage = split (',',$bias_voltage); if ($pvt_name =~m/^([a-z].*)([0-9]p.*)/) { my ($process, $vol_temp) = ($1, $2); my $process_two = substr($process, 0, 2); #got ff from ffg +p my @vol_temp = split ('v',$vol_temp); my $temp = $vol_temp[1]; my $volt_1 = $vol_temp[0]; $volt_1 =~ s/[0]$//; $vol_temp[0] =~ s/[0]$//; my $volt_2 = $vol_temp[0]; my @volt_2 = split('p',$volt_2); $volt_2 = join('.',@volt_2); #replacing p with . my $file_name1 = "lib_pg/$in_macro"; print " FNAME 1: $file_name1\n"; if (-e $file_name1) { my $data = handle_library_file($file_name1); # Do something with the $data } my $file_name2 = "lib_pg/$in_macro"; print " FNAME 2: $file_name2\n"; if (-e $file_name2) { my $data = handle_library_file($file_name2); # Do something with the $data } } } } sub handle_library_file { my $file_name = shift; open my $LIBFILE, "<", $file_name or die "Can not open INPUT liber +ty file $file_name: $!"; while (my $libline = <$LIBFILE>) { chomp $libline; if ($libline=~m/^\s*voltage_map\((.*)\)/g) { my @volt_array = ("$1"); my $lib_volt = $1; my @lib_volt = split (',',$lib_volt); print "FILE lib_volt: @lib_volt \n"; } } # After gathering and processing the data, return it! }

And if anyone else has any guesses as to what to do, I took his second code chunk, named it 'mnk_alpha' and put it in the same directory as the script (which I called 'pm_1115228.pl'). I then created a subdirectory named 'lib_pg' and dropped his third, fourth and fifth code chunks in that directory named 'ss0p905v100c', 'ffg0p82v100c' and 'tt0p4v100c', respectively. I'm pretty sure I got the $file_name1 and $file_name2 bit wrong, but at this point the question became unclear enough and I have written enough, that I'm kinda done with it, pending further input from the OP.

A sample run gives me:

$ perl pm_11105228.pl ffg0p82v100c mnk_alpha alpha[1]: put cornerData(ffg0p82v100c) {VDD 0.825,VDDQN 1.17,VDDM 1.17 +,TEMP 100} PVT_NAME: ffg0p82v100c BIAS_V: VDD 0.825,VDDQN 1.17,VDDM 1.17 FNAME 1: lib_pg/ffg0p82v100c FILE lib_volt: VDDQN 1.1 FILE lib_volt: VDDM 1.1 FILE lib_volt: VSS 0 FNAME 2: lib_pg/ffg0p82v100c FILE lib_volt: VDDQN 1.1 FILE lib_volt: VDDM 1.1 FILE lib_volt: VSS 0 alpha[2]: put cornerData(ssg0p905v100c) {VDD 0.825,VDDQN 1.1,VDDM 0.17 +,TEMP 100} PVT_NAME: ssg0p905v100c BIAS_V: VDD 0.825,VDDQN 1.1,VDDM 0.17 FNAME 1: lib_pg/ffg0p82v100c FILE lib_volt: VDDQN 1.1 FILE lib_volt: VDDM 1.1 FILE lib_volt: VSS 0 FNAME 2: lib_pg/ffg0p82v100c FILE lib_volt: VDDQN 1.1 FILE lib_volt: VDDM 1.1 FILE lib_volt: VSS 0 alpha[3]: put cornerData(tt0p40v100c) {VDD 0.825,VDDQN 1.07,VDDM 2.15, +TEMP 100} PVT_NAME: tt0p40v100c BIAS_V: VDD 0.825,VDDQN 1.07,VDDM 2.15 FNAME 1: lib_pg/ffg0p82v100c FILE lib_volt: VDDQN 1.1 FILE lib_volt: VDDM 1.1 FILE lib_volt: VSS 0 FNAME 2: lib_pg/ffg0p82v100c FILE lib_volt: VDDQN 1.1 FILE lib_volt: VDDM 1.1 FILE lib_volt: VSS 0 alpha[4]:

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Comparing values from two different files and flag error
by anirbanphys (Beadle) on Aug 29, 2019 at 17:26 UTC

    Thank you roboticus for reading the post and providing some valuable feedback. You have justified things rightly. I agree with your points. The fact is, I am not at all good at this language. For the purpose of automation I use perl and I seek help Monks from Perl Monks :). But I can assure you I noted all the valuable feedback and this will always be reflected for my next perl assignment.

    Let me explain you what is the expectation from the code.

    I need to compare the different bias voltages, one which we picked up from "mnk_alpha" and the other from the files, inside of lib_pg directory.For example "put cornerData(ffg0p82v100c) {VDD 0.825,VDDQN 1.17,VDDM 1.17 +,TEMP 100} " line bias voltages for the given PVT ffg0p82v100c of mnk_alpha file and the corresponding file (file name must be <cell_name>_PVT_pg.lib, here the cell name is "mnk_lib" and PVT is ff0p82v100c. We are omitting the word "g") inside mnk_lib/lib_pg/mnk_lib_ff0p82v100c_pg.lib, if it is wrong it will flag an ERROR message. Beside this if any bias voltages are missing from any of the file, it will also display the message. Please follow the desired output in the terminal or in a separate file after running the code.

    DESIRED OUTPUT

    PVT === ffg0p82v100c File mnk_alpha ==== VDD 0.825,VDDQN 1.17,VDDM 1.17 File mnk_lib_ff0p82v100c_pg.lib ===== VDDQN 1.1, VDDM 1.1,VSS 0 Bias voltage differences has been observed for VDDQN, VDDM. Missing Bias voltages VSS from mnk_alpha file. VDD from mnk_lib_ff0p82v100c_pg.lib file PVT === ssg0p905v100c File mnk_alpha === VDD 0.825,VDDQN 1.1,VDDM 0.17 File mnk_lib_ss0p905v100c_pg.lib ===== VDDQN 1.1, VDDM 1.1,VSS 0 Bias voltage differences has been observed for VDDQN, VDDM. Missing Bias voltages VSS from mnk_alpha file. VDD from mnk_lib_ss0p905v100c_pg.lib file PVT === tt0p40v100c File mnk_alpha === VDD 0.825,VDDQN 1.07,VDDM 2.15 File mnk_lib_tt0p40v100c_pg.lib ===== VDDQN 1.1, VDDM 1.1,VSS 0 Bias voltage differences has been observed for VDDQN, VDDM. Missing Bias voltages VSS from mnk_alpha file. VDD from mnk_lib_tt0p40v100c_pg.lib file

    At time of posting I was not very clear what should be the desired output and please accept my sincere apologies for this.

    Few things I want to explain more to avoid confusions :)

    1. The file inside <Cell>/lib_pg/ directory could be mnk_lib_ss0p905v100c_pg.lib or mnk_lib_ssg0p905v100c_pg.lib. And that is why I did some operations in the first posted code.

    2. The code which I posted at the beginning, actually just grepping the bias voltages but not comparing.

    I hope this can give some light to understand the problem. I updated the code, and this look like this ::
    use strict; use warnings; my $in_macro = $ARGV[0]; my $alpha_config = $ARGV[1]; if ($#ARGV!=1) { print "USAGE :: perl bias_mnk.pl <<CELL_NAME>> <<config_FILE>> \n +\n"; exit(1); } my $pvt_name; my $bias_voltage; my @bias_voltage; open my $ALPHAFILE, "<", $alpha_config or die "Can not open config_FIL +E"; while (my $line = <$ALPHAFILE>) { chomp $line; print "alpha[$.]: $line \n"; if ($line =~m/\s*cornerData\((.*)\)\s*\{(.*),TEMP\s*(.*)/g) { $pvt_name = $1; $bias_voltage = $2; print " PVT_NAME: $pvt_name\n"; print " BIAS_V: $bias_voltage\n"; @bias_voltage = split (',',$bias_voltage); if ($pvt_name =~m/^([a-z].*)([0-9]p.*)/) { my ($process, $vol_temp) = ($1, $2); my $process_two = substr($process, 0, 2); #got ff from ffg +p my @vol_temp = split ('v',$vol_temp); my $temp = $vol_temp[1]; my $volt_1 = $vol_temp[0]; $volt_1 =~ s/[0]$//; $vol_temp[0] =~ s/[0]$//; my $volt_2 = $vol_temp[0]; my @volt_2 = split('p',$volt_2); $volt_2 = join('.',@volt_2); #replacing p with . my $file_name1 = "$in_macro/lib_pg/$in_macro\_$pvt_name\_p +g.lib"; if (-e $file_name1) { print " FNAME 1: $file_name1\n"; my $data = handle_library_file($file_name1); # Do something with the $data } my $file_name2 = "$in_macro/lib_pg/$in_macro\_$process_two +$volt_1\v$temp\_pg.lib"; if (-e $file_name2) { print " FNAME 2: $file_name2\n"; my $data = handle_library_file($file_name2); # Do something with the $data } } } } sub handle_library_file { my $file_name = shift; open my $LIBFILE, "<", $file_name or die "Can not open INPUT liber +ty file $file_name: $!"; while (my $libline = <$LIBFILE>) { chomp $libline; if ($libline=~m/^\s*voltage_map\((.*)\)/g) { my @volt_array = ("$1"); my $lib_volt = $1; my @lib_volt = split (',',$lib_volt); print "FILE lib_volt: @lib_volt \n"; } } # After gathering and processing the data, return it! }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2020-06-03 13:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (23 votes). Check out past polls.

    Notices?