Computing.Net > Forums > Programming > Basic PERL programming script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Basic PERL programming script

Reply to Message Icon

Name: Sadspade
Date: August 5, 2008 at 10:50:10 Pacific
OS: Windows Vista
CPU/Ram: Intel Cetrino Duo
Product: HP
Comment:

Hi all,
i have a text file which looks like this:

255 255 255 254 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 000 127 255 255 255 255 255

and i would like to extract and modify each element and output it into a new file which looks like this:

z403,1000,0; # "z403,1000 = column 4, row 1
z403,1000,0;
z404,1002,50; # "z404,1002 = column 5, row 3
z404,1002,0;
z405,1002,25; # "z405,1002 = column 6, row 3
z405,1002,0;

Here's a PERL script written by myself which converts the text file into the output:

#!/usr/bin/perl

if($#ARGV >= 0){

while($#ARGV >= 0){
$filename = $ARGV[0];
shift @ARGV;
open(FILE,$filename) or die "no file";
while($content = <FILE>){
chomp($content);
@values = split (/\s/,$content);

$y=0;
$x=0;
foreach $val(@values){

if ($val == 255){

$first = 400 + $x;
$second = 1000 + $y;
$third = 0;
$x++;
}
elsif ($val != 255){

$Dfirst = 400 + $x;
$Dsecond = 1000 + $y;
$Dthird = (255 - $val) * 0.196;
printf "z%.0f,%.0f,%.0f;\n",$Dfirst,$Dsecond,$Dthird;
printf "z%.0f,%.0f,0;\n",$Dfirst,$Dsecond;
$x++;
}
}
}

However, the output of the PERL program turns out to be like this:

z403,1000,0;
z403,1000,0;
z426,1000,50;
z426,1000,0;
z427,1000,25;
z427,1000,0;

Is there something wrong with my PERL script which does not give me the desired output? Any suggestions?




Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: August 5, 2008 at 18:07:35 Pacific
Reply:

hint: your $y is always 0.


0

Response Number 2
Name: FishMonger
Date: August 5, 2008 at 22:44:36 Pacific
Reply:

1) You're missing the following 2 pragmas which should be in every Perl script you write.

use warnings;
use strict;

2) You're not localizing your vars by declaring them with the my keyword.

3) There's no need to check the size of @ARGV twice.

4) It's better to use the 3 arg form of open and to use a lexical var for the filehandle.

5) The die statement should include the filename and error message from the system when the open call fails.

6) You have 3 unused variables.
Name "main::first" used only once: possible type.
Name "main::second" used only once: possible type.
Name "main::third" used only once: possible type.

7) As ghostdog pointed out, you're not incrementing $y.


0

Response Number 3
Name: Poco87
Date: August 7, 2008 at 09:52:51 Pacific
Reply:

I would use print instead of printf for better readability since you aren't using decimals, just roud them off instead...and I'm assuming you do not want to print '# "z403,1000 = column 4, row 1', etc?


0

Response Number 4
Name: Poco87
Date: August 7, 2008 at 10:26:31 Pacific
Reply:

Forget about printf I'm an idiot.

Got it to work, but I just did it for one file:

#!/usr/bin/perl


$filename = "c:/scripts/data.txt";

open(FILE,$filename) or die "no file";

$test = 0;
$y=0;

while (<FILE>) {
$content = $_;
chomp($content);
@values = split (/\s/,$content);


$x=0;

foreach $val(@values){

if ($val == 255) {

$first = 400 + $x;
$second = 1000 + $y;
$third = 0;
$x++;
} elsif ($val != 255) {

$Dfirst = 400 + $x;
$Dsecond = 1000 + $y;
$Dthird = (255 - $val) * 0.196;
printf "z%.0f,%.0f,%.0f;\n",$Dfirst,$Dsecond,$Dthird;
printf "z%.0f,%.0f,0;\n",$Dfirst,$Dsecond;

$x++;


}
}

$y++;

}


I'm assuming y was used for the line number starting at zero and x was used for the column number, in which case you were reseting y for each line in addition to not incrementing it.

Sorry but I changed a few things to my way of doing things, I'm sure they can be changed back with no problem.


0

Response Number 5
Name: Poco87
Date: August 7, 2008 at 10:32:53 Pacific
Reply:

Actually this is the same but without the unnecessary stuff:

#!/usr/bin/perl


$filename = "c:/scripts/data.txt";

open(FILE,$filename) or die "no file";

$y=0;

while ($content = <FILE>) {

chomp($content);
@values = split (/\s/,$content);
$x=0;

foreach $val(@values) {

if ($val != 255) {

$Dfirst = 400 + $x;
$Dsecond = 1000 + $y;
$Dthird = (255 - $val) * 0.196;
printf "z%.0f,%.0f,%.0f;\n",$Dfirst,$Dsecond,$Dthird;
printf "z%.0f,%.0f,0;\n",$Dfirst,$Dsecond;

}

$x++;

}

$y++;

}


Sorry for posting three times I get excited when I find something to do at work.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

Batch file for processing... Batch file: rename file b...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Basic PERL programming script

Simple perl program www.computing.net/answers/programming/simple-perl-program/5985.html

Need Perl Programming Help!!!!! www.computing.net/answers/programming/need-perl-programming-help/762.html

perl program www.computing.net/answers/programming/perl-program/3890.html