Need help to write a Perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to write a Perl script
# 1  
Old 03-21-2008
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 05:14 AM..
# 2  
Old 03-21-2008
Quote:
Originally Posted by user_prady
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
# 3  
Old 03-21-2008
$_ is used when no lvalue defined. $_ =~ /expr/ is equil to /expr/

Let's Perl!
# 4  
Old 03-21-2008
Quote:
Originally Posted by 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
An example of why you should use "strict" and declare your variables properly.
# 5  
Old 03-23-2008
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);
}

# 6  
Old 03-24-2008
Bug

Quote:
Originally Posted by era
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
# 7  
Old 03-24-2008
Quote:
Originally Posted by user_prady
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.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to write Perl Script to Get MQ Queue Depth?

Hi , I got the following script from internet to display queue depth using Perl Script. However, when I execute it , im getting following error. Can anyone shed light on what is going wrong? #!/usr/bin/perl ## 07/23/01 ## Depth Inquiry sample program. ## Arguments: ## Connects to... (11 Replies)
Discussion started by: srkmish
11 Replies

2. Shell Programming and Scripting

Perl script to create/write into spreadsheet

Hi, I need help in debug following script. can somebody help....!!! #!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; # Create a new workbook called simple.xls and add a worksheet. my $workbook = Spreadsheet::WriteExcel->new('simple.xls'); my $worksheet =... (1 Reply)
Discussion started by: chettyravi
1 Replies

3. Shell Programming and Scripting

Need a UNIX/perl script to read and write the data

Hi, I have on Designdocument in that information is stored with in tabular format.I need Perl/unix script to read and write the data using perl script? Regards, Ravi (4 Replies)
Discussion started by: toravi.pentaho
4 Replies

4. Shell Programming and Scripting

Need a perl script to read and write the data

Hi, I have on Designdocument in that information is stored with in tabular format.I need Perlscript to read and write the datausing perl script? Regards, Ravi (0 Replies)
Discussion started by: toravi.pentaho
0 Replies

5. Shell Programming and Scripting

How to write a update query in perl script?

can any one suggest me on how to write a update query in perl script for Oracle database and also tell me abt how we can write a code for sending mails with report as attachment to appropriate persons? (1 Reply)
Discussion started by: Ramesh V Kumar
1 Replies

6. Shell Programming and Scripting

perl script to check read/write/execute permission for 'others'

I want to check access rights permissions not for 'user', not for 'group', but for 'others'. I want to do it by system command in which i want to use 'ls -l' and 'awk' command. I have written the following program : #!/usr/bin/local/perl #include <stdlib.h> system ("ls -l | awk... (1 Reply)
Discussion started by: shubhamsachdeva
1 Replies

7. Shell Programming and Scripting

perl script to find, write, repeat...

I am a novice writing perl scripts so I'd appreciate any help you guys can offer. I have a list of 100 words in a file (words.txt) and I need to find them in a second file (data.txt). Whenever one of these words is found I need to write that line to a third file (out.txt) and then continue... (1 Reply)
Discussion started by: tgamble
1 Replies

8. Shell Programming and Scripting

Help need to write a script on column separation for syslog output in perl

HI Pros, I have a issue.I need to write a script to parse the logs got from syslog server and update the same in my database.I need the following output.I donot know perl and I heard it very easy to write in perl I have the sample log I need each column seperated by commas and all equals... (0 Replies)
Discussion started by: iron_michael86
0 Replies

9. Shell Programming and Scripting

write a perl script or kornshell reading a two files and outputting to comma format

Hello Can someone help me to write a perl script or kornshell reading a two files and outputting to comma format. Here is the two files listofdisks.txt id, diskname, diskgroup, diskisze(GB), FC 1, CN34, GRP1, 30, FC_CN34 2, CN67, GRP5, 19, 4, VD1, GRP4, 23, FC_VD1 6, CF_D1, ... (0 Replies)
Discussion started by: deiow
0 Replies

10. UNIX for Dummies Questions & Answers

Should I write a PERL Script or Shell Script?

Hello, I have done some BASIC shell scripting/PERL scripting before so I am familiar with the languages. I am not really sure which one would lend itself better to the application I have to write. I am required to scan the message logs for possible break in attempts. If I use shell scripting... (2 Replies)
Discussion started by: mojoman
2 Replies
Login or Register to Ask a Question