ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/usr/bin/env perl
open my $file, $ARGV[1] or die "Could not open file";
while (<$file>) # iterate over the lines
{
s/D/e/g;
for (split) # iterate over the columns in the current line
{
$_ *= 219474.6306726;
print "$_\n";
}
}
close $file;
I have no idea if this will work, I'm still a bit new to Perl, but I couldn't resist .
I am not sure how this affect your double precision.
btw. I realise you were giving us a dumbed down example, but as you can see for future questions, dealing with floating point precision can make a large impact
if not known about earlier
Edit: Also, maybe you could show us some actual input? (changed to protect the data of course if necessary)
Hi grail! Your code is neat as always, but why not using just a counter to print the line number? Just another suggestion (a lot of work for the original poster ) but I would end-up with something like this:
Code:
BEGIN { factor = 219474.6306726 }
{
gsub(/D/,"E")
for ( i=1; i<=NF; i++ )
array[++count] = $i
}
END {
for ( i=1; i<=count/NR; i++ ) {
for ( j=0; j<NR; j++ ) {
print ++c, array[i+j*NF] * factor
}
}
}
Nice attempt! I'm not a perl expert, too... but I'm afraid it parses the file row by row, whereas we need a trick to parse by columns.
No, the while loop goes through the file line by line, all instances of 'D' are replaced with 'e', and the for loop goes through the current line column by column.
but why not using just a counter to print the line number?
Hey Colucix
Based on the OPs original request they wanted the line numbers to equal the line the column came from as opposed to where your code has an ever increasing
count, ie if there are 3 rows and 3 columns it would be
Based on the OPs original request they wanted the line numbers to equal the line the column came from as opposed to where your code has an ever increasing count
Uh, sorry... I missed that part. A little modification to my previous code:
Code:
BEGIN { factor = 219474.6306726 }
{
gsub(/D/,"E")
for ( i=1; i<=NF; i++ )
array[++count] = $i
}
END {
for ( i=1; i<=count/NR; i++ ) {
for ( j=0; j<NR; j++ ) {
print j+1, array[i+j*NF] * factor
}
}
}
Quote:
Originally Posted by grail
So after a little plagiarism from colucix (hope you don't mind ):
No, the while loop goes through the file line by line, all instances of 'D' are replaced with 'e', and the for loop goes through the current line column by column.
It makes heavy use of the implicit $_ variable.
Yup. But it should not print the splitted fields immediately, otherwise you get alternate output as you read it from left to right, e.g.
Code:
$ cat test.pl
#!/usr/bin/env perl
open $file, $ARGV[0] or die "Could not open file: $!";
while (<$file>) {
s/D/e/g;
for (split)
{
$_ *= 219474.6306726;
print "$_\n";
}
}
close $file or die "$file: $!";
$ cat file
1.0000D-05 2.0000D-05 3.0000D-05
1.0000D-05 2.0000D-05 3.0000D-05
1.0000D-05 2.0000D-05 3.0000D-05
$ ./test.pl file
2.194746306726
4.389492613452
6.584238920178
2.194746306726
4.389492613452
6.584238920178
2.194746306726
4.389492613452
6.584238920178
$
Just a little note (then I will stop... promised ): why do you use a quoted space in the print statement? Comma separated arguments will be printed out as OFS separated strings. I find the comma more elegant and quick to type.
#!/usr/bin/env perl
open my $file, $ARGV[1] or die "Could not open file";
for my $col (0 .. 2)
{
seek $file, 0, SEEK_SET;
while (<$file>)
{
my @cols = split;
$_ = $cols[$col];
s/D/e/g;
$_ *= 219474.6306726;
print "$_\n";
}
}
close $file;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.