LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2009, 11:50 AM   #16
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
just for giggles:


I wanted to see what would happen if I incremented beyond 1000...

Code:
$ perl -e  '$x='file000'; print $x++, "\n" foreach (1..1002)'
file000
file001
file002
file003
  ...
file998
file999
filf000
filf001
Very interesting. Slightly disturbing, but interesting.
 
Old 10-18-2009, 12:59 PM   #17
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by bartonski View Post
I wanted to see what would happen if I incremented beyond 1000...

Code:
$ perl -e  '$x='file000'; print $x++, "\n" foreach (1..1002)'
file000
file001
file002
file003
  ...
file998
file999
filf000
filf001
Very interesting. Slightly disturbing, but interesting.
Part of what I meant by DWIM -> WTF?

See this more generally:
Code:
telemachus ~ $ perl -e '$x='aa'; print $x++, "\n" for (1..100);'
 
Old 10-18-2009, 05:29 PM   #18
SuperDude123
Member
 
Registered: Nov 2008
Posts: 158

Original Poster
Rep: Reputation: 30
How do I spit out the files as filename001.txt, filename002.txt, ..., instead of just printing the names?

Also, only my first file has data in it, but anything after that is just blank. Any ideas why?

Here's my script:

#!/usr/bin/perl

use strict;
use warnings;

# Input and Output files
open (IN, "data.txt");

# stating variables

my $search1 = ' ';
my $search2 = ' ';
my $found1 = ' ';
my $sep1 = ' ';
my $temp1 = ' ';
my $newvalue1 = ' ';
my $found2 = ' ';
my $sep2 = ' ';
my $temp2 = ' ';
my $newvalue2 = ' ';
my $n = ' ';
my $increments = ' ';
$n = 1;

# What is being searched for
$search1 = " Cookies Sold=";
$search2 = "Pies Sold=";

# Starting Values
$newvalue1 = 0; # Starting value for search1
$newvalue2 = 0; # Starting value for search2

# Increment Value
$increments = 1;

while ( $n < 10 ) {
open( OUT, ">filename00" . $n . ".txt" ); #

while (<IN>) {

$found1 = index( $_, $search1 );
if ( $found1 >= 0 ) {
$sep1 = index( $_, "=" );
$temp1 = substr( $_, 0, $sep1 + 1 );
$newvalue1 = $newvalue1 + $increments;
print OUT "$temp1$newvalue1 \n";
}

else {

$found2 = index( $_, $search2 );
if ( $found2 >= 0 ) {
$sep2 = index( $_, "=" );
$temp2 = substr( $_, 0, $sep2 + 1 );
$newvalue2 = $newvalue2 + $increments;
print OUT "$temp2$newvalue2 \n";
}

else {
print OUT $_;
}
}

}
$n = $n + 1;
}

close OUT;
close IN;

Last edited by SuperDude123; 10-18-2009 at 05:43 PM.
 
Old 10-18-2009, 05:45 PM   #19
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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.
 
Old 10-18-2009, 05:49 PM   #20
SuperDude123
Member
 
Registered: Nov 2008
Posts: 158

Original Poster
Rep: Reputation: 30
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.
 
Old 10-18-2009, 05:53 PM   #21
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by SuperDude123 View Post
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.

Take your pick and try one of those.
 
Old 10-18-2009, 06:03 PM   #22
SuperDude123
Member
 
Registered: Nov 2008
Posts: 158

Original Poster
Rep: Reputation: 30
But where and how do you integrate:

perl -e '$x='file000'; print $x++.".txt", "\n" foreach (1..11)

In the script?

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.
 
Old 10-18-2009, 06:32 PM   #23
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by SuperDude123
But where and how do you integrate:

perl -e '$x='file000'; print $x++.".txt", "\n" foreach (1..11)

In the script?

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.
 
Old 10-18-2009, 07:06 PM   #24
SuperDude123
Member
 
Registered: Nov 2008
Posts: 158

Original Poster
Rep: Reputation: 30
So what would cause the script to write empty files (002 up) ?
 
Old 10-18-2009, 07:33 PM   #25
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by SuperDude123 View Post
So what would cause the script to write empty files (002 up) ?
Start here maybe: http://www.linuxquestions.org/questi...5/#post3722527
 
Old 10-19-2009, 01:04 AM   #26
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by SuperDude123 View Post
...
# stating variables

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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
while loop creates a problem for a file opened in append mode for writing in PERL gaynut Programming 2 07-28-2008 07:26 PM
Perl eternal loop problem x_terminat_or_3 Programming 4 08-01-2007 02:12 AM
perl loop jazman Programming 3 07-05-2006 10:31 AM
Need help with perl loop! morbid_ru Programming 1 02-24-2004 01:14 PM
Need help with perl loop! morbid_ru Programming 2 02-17-2004 05:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration