perlquestion
suvendra123
<p>
Hi Monks,
</p>
<p>
I have a text file xyz.txt which contains
</p>
<c>
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);
</c>
<p>
Another text file i.e pqr.txt which contains
</p>
<c>
-parallel 3 -words 96 -bits 131 -mux 4 -bank 1
</c>
<p>
My code is as below
</p>
<readmore>
<code>
#!/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.
};
}
</code>
<p>
I am getting output as
</p>
<c>
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;
</c>
<p>
But I want output as
</p>
<c>
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);
</c>
<p>
Where is the mistake ,
<br/>Command I am using to run is
<c>
perl abc.pl pqr.txt
</c>
<p>
where the pqr.txt contains
</p>
<c>
-parallel 3 -words 96 -bits 131 -mux 4 -bank 1
</c>
</readmore>
Thanks
</p>