Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 255and 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?

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.

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?

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.

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.

![]() |
Batch file for processing...
|
Batch file: rename file b...
|

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