Adam Cruge

Broken In
Can you please help me with Perl programming.
I have an input file, I have to make it this type of output file.
But my problem is how to calculate and test those conditions. Please help me.


Question: Write a script for the following: User will input an ibis file (sample file is attached. Please don’t worry much about the term “ibis”), in which you will have four columns: Voltage, I(typ), I(min), I(max) for [POWER Clamp] and [GND Clamp].
For [GND Clamp], if a current value [Ityp/Imin/Imax] is positive or if it’s modulus is less than 10nA, make it to 0(zero).
Similarly, for [POWER Clamp], if a current value [Ityp/Imin/Imax] is negative or if it’s modulus is less than 10nA, make it to 0(zero).
All lines beginning with ‘|’ is comment and thus you don’t have to operate on that line.
Also, if two consecutive lines have identical values for ALL current columns, you can delete the latter line provided it is not the last line in the clamp table. “[GND Clamp]” & [POWER Clamp] are keywords in an ibis file.
Please have a look at input.txt & output.txt attached with the mail. You will get a better idea about the task then.
 
OP
A

Adam Cruge

Broken In
I have stored each word into an array called @word. And now I want to compare these words using "if"
suppose $word[$i] contains a number like -9.345e-9
I am doing this way if ( $word[$i] > 0 ){
But it is not working. Please help.
 
OP
A

Adam Cruge

Broken In
|
[GND Clamp]
|
|
| Voltage I(typ) I(min) I(max)
|
-2 -0.111051 -0.0620698 -0.124743
-1.9998 -0.111028 -0.0620569 0.3457
-0.174 -3.14446e-09 -1.9459e-07 -7.99283e-09

PHP:
#! /usr/local/bin/perl 


$GNDpointer=0;
$POWERpointer=0;

@word = 0;
$file= $ARGV[0];
if (defined($file)){
	print "The input file is passed as a command line argument. \n";
	$num= @ARGV;
	if ( $num ==1 ){
		print "$num file is given as an input. \n";
	}
	else{
		print "$num files are given as an input. \n";
		print "But this script can handle a single file at a time, so the first file should be treated as an input.\n";
	}
	open (FILE, "$file") or die "ERROE: Can't locate file: $! \n";
	if  ( !-e $file){
	print "ERROR: No file found: $! \n";
	exit;
	}
	if  ( -d $file){
	print "ERROR: No file found: $! \n";
	exit;
	}
	if  ( -z $file){
	print "ERROR: File is empty: $! \n";
	exit;
	}
}
else{
	print "Enter the name of the file with proper extention: ";
	chomp( $file = <STDIN> );
	open (FILE, "$file") or die "ERROE: Can't locate file: $! \n";
	if  ( -d $file){
		print "ERROR: No file found: $! \n";
		exit;
	}
	if  ( -z $file){
	print "ERROR: File is empty: $! \n";
	exit;
	}
}	


while (<FILE>){
	push @line, $_;
}
close FILE;
open FILE, ">output.txt";
for ( $i=0; $i<@line; $i++ ){
	for $w( split / /, $line[$i] ){
		push @word, $w;
	}
	if( $word[0] eq "[GND" ){
		$GNDpointer= $i+1;
	}
	if( $word[0] eq "[POWER" ){
		$POWERpointer= $i+1;
	}
	@word= ();
}	
for ( $i=0; $i<$GNDpointer; $i++ ){
	print FILE "$line[$i]";
}
close FILE;
open FILE, ">>output.txt";
$x= $GNDpointer;
$y= $POWERpointer ;
for( $i=$x; $i< $y; $i++ ){
	for $ln (split //, $line[$i]){
		push @character, $ln;
	}
	print "$character[0] \n";
	if ( $character[0] eq "|" ){
		print FILE "$line[$i]";
	}
}
----------------------------------------------------------------------

OUTPUT
Code:
|
[GND Clamp]
|
|
| Voltage I(typ) I(min) I(max)
|
-2 -0.111051 -0.0620698 -0.124743 
-1.9998 -0.111028 -0.0620569 0.3457 
-0.174 -3.14446e-09 -1.9459e-07 -7.99283e-09

-------------------------------------------------------------
EXPECTED OUTPUT
Code:
|
[GND Clamp]
|
|
| Voltage I(typ) I(min) I(max)
|
 
Last edited by a moderator:

ninad_mhatre85

Journeyman
Read the comments below

|
[GND Clamp]
|
|
| Voltage I(typ) I(min) I(max)
|
-2 -0.111051 -0.0620698 -0.124743
-1.9998 -0.111028 -0.0620569 0.3457
-0.174 -3.14446e-09 -1.9459e-07 -7.99283e-09

PHP:
#! /usr/local/bin/perl 


$GNDpointer=0;
$POWERpointer=0;

@word = 0;
$file= $ARGV[0];
if (defined($file)){
	print "The input file is passed as a command line argument. \n";
	$num= @ARGV;
	if ( $num ==1 ){
		print "$num file is given as an input. \n";
	}
	else{
		print "$num files are given as an input. \n";
		print "But this script can handle a single file at a time, so the first file should be treated as an input.\n";
	}

        #  --- Why are you opening a file before checking if file exist or not ?? 
        #  --- ALWays specify what kinfd of IO operation you are doing ... 
        #  --- open(FILE,'<', $file ) - I am reading a file 
	
        open (FILE, "$file") or die "ERROE: Can't locate file: $! \n";

        #  --- from here 
	if  ( !-e $file){
	print "ERROR: No file found: $! \n";
	exit;
	}
	if  ( -d $file){
	print "ERROR: Input should be file and not directory : $! \n";
	exit;
	}
	if  ( -z $file){
	print "ERROR: File is empty: $! \n";
	exit;
	}
        #  --- till here should be before open statement ...
}
else{
	print "Enter the name of the file with proper extention: ";
	chomp( $file = <STDIN> );

	open (FILE, "$file") or die "ERROE: Can't locate file: $! \n";

        #  --- SAME GOES here , file checks should be before open 

	if  ( -d $file){
		print "ERROR: No file found: $! \n";
		exit;
	}
	if  ( -z $file){
	print "ERROR: File is empty: $! \n";
	exit;
	}
}	


## --- You are reading a file in an array 
## --- You can directly read the file in array
while (<FILE>){
	push @line, $_;
}
close FILE;

open FILE, ">output.txt";

# --- You can directly read the file in array
for ( $i=0; $i<@line; $i++ ){
	for $w( split / /, $line[$i] ){
		push @word, $w;
	}
	if( $word[0] eq "[GND" ){
		$GNDpointer= $i+1;
	}
	if( $word[0] eq "[POWER" ){
		$POWERpointer= $i+1;
	}
	@word= ();
}	
for ( $i=0; $i<$GNDpointer; $i++ ){
	print FILE "$line[$i]";
}
close FILE;
open FILE, ">>output.txt";
$x= $GNDpointer;
$y= $POWERpointer ;
for( $i=$x; $i< $y; $i++ ){
	for $ln (split //, $line[$i]){
		push @character, $ln;
	}
	print "$character[0] \n";
	if ( $character[0] eq "|" ){
		print FILE "$line[$i]";
	}
}
----------------------------------------------------------------------

I am not going to change the logic of your code, just tweak your code ( Below is your code , smaller size )

PHP:
#! /usr/local/bin/perl 


my ($GNDpointer,$POWERpointer) = (0,0) ;

my $file = '' ;

# Check if argument is there , if it is then assume its file name otherwise ask user to give file name...
if ( scalar @ARGV == 1 )
{
   $file= $ARGV[0];
} else
{
    chomp ( $file = <STDIN> ) ;
}

# Checking if file exists & not directory & readable & not empty
# unless -> if not 
unless ( -e $file || -r $file || -z $file || -d $file )
{
    print "Error : $file does not exists / readable / empty / directory\n" ;
    exit 1 ; 
}

# Opening a file .


open(IN_FILE,'<',$file) or die "Can not open file Error : $!\n" ;
   chomp ( my @line = <IN_FILE> ) ;  # I read file in an array 
close (IN_FILE);

open (OUT_FILE,'>',"output.txt") or die "Can not open output file .. Error : $!\n" ;

##############################################
### 1st approach ( your code tweaked !! )
##############################################

my $i = 0 ;
# Use foreach loop
foreach ( @line )
{
    $GNDpointer   = $i+1 if ( $_ =~ /^\[GND/ ) ; 
    $POWERpointer = $i+1 if ( $_ =~ /^\[POWER/ ) ;
    $i++ ; 
}

# Print Lines till GND point in file ..
$" = "\n" ; 
print OUT_FILE @line[0..$GNDpointer] ;

my @InBwContents = @line[$GNDpointer..$POWERpointer] ;

foreach ( @InBwContents )
{
    print OUT_FILE "$_\n" if ( $_ =~ /^|/ ) ; # Print if line starts with |    
}

close(OUT_FILE) ; 

#################################################
### 2nd approach ( Very compact ) UNCOMMENT LINES 
#################################################

#foreach ( @line )
#{
#    if ( $_ =~ /^\[POWER/ )
#    {
#        print OUT_FILE "$_\n" ;
#        last ; 
#    }
#    
#    if ( $_ =~ /^\[GND/ )
#    {
#        print OUT_FILE "$_\n" ;
#    } elsif ( $_ =~ /^|/ )
#    {
#        print OUT_FILE "$_\n" ;
#    }
#}
#
#close(OUT_FILE) ;
 
Top Bottom