Virtual Secretary wrote:
> I messed this one up:)v I did this:
[snip]
> while (@clients){
> print FILE "$_\n";
> }
Andre, if you want to loop through your clients array, but stop at the end,
try removing each array element after you've printed it out. That way your
array will become empty, and the while condition will return false, ending the loop.
If you need the clients array after the loop for other parts of your
program, you can save the data in another array first.
In your code which is quoted above, you are telling Perl to do something
different than what you really wanted. If you spoke your code out loud in
human-speak, your code says:
"Perl, while there is something in my array of clients, print the value
of '$_' and a newline to my file."
And Perl says:
"OK Andre, if you say so. --But that probably isn't what you want.
You haven't asked me to loop through the list of your clients there, instead
you have asked me to print the value of '$_' followed by a newline as long as
there is as at least one element in the @clients array."
"Andre, Unless something happens to clear out all the elements in your array,
I'll have to keep printing "$_\n" forever! Forever is a long time, and
if I have to keep executing your command that long, I'll probably keel over
and die when I get too tired."
"Now I suspect that you might be confused when you see that I've only been
printing newline characters instead of a line seperated list of all the
elements in your @clients array. Just remember as a basic rule that I don't
automatically assign a value to '$_' all of the time. Just because you say
"while" and make a loop construct doesn't mean you get something new stuffed
into '$_' each time I make the loop.
I only do that when I have a list of things to loop over. Because you've
only asked me to perform a command for as long as a certain condition is
true, I don't have a list of things to loop over. In turn, '$_' just contains
an empty value and that's why you keep seeing all of those empty lines.
Sometimes I might still have some kind of value inside of '$_' left over
from some other previous of command or another surrounding loop. In that
case, you'll see that value printed over and over on each line in your file.
Either way, you won't see what you wanted."
Thanks Perl! Alrighteethen, now that we understand what Perl just said, let's
try something different. Here's an example:
while (@clients){
print(FILE shift(@clients));
print(FILE "\n");
}
print "DONE\n\n";
As an appendage to what we've seen here, I'd like to pass along the contents
of a post I made to usenet last night about handling files. It might help you
work with files more easily and efficiently. I'll paste in a few selected
parts of that post below my signature here.
--
-Tommy Butler
http://atrixnet.com
I'm looking for work!
contract or perm, full or part time
Download my résumé http://www.atrixnet.com/resume
the Open Source Perl Archives
at Atrixnet.
http://www.atrixnet.com/pub/
--------/////Begin quoted usenet excerpts/////----------------------------
...here's an approach I often use for file IO. It takes advantage of some
great functionality often needed for handling files. Futils.pm is a module
you can get at the Open Source Perl Archives at Atrixnet. Look for it in
the modules subsection. It isn't hard to find :O)
URL
# use a code library for file handling
use Futils;
# use the library to magically bring to life a new Futils object
my($f) = Futils->new();
# what file do I want to open?
my($file) = '/foo/bar/baz';
# in bytes, what is the limit of data I'll
# allow my Futils object to read from a file?
# We'll set it to the already-set default: 10 megs
$f->set('readlimit' => 10000000);
# load up the file and put it's contents into the
# scalar variable '$content'
my($content) = $f->load_file($file);
# do you want the lines of the file individually?
# note to gurus: I'm sorry, wantarray() just doesn't
# do what I want here, hence the switch.
my(@content) = $f->load_file($file,'--as-list');
# make a new file in some directory that doesn't
# exist, and stuff data in it.
# reading from and writing to files always
# implements LOCK_EX on platforms which support it
my($dir) = 'home/blah/blah/black/sheep/haveyou/any/wool/yessir/yessir';
my($newfile) = '3bags.full';
$f->
write_file
(
'file' => $dir.'/'.$newfile,
'content' => "that's all she wrote",
);
# or append to a file maybe?
$f->
write_file
(
'file' => $dir.'/'.$newfile,
'content' => "until she wrote some more",
'mode' => 'append',
);
# What's in our new directory?
my(@dir) = $f->list_dir($dir);
# Show only files in '$dir', exclude directories
@dir = $f->list_dir($dir,'--files-only');
# I want to load all the files inside my home dir, and
# store the name and content for each file in a name/value pair
# we'll use a hash reference to implement that functionality
# readlimit still applies, but to individual files only.
my($files) = $f->load_dir($dir);
# what did we get?
foreach (keys(%{$files})) {
print
(
'I have loaded the contents of $_ into memory.',
'this is what was in the file:',
$f->newline(),
$files->{$_}
);
}
# newline() method implements platform specific
# CRLF/LF line seperator character differentiation.
# $Futils::SL implements the directory path seperator for the host platform
If you want to use the Futils module, just put it in the same directory
as your script/program. Merry Christmas, Ho-Ho-Ho! I wish everything was
that simple. Really, I'm serious -- that's all you have to do. Download
the code and put it into the directory where you run your script/program
and use it like any other module.