Gerald Wheeler wrote:
I could use some real expert help here..
I have only the basic - no add-on modules except what comes with Perl
What is needed, Is this possible with Perl 5.x..
read from file olddata.txt output to newdata.txt
1) output all numerics inside parenthesis to negative values e.g., (32)
should output as -32
2) output all numerics inside double quotes to the number inside the
quotes without the comma e.g., "2,345" should be output as 2345
3) ignore (do not output) lines with alphabet characters A-Za-z
4) ignore (do not output) lines with only commas
5) ignore (do not output) the first and last column of the line
6) print out data as one data value per line preceeded by an index
starting at 1, from top of column one to bottom of column one, then
column two (top to bottom), etc.
Thanks for any help....
here is the file:
[ SNIP ]
#!/usr/bin/perl
use warnings;
use strict;
my $file_in = 'olddata.txt';
my $file_out = 'newdata.txt';
open my $IN, '<', $file_in or die "Cannot open '$file_in' $!"
open my $OUT, '>', $file_out or die "Cannot open '$file_out' $!"
my @data;
while ( <$IN> ) {
# 3) ignore (do not output) lines with alphabet characters A-Za-z
next if /[A-Za-z]/;
# 4) ignore (do not output) lines with only commas
next if /^,+$/;
# 1) output all numerics inside parenthesis to negative values e.g., (32)
# should output as -32
s/ \( ( \d+ ) \) /-$1/xg;
# 2) output all numerics inside double quotes to the number inside the
# quotes without the comma e.g., "2,345" should be output as 2345
s/ " ( [^"]+ ) " / ( my $x = $1 ) =~ tr!,!!d; $x /exg;
# 5) ignore (do not output) the first and last column of the line
s/^[^,]*,//;
s/,[^,]*$//;
my $index;
push @{ $data[ $index++ ] }, $_ for split /,/;
# If you want *all* empty elements
# push @{ $data[ $index++ ] }, $_ for split /,/, $_, -1;
# If you want *NO* empty elements
# /\d/ && push @{ $data[ $index++ ] }, $_ for split /,/;
}
# 6) print out data as one data value per line preceeded by an index
# starting at 1, from top of column one to bottom of column one, then
# column two (top to bottom), etc.
my $index = 1;
for my $row ( @data ) {
print $OUT $index++, ",$_\n" for @$row;
}
__END__
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/