Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-08-15 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?