Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how much info can a variable store?

Status
Not open for further replies.

gammaman1

Programmer
Jun 30, 2005
23
US
I'm trying to store a large amount of data into a variable, and then ouputting the results to a text file.

open results, ">results";
$test = `execute program that will output`;
print results "$test";
close results;

so now I will have a huge text file named results. But when I run this, the system hangs. Is there a better way of doing this?

thanks,
gammaman1
 
concerning the first question: as far as I know perl doesn't care about the amount of data, so it will load until the memory is full.

concerning the second question: does the program outputs the data or is it directed to a file?
 
gammaman,

There are a couple of better ways of doing what I think you're trying to do.

If you're just storing the output of some program in a file, you can do it like this:
Code:
system('some_program > a_file');
You might well know that already though.

If you need to change the output of some program and then store it in a file, you can do it like this:
Code:
open(A_FILE,'>a_file');
open(SOME_PROGRAM,'some_program|');
while(<SOME_PROGRAM>){
[tab]# each line of output is in $_
[tab]# so you can change it as you go
[tab]# or just write it to the output file like this
[tab]print A_FILE $_;
}
close(SOME_PROGRAM);
close(A_FILE);
Does that help you?

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Thanks for the replies; They've given me more ideas. The problem is that the PERL program I'm writing has to run on windows XP. I have a windows version of grep installed. So what I'm trying to do is this:

1. run the program
2. pipe the results into a grep
3. then pipe that result into another grep
4. now output the results

Unfortunately, windows doesn't seem to have piping, so I was thinking of outputing the results into seperate files to run grep on. Any ideas or approaches to solving this would be much appriciated :)

thanks,
gammaman1
 
why do you say piping doesn't work in windows? Windows also actually has a grep-like program with crude regex support called "findstr.exe" I would probably use Mike's method though.

Code:
c:\>dir c:\Perl\bin | findstr -i perl
 
wow, windows has grep? I never heard of findstr before.
Anyways, I tried Mike's solution, and it seems to work fine if I use a program with little output. But when I do it for the program I'm using, cleartool, it outputs this error message:

grep: writing output: Invalid argument

I tried looking for this on Google but didn't find anything. Any ideas?

thanks,
gammaman1
 
Gammaman,

Use the pattern matching (regular expressions) built into Perl.

You can say things like:

if(/current line matches this text/){
print "matches\n";
} else {
print "doesn't match\n";
}

You should probably replace "current line matches this text" with whatever you need to though :)


Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top