Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Output not correct

by nursyza (Novice)
on Nov 26, 2018 at 18:41 UTC ( [id://1226380]=perlquestion: print w/replies, xml ) Need Help??

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

hi, firstly this is the text file that my program is currently reading

INPUT N1, N2, N3, N6, N7; OUTPUT N22, N23; WIRE N10, N11, N16, N19; NAND NAND2_1 (N10, N1, N3); NAND NAND2_2 (N11, N3, N6); NAND NAND2_3 (N16, N2, N11); NAND NAND2_4 (N19, N11, N7); NAND NAND2_5 (N22, N10, N16); NAND NAND2_6 (N23, N16, N19); ENDMODULE

below is the code

#!/usr/bin/perl use warnings; use strict; my $filename = 'c:\Users\aqids\Desktop\SCOAP\module.txt'; my $INPUT_DATA; my @test_input_array; open(FH, '<', $filename) or die "Cannot open file!"; while(<FH>) { #READ MODULE NAME my $MODULE_NAME = $_; my @test_input_array; if (defined($MODULE_NAME) && ($MODULE_NAME =~ /(MODULE \w+) /)) { my $module_name = $1; print "Module name = $module_name\n"; } #READ input N1,N2,N3,N6,N7; my $INPUT_DATA = $_; if (defined($INPUT_DATA) && ($INPUT_DATA =~ /INPUT (.*);/)) { my @input_array = split /,/, $1; @test_input_array = @input_array; my $size_input = @input_array; print "Primary input = "; print scalar "@input_array\n"; print "Number of primary input = $size_input\n"; } #READ output N22, N23; my $OUTPUT_DATA = $_; if (defined($OUTPUT_DATA) && ($OUTPUT_DATA =~ /OUTPUT (.*);/)) { my @output_array = split /,/, $1; my $size_output = @output_array; print "Primary output = "; print scalar "@output_array\n"; print "Number of primary output = $size_output\n"; } #READ wire N10, N11, N16, N19; my $WIRE_DATA = $_; if (defined($WIRE_DATA) && ($WIRE_DATA =~ /WIRE (.*);/)) { my @wire_array = split /,/, $1; my $size_wire = @wire_array; print "Wires = "; print scalar "@wire_array\n"; print "Number of wires = $size_wire\n\n"; } my $gate_DATA = $_; if (defined($gate_DATA) && ($gate_DATA =~ /(.*) (.*) \((.*),(.*),( +.*)\);/) && ($gate_DATA !~ /MODULE/) ) { my $gate_type = $1; my $gate_name = $2; my $gate_output = $3; my $input_A = $4; my $input_B = $5; print "Gate type = $gate_type\n"; print "Gate name = $gate_name\n"; print "Output = $gate_output\n"; print "Input A = $input_A\n"; print "Input B = $input_B\n"; if (grep { $input_A eq $_ } @test_input_array) { print "Yes\n"; print "@test_input_array\n\n"; } else { print "No\n\n"; } }

the problem right now is at....if (grep { $input_A eq $_ } @test_input_array)....as the reading for NAND2_1 should print out the first statement which is "Yes". However, it prints out No when it should be Yes. I am currently comparing the $input_A with INPUT N1, N2, N3, N6, N7. Meaning if $input_A contains any of the INPUT mention, it should print the first statement. Can someone help me fix it? Thanks.

Replies are listed 'Best First'.
Re: Output not correct
by choroba (Cardinal) on Nov 26, 2018 at 19:15 UTC
    I added the following line before the comparison:
    warn "<$input_A> in [@test_input_array]";

    And it shows you have two problems:

    < N1> in [] at ./1.pl line 68, <FH> line 4. No

    You probably don't want to include the space before N1 to the value, and you probably expect the array not to be empty.

    To help you with the second problem: check the scope of both the @test_input_array variables. You probably don't need one of them.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Output not correct
by tinita (Parson) on Nov 26, 2018 at 19:15 UTC
    Ok, this is called debugging and it will probably also help you in the future.
    When a pattern matching or a comparison or a grep does not work like you think it should, you can simply print the contents of the variable(s).
    use Data::Dumper; local $Data::Dumper::Useqq = 1; # show things like newlines, tabs as " +\n", "\t" print Dumper $input_A; print Dumper \@test_input_array;
    I suspect the elements in @test_input_array have spaces where you don't expect them (you split on a single comma and forget about the spaces).
Re: Output not correct
by hippo (Bishop) on Nov 26, 2018 at 19:08 UTC
    Can someone help me fix it?

    Your code as posted doesn't compile, so that's not a great start.

    Regardless, you try to match elements in an array to a scalar and are surprised when the match fails. But when it fails all it prints is "No\n\n" which isn't so useful. Why not print $imput_A and @test_input_array? That might make it obvious where you are going wrong.

    Other things which don't help your cause here are the inconsistent indentation and the hard-coded pathname which makes for more work for others who might want to help you.

      Every time I see posts formatted like this:

      sub foo { foreach my $thing (@_) { # do something } } print "Hello\n"; print "something else\n";

      I take for granted that there's no way the code is going to compile. Not that formatting has anything to do with compiling, but lack of formatting almost guarantees the sort of sloppy attention to detail that will assure compile-readyness is not a foregone conclusion.


      Dave

Re: Output not correct (Verilog::Netlist)
by toolic (Bishop) on Nov 27, 2018 at 02:22 UTC
    Your input file looks like Verilog, except that Verilog requires the keywords to be lower-case. If so, you can use the Verilog::Netlist parser to read the file and get all the signals you need. This is preferable to rolling your own parser, which can be difficult.
    use warnings; use strict; use Data::Dumper qw(Dumper); use Verilog::Netlist qw(); use File::Slurp qw(write_file read_file); my $file = shift; # Create a temporary file with lower-case keywords my @lines; for (read_file($file)) { s/(\w+)/lc $1/e; push @lines, $_ } write_file('temp', @lines); my $nl = Verilog::Netlist->new(); $nl->read_file(filename => 'temp'); $nl->exit_if_error(); my %sigs; my @wires; for my $mod ($nl->top_modules_sorted()) { print 'mod=', $mod->name, "\n"; for my $sig ($mod->nets()) { push @wires, $sig->name; } for my $sig ($mod->ports_sorted()) { my $dir = $sig->direction(); $dir .= 'put' unless $dir eq 'inout'; my $name = $sig->name(); $sigs{$name} = {dir => $dir}; } for my $cell ($mod->cells_sorted) { printf " Cell %s\n", $cell->name; for my $pin ($cell->pins_sorted) { printf " %s\n", $pin->netname; } } } print Dumper(\%sigs); print Dumper(\@wires);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-03-28 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found