Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 25-12-2010, 09:31 AM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Feb 2008
Posts: 26
Default Perl


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.
Adam Cruge is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 26-12-2010, 09:55 AM   #2 (permalink)
Right Off the Assembly Line
 
Join Date: Feb 2008
Posts: 26
Default Re: Perl

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.
Adam Cruge is offline   Reply With Quote
Old 26-12-2010, 01:14 PM   #3 (permalink)
Wise Old Owl
 
pulkitpopli2004's Avatar
 
Join Date: Jul 2010
Location: D!ll!
Posts: 1,131
Default Re: Perl

mail me ur ibis file i wud send u a soln.. id is pulkitpopli2004@yahoo.co.in

n by d way from where u got to knw abt ibis??
pulkitpopli2004 is offline   Reply With Quote
Old 27-12-2010, 10:10 AM   #4 (permalink)
Right Off the Assembly Line
 
Join Date: Feb 2008
Posts: 26
Default Re: Perl

|
[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 Code:
#! /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 ==){
        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 
$wsplit / /, $line[$i] ){
        
push @word$w;
    }
    if( 
$word[0eq "[GND" ){
        
$GNDpointer$i+1;
    }
    if( 
$word[0eq "[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[0eq "|" ){
        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 Liverpool_fan; 27-12-2010 at 02:34 PM. Reason: Added code tags
Adam Cruge is offline   Reply With Quote
Old 21-01-2011, 12:37 AM   #5 (permalink)
Broken In
 
ninad_mhatre85's Avatar
 
Join Date: Feb 2006
Posts: 116
Default Re: Perl

Read the comments below

Quote:
Originally Posted by Adam Cruge View Post
|
[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 Code:
#! /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 ==){
        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 
$wsplit / /, $line[$i] ){
        
push @word$w;
    }
    if( 
$word[0eq "[GND" ){
        
$GNDpointer$i+1;
    }
    if( 
$word[0eq "[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[0eq "|" ){
        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 Code:
#! /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 == )
{
   
$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 

}

# 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 ;
# Use foreach loop
foreach ( @line )
{
    
$GNDpointer   $i+if ( $_ =~ /^\[GND/ ) ; 
    
$POWERpointer $i+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) ; 
__________________
Life is not measured by moments u breath but by the moments that take ur breath away
ninad_mhatre85 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:24 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2