Hi Monks,

I have a text file xyz.txt which contains

wire [130 : 0] dgrs_1; wire [130 : 0] dgrs_2; wire [130 : 0] mgs_1; wire [130 : 0] mgs_2; wire [130 : 0] pqr_1; wire [130 : 0] pqr_2; int abc; int pqr_1; int pqr_2; .dgrs_1(dgrs_1); .dgrs_2(dgrs_2); .mgs_1(mgs_1); .mgs_2(mgs_2);

Another text file i.e pqr.txt which contains

-parallel 3 -words 96 -bits 131 -mux 4 -bank 1

My code is as below

#!/usr/bin/perl use strict; use warnings; use File::Temp; use File::Copy qw(move); use Fcntl qw(:flock); ###################################################################### +############# open(my $in, '<', $ARGV[0]) or die "Unable to open for read: $!"; #} my $a; my $b; my $word_size; my $string_len; my $bank; my $mux_val; my $par_value; while (my $line = <$in>) { #============Address width======================== while ( $line =~ /-bits\s(\d+)/g ) { my $first = $1; $a=$first } #===========Data width============================ while ( $line =~ /-words\s(\d+)/g ) { my $second = $1; $word_size=$second; my $binary_number = sprintf ("%b",$second); $string_len = length($binary_number); } while ( $line =~ /-bank\s(\d+)/g ) { my $third = $1; $bank=$third; } while ( $line =~ /-mux\s(\d+)/g ) { my $fourth = $1; $mux_val=$fourth; } while ( $line =~ /-parallel\s(\d+)/g ) { my $fifth = $1; $par_value=$fifth; } while ( $line =~ /-seq\s(\d+)/g ) { my $sixth = $1; my $par_value=$sixth; } } print "$mux_val\n"; close $in; ###################################################################### +############# my $target_file1 = 'xyz.txt'; { open my $in_fh1, '<', $target_file1 or die "Cannot open input file +: $!\n"; flock $in_fh1, LOCK_EX | LOCK_NB or die "Cannot obtain a lock on $ +target_file1: $!\n"; my $temp_out = File::Temp->new(TEMPLATE => "$0-$$-XXXXX"); while ( my $line = <$in_fh1> ) { $line =~ s/dgrs_\d/dgrs/g; $line =~ s/mgs_\d/mgs/g; $line =~ s/pqr_\d/pqr/g; print $temp_out $line; } close $in_fh1 or die "Failed to close $target_file1. Aborting. $!\ +n"; $temp_out->flush; eval { move($temp_out->filename, $target_file1); } or do { warn "Failed to swap $temp_out into $target_file1: $!\n"; # There may need to be more cleanup here, or possibly a die is + more appropriate. }; } my $file = 'xyz.txt'; my %seen = (); { local @ARGV = ($file); local $^I = '.bac'; while(<>){ $seen{$_}++; next if $seen{$_} > 1; print; } } print "finished processing file."; my $target_file = 'xyz.txt'; { open my $in_fh1, '<', $target_file1 or die "Cannot open input file +: $!\n"; flock $in_fh1, LOCK_EX | LOCK_NB or die "Cannot obtain a lock on $ +target_file1: $!\n"; my $temp_out1 = File::Temp->new(TEMPLATE => "$0-$$-XXXXX"); while ( my $line = <$in_fh1> ) { if (my ($intro) = $line =~ m{ \A (.*dgrs) }xms) { print $temp_out1 "${intro}_$_;\n" for 1..$par_value; } if (my ($intro) = $line =~ m{ \A (".dgrs") }xms) { print $temp_out1 "${intro}_$_;\n" for 1..$par_value; } if (my ($intro) = $line =~ m{ \A (.*mgs) }xms) { print $temp_out1 "${intro}_$_;\n" for 1..$par_value; } if (my ($intro) = $line =~ m{ \A (.*pqr) }xms) { print $temp_out1 "${intro}_$_;\n" for 1..$par_value; } else { next if $line =~ m/.*dgrs/; next if $line =~ m/.*mgs/; next if $line =~ m/.*pqr/; print $temp_out1 $line; } } close $in_fh1 or die "Failed to close $target_file1. Aborting. $!\ +n"; $temp_out1->flush; eval { move($temp_out1->filename, $target_file1); } or do { warn "Failed to swap $temp_out1 into $target_file1: $!\n"; # There may need to be more cleanup here, or possibly a die is + more appropriate. }; }

I am getting output as

wire [130 : 0] dgrs_1; wire [130 : 0] dgrs_2; wire [130 : 0] dgrs_3; wire [130 : 0] mgs_1; wire [130 : 0] mgs_2; wire [130 : 0] mgs_3; wire [130 : 0] pqr_1; wire [130 : 0] pqr_2; wire [130 : 0] pqr_3; int abc; int pqr_1; int pqr_2; int pqr_3; .dgrs(dgrs_1; .dgrs(dgrs_2; .dgrs(dgrs_3; .mgs(mgs_1; .mgs(mgs_2; .mgs(mgs_3;

But I want output as

wire [130 : 0] dgrs_1; wire [130 : 0] dgrs_2; wire [130 : 0] dgrs_3; wire [130 : 0] mgs_1; wire [130 : 0] mgs_2; wire [130 : 0] mgs_3; wire [130 : 0] pqr_1; wire [130 : 0] pqr_2; wire [130 : 0] pqr_3; int abc; int pqr_1; int pqr_2; int pqr_3; .dgrs(dgrs_1); .dgrs(dgrs_2); .dgrs(dgrs_3); .mgs_1(mgs_1); .mgs_2(mgs_2); .mgs_3(mgs_3);

Where is the mistake ,
Command I am using to run is

perl abc.pl pqr.txt

where the pqr.txt contains

-parallel 3 -words 96 -bits 131 -mux 4 -bank 1
Thanks


In reply to Unable to find my mistake by suvendra123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.