The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl -write values in a file to @array in perl meghana Shell Programming and Scripting 12 07-17-2008 01:38 PM
Help me to write the script gyana_cboy Shell Programming and Scripting 5 10-04-2007 11:19 PM
how to write perl substitute command in shell scripts param_it UNIX for Dummies Questions & Answers 3 07-03-2007 01:09 AM
need help to write perl code getdpg Shell Programming and Scripting 0 09-20-2006 06:24 AM
Help! Need to write my first script fundidor Shell Programming and Scripting 5 01-08-2004 06:20 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Sep 2007
Posts: 160
Stumble this Post!
Need help to write a Perl script

Hello friends,

I am having a awk script which does my goal , but I want to learn perl , after learning the basics in perl now I am trying to convert my nawk script to perl .

Please help me to do some task in perl that I ve already did in nawk.

Like I am facing some problem in perl with $_, I think it is similar to $0 in nawk.

part of my perl code as follows
Code:
open(IN, $file)|| die("Could not open file");
while(<IN>) {
 if($_ !~ /^"/){
    if($_ =~ /^\*/){
    }
    else{
       @fld = split(/,/, $_);
       if( $fld[3] == 1 ){
              print $_;
              $binary1 = HexToBinary(substr($fld[0],0,2));
              print $_;
        }
     }
  }
}
sub HexToBinary{
        my(%h)=( '0'=>'0000'
                ,'1'=>'0001'
                ,'2'=>'0010'
                ,'3'=>'0011'
                ,'4'=>'0100'
                ,'5'=>'0101'
                ,'6'=>'0110'
                ,'7'=>'0111'
                ,'8'=>'1000'
                ,'9'=>'1001'
                ,'A'=>'1010'
                ,'B'=>'1011'
                ,'C'=>'1100'
                ,'D'=>'1101'
                ,'E'=>'1110'
                ,'F'=>'1111'
        );
        $_=uc $_[0];
        s/([0-9A-F])/$h{$1}/g;
        return $_;
};
Although both the print statement seems to be same but gives me a different result. If i am not wrong incase of nawk we can print the current line anywhere with just typing
Code:
print $0

Regards,
user_prady

Last edited by user_prady; 03-21-2008 at 12:14 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Sep 2007
Posts: 160
Stumble this Post!
Quote:
Originally Posted by user_prady View Post
Hello friends,

I am having a awk script which does my goal , but I want to learn perl , after learning the basics in perl now I am trying to convert my nawk script to perl .

Please help me to do some task in perl that I ve already did in nawk.

Like I am facing some problem in perl with $_, I think it is similar to $0 in nawk.

part of my perl code as follows
Code:
open(IN, $file)|| die("Could not open file");
while(<IN>) {
 if($_ !~ /^"/){
    if($_ =~ /^\*/){
    }
    else{
       @fld = split(/,/, $_);
       if( $fld[3] == 1 ){
              print $_;
              $binary1 = HexToBinary(substr($fld[0],0,2));
              print $_;
        }
     }
  }
}
sub HexToBinary{
        my(%h)=( '0'=>'0000'
                ,'1'=>'0001'
                ,'2'=>'0010'
                ,'3'=>'0011'
                ,'4'=>'0100'
                ,'5'=>'0101'
                ,'6'=>'0110'
                ,'7'=>'0111'
                ,'8'=>'1000'
                ,'9'=>'1001'
                ,'A'=>'1010'
                ,'B'=>'1011'
                ,'C'=>'1100'
                ,'D'=>'1101'
                ,'E'=>'1110'
                ,'F'=>'1111'
        );
        $_=uc $_[0];
        s/([0-9A-F])/$h{$1}/g;
        return $_;
};
Although both the print statement seems to be same but gives me a different result. If i am not wrong incase of nawk we can print the current line anywhere with just typing
Code:
print $0

Regards,
user_prady
Got the problem ...
Code:
 $_=uc $_[0];
        s/([0-9A-F])/$h{$1}/g;
        return $_;
Changed to
Code:
  $val = uc $_[0];
  $val =~ s/([0-9A-F])/$h{$1}/gi;
  return $val;
Regards,
user_prady
Reply With Quote
  #3 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Mar 2008
Posts: 2
Stumble this Post!
$_ is used when no lvalue defined. $_ =~ /expr/ is equil to /expr/

Let's Perl!
Reply With Quote
  #4 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Jan 2008
Posts: 306
Stumble this Post!
Quote:
Originally Posted by user_prady View Post
Got the problem ...
Code:
 $_=uc $_[0];
        s/([0-9A-F])/$h{$1}/g;
        return $_;
Changed to
Code:
  $val = uc $_[0];
  $val =~ s/([0-9A-F])/$h{$1}/gi;
  return $val;
Regards,
user_prady
An example of why you should use "strict" and declare your variables properly.
Reply With Quote
  #5 (permalink)  
Old 03-23-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,607
Stumble this Post!
The logic is a bit hard to follow. Could you post a sample input and sample output for that?

I would start by refactoring your script based on the following observations;

* the "if" has only an "else" clause. It would seem more natural to invert the test and have only a "then" clause.

* Perl can easily convert hex to binary, and generally between different number bases, so that part of the script is easy to simplify. But you are not using the value of $binary anywhere?

Code:
open(IN, $file)|| die("Could not open file: $!");
while(<IN>) {
 unless (/^["*]/) {
   @fld = split(/,/, $_);
   if( $fld[3] == 1 ){
          print $_;
          $binary1 = HexToBinary(substr($fld[0],0,2));
          print $_;
	  # do you mean
	  # $first = $binary1 . substr($fld[0],2);
          # shift @fld
          # print join ("," $first, @fld)
          # ?
    }
  }
}
sub HexToBinary{
    my $hex = $h;
    return sprintf "%b", hex($h);
}
Reply With Quote
  #6 (permalink)  
Old 03-23-2008
Registered User
 

Join Date: Sep 2007
Posts: 160
Stumble this Post!
Smile

Quote:
Originally Posted by era View Post
The logic is a bit hard to follow. Could you post a sample input and sample output for that?

I would start by refactoring your script based on the following observations;

* the "if" has only an "else" clause. It would seem more natural to invert the test and have only a "then" clause.

* Perl can easily convert hex to binary, and generally between different number bases, so that part of the script is easy to simplify. But you are not using the value of $binary anywhere?

Code:
open(IN, $file)|| die("Could not open file: $!");
while(<IN>) {
 unless (/^["*]/) {
   @fld = split(/,/, $_);
   if( $fld[3] == 1 ){
          print $_;
          $binary1 = HexToBinary(substr($fld[0],0,2));
          print $_;
	  # do you mean
	  # $first = $binary1 . substr($fld[0],2);
          # shift @fld
          # print join ("," $first, @fld)
          # ?
    }
  }
}
sub HexToBinary{
    my $hex = $h;
    return sprintf "%b", hex($h);
}

Thank you all for your kind reply.

Era sorry for not sending my whole goal and code.

Here it goes...Its a FPGA test pattern .But I think I cant send all my constarints at once.

Input
Code:
"RxData","Time","NSysClkEn"
000000,0000,1,0,0,0,0,0,0
000000,0000,0,0,0,0,0,0,0
*Comment Control Frame at 10 us
000000,030C,0,0,0,0,1,0,1
241000,0000,0,1,3,0,0,0,0
000100,0000,0,0,2,0,0,0,0
*Comment Control Frame at 65 us
000000,13CE,0,0,0,0,1,0,1
000200,0000,0,1,2,0,0,0,0

*Main Start
*Comment Frame 2 at 1.04167 us
000000,2451,0,0,0,0,1,0,1
A8F9FF,0000,0,1,3,0,0,0,0
9F999F,0000,0,0,3,0,0,0,0
FFF9FF,0000,0,0,3,0,0,0,0
F9FF9F,0000,0,0,3,0,0,0,0
999DF3,0000,0,0,3,0,0,0,0
73BDF5,0000,0,0,3,0,0,0,0
D5FDF5,0000,0,0,3,0,0,0,0
5FD9B5,0000,0,0,3,0,0,0,0
1BB3BB,0000,0,0,3,0,0,0,0
7DF97D,0000,0,0,3,0,0,0,0
3BDDDB,0000,0,0,3,0,0,0,0
*Main End
Constraints:
Code:
1.dont process lines starting with lines " (double quotes)and  * (astris)
2.Prints the lines starting with * as it is. 
3.if the fourth field is "1" then take the fist two digits of the first field and then convert it to binary.
   checks the binary no with a look up table.
4.prints the second field in decimal when field 9th and 7th are "1".
For the time being I am explananing that much when I ll archive that goal in Perl I ll explain further friends.

Regards,
user_prady
Reply With Quote
  #7 (permalink)  
Old 03-23-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,607
Stumble this Post!
Quote:
Originally Posted by user_prady View Post
1.dont process lines starting with lines " (double quotes)and * (astris)
2.Prints the lines starting with * as it is.
Just to help you get a feel for some Perl idioms, here's how I'd tackle those two.

Code:
while (<>) {
  next if m/^"/;
  if (m/^\*/) {  # that's "as-te-risk" btw (^:
    print;
    next;
  }

  # ... more processing here
}
A few "next if" up near the top of the loop let you do away with simple special cases, and then concentrate on the real meat in the main loop, without putting you inside too many conditional braces -- they tend to make the code harder to grasp, even if the conditions are simple.

Making the main loop work over standard input is often a good idea; even if you always process the same file name in the production system, modularity is a good help whenever you need to test, modify, debug, or otherwise maintain the code -- you can run a few simple test files through it when you like, without modifying the code. (And if you are at all ambitious, you should store it with a few such test files and a test script which verifies that they are processed by the spec. This is a lifesaver when you need to make a strategic modification a few years from now and wonder whether you're breaking anything. See the Test::Simple documentation for more on this.)
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0