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.
I would like to ask for help. I have a perl script to do a task (e.g. printf). I would like to read a text.txt which contains ID and x value. And I would like to save the output of printf into a new file. Any help?
So far, I have program until this stage, but it fails.
Code:
open (DATA, "<text.txt");
my $th = 0.000;
for ($th=0; $th<=5; $th+=0.001)
{
sysopen (FILE, "0_"+$th+"_text.txt", O_RDWR | O_CREAT);
while(<DATA>){
my($ID, $x) = split;
if ($x >= $th)
{
printf FILE "%D\n", $ID;
}
}
close(FILE);
}
close(DATA);
It gives me two type errors:
Code:
1. Filehandle FILE opened only for input at the "printf FILE "%D\n", $ID;"
2. Argument "O_SVWST" isn't numeric in sysopen at " sysopen (FILE, "0_"+$th+"_text.txt", O_RDWR | O_CREAT);"
Please help me. Thanks alot!
Last edited by eminempark; 09-12-2013 at 01:12 AM.
Have a look at this simple example (open infile, write line to outfile):
Code:
#!/usr/bin/perl
use strict ;
use warnings ;
# open file for reading
open( INFILE, "<infile" )
or die "Can't open infile : $!\n" ;
# open file for writing
open( OUTFILE, ">outfile" )
or die "Can't open outfile : $!\n" ;
# do your stuff here
while ( <INFILE> ) {
printf OUTFILE "%D\n", $_ ;
}
# close both filehandles
close( OUTFILE ) ;
close( INFILE ) ;
exit 0 ;
I would like to ask about the output file name. For example, I would like to name my file output from the variable $th. How can I do that? Because from the code that you provided, it can work if we already have the output file. But if we want to automatic create and based on the variable $th? Thank you very much.
I would like to ask about the output file name. For example, I would like to name my file output from the variable $th. How can I do that?
Quote:
But if we want to automatic create and based on the variable $th?
Code:
#!/usr/bin/perl
use strict ;
use warnings ;
my $outfile = "/path/to/my.outfile" ;
open( INFILE, "<infile" ) # use hard coded file name
or die "Can't open infile : $!\n" ;
open( OUTFILE, ">$outfile" ) # use variable instead of hard coded
or die "Can't open $outfile : $!\n" ;
while ( <INFILE> ) {
printf OUTFILE "%D\n", $_ ;
}
close( OUTFILE ) ;
close( INFILE ) ;
exit 0 ;
The above overwrites the outfile (single >), if you want/need to append to the outfile then use >> instead of > open( OUTFILE, ">>$outfile" )
Quote:
Because from the code that you provided, it can work if we already have the output file.
Nope, that isn't true. output will be created if it isn't there:
you have only one input file that should be opened only once - that will happen before the while loop.
You have a lot of output files, all of them should be opened inside the loop - when you know the name
you have only one input file that should be opened only once - that will happen before the while loop.
You have a lot of output files, all of them should be opened inside the loop - when you know the name
It still produce the same file name. With alot 0 behind. any solution for this? I really need help.
we have still no info about the data you want to handle. please uncomment the line #use warnings and fix problems. Also you may need to check if split was successful.
"unable to process" is not enough to help you, please specify what have you got and what did you expect.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.