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.
First, what do you mean by "spit out" the items? If you mean, how do you use the items for printing, it's not hard. Once you have the array of filenames you do roughly this:
Code:
for my $file (@filenames) {
open my $out_fh, '>', $file
or die "Bad open on $file: $!";
print $out_fh "Whatever...";
close $out_fh
or die "Bad close on $file: $!";
}
Second, that looks to be the same code you posted the first time. It's still not formatted in code blocks, and you don't seem to have changed it at all. You need to meet folks at least half way.
When I cut and paste, it removed all the tabs " ", but I did cut and paste what is mentioned in page one.
By spit out, I mean when the loop has gone over its stuff, it creates a file called filename001.txt, then, after $n = 1 becomes $n = 2, it runs again and creates filename002.txt and so on until $n <10.
When I cut and paste, it removed all the tabs " ", but I did cut and paste what is mentioned in page one.
You should be able to copy text from a text editor here without trouble. Beyond that, please - at the very least - wrap code in [code][/code] tags.
Quote:
Originally Posted by SuperDude
By spit out, I mean when the loop has gone over its stuff, it creates a file called filename001.txt, then, after $n = 1 becomes $n = 2, it runs again and creates filename002.txt and so on until $n <10.
That sounds like you're still trying the original method from page one. I suggested one alternative, Bartonski suggested another, and Bigearsbilly suggested a third.
All that I can see that this does is prints the names until 00 to 10, since $x is assigned as a variable. How can this be used (my guess) in the "open( OUT, ">filename00" . $n . ".txt" );" line?
Edit:
My script does create all the files, but all the files but file one are empty. Would your idea fix that?
Last edited by SuperDude123; 10-18-2009 at 06:04 PM.
All that I can see that this does is prints the names until 00 to 10, since $x is assigned as a variable. How can this be used (my guess) in the "open( OUT, ">filename00" . $n . ".txt" );" line?
This is what I was explaining in my last reply: you need to use the line you quoted to create an array of filenames. (You wouldn't use exactly that line; it would need to be edited somewhat to fit the new use.) Then you iterate over the array and print out to each of the different files in turn.
If you're not sure how to do those things, then I highly recommend you take a look at Beginning Perl which is available freely online.
my $search1 = ' ';
my $search2 = ' ';
my $found1 = ' ';
my $sep1 = ' ';
my $temp1 = ' ';
my $newvalue1 = ' ';
my $found2 = ' ';
my $sep2 = ' ';
my $temp2 = ' ';
my $newvalue2 = ' ';
my $n = ' ';
my $increments = ' ';
...
Generally a horrible idea - I mean the initial values, not the declaration itself.
If a variable is declared and not initialized, then if by mistake it is used uninitialized, Perl warns about this.
In the above case all the variables look initialized to Perl, though in reality (functionally, algorithmically) they are not.
The variables should be initialized only in cases it makes functional/algorithmic sense, for example:
Code:
my $sum = 0; # $sum will be used in RHS to be added with incoming numbers
open(my $fh, '<', $file_with_numbers_per_line) or die "cannot open '$file_with_numbers_per_line' for reading, \$!=$!";
while(defined(my $number = <$fh>))
{
chomp($number);
$sum += $number;
}
warn "\$sum=$sum";
close($fh);
.
I said the idea was horrible because I had to debug somebody else's code having such "initializations", and it was horrible.
With uninitialized variable Perl tells at which line the uninitialized variable used and since perl-5.10.0 which uninitialized variable it is.
Last edited by Sergei Steshenko; 10-19-2009 at 01:15 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.