Hello Derek,

> Guay, 

Err, my first name is Jean-Sebastien. My last name is Guay. French-language
people have a bad habit to put the last name first, as in "Guay,
Jean-Sebastien"... So I understand why this is a bit confusing.

> What do you mean when you say "Just use $line inside the while, and you'll
read one line at a time."  ??? 
> I am doing this, aren't I? 

No. I will explain step by step what your code does:

> open (CRITICALSERVERS, "$crout") || die "can't open file \n: $!";

As I said, you should replace || by or in the above line. See the precedence
rules in "perldoc perlop" for details.

> while ( defined($line = <CRITICALSERVERS>) ) {

Here, you read one line into $line, and the "current line pointer" advances
to the next line. The while is essentially there to check that you don't
read past the end of file. So in theory, the above construct will read one
line at a time, until the end of file.

>     chomp ($line);

Here, you remove the end of line delimiter (\n, \r\n or \r depending on your
platform). This is good, before working with the text inside a variable you
should make sure there is no end of line.

>     my @tsm = <CRITICALSERVERS>;

Here, you slurp the whole file, minus the first line which you read into
$line above, into an array. After this, the whole file will be in your @tsm
array, one line per array element, and the "current line pointer" will be at
the end of the file.

>    foreach $_ (@tsm) {
>        print $_;
>    }

This will print every line that's in the array. Note that as I said above,
the first line will not be in the array, so you will not see all the lines
of your file printed onscreen...

> }
> close (CRITICALSERVERS);


Perhaps you are not familiar with the term "slurp". What that means, is that
you are reading the whole file into memory. This is wasteful, and can lead
to problems later, if you try to do it with a very large file (not enough
memory, machine starts swapping, etc). But it is also sometimes faster, if
you need to do a very simple step on every line of your file. In most cases,
you should not slurp a file. Instead, read one line at a time.

Here is the code I would suggest.

    open (CRITICALSERVERS, "$crout") or die "can't open file \n: $!";
    my $line;
    while ( $line = <CRITICALSERVERS> ) {
        chomp ($line);
        print $line . "\n";
    }
    close (CRITICALSERVERS);

Essentially, the changes are the following:
1) Changed || for or on the open line
2) No need to check if $line is defined after reading, since $line will at
least contain a "\n" when the line is empty and will contain nothing at end
of file.
3) The while will read one line at a time, and print it. Nowhere do I read
the whole file at once, as you did in your "my @tsm = <CRITICALSERVERS>;"
line.

So that's what I meant. I hope it is all clear.

If you really wanted to slurp the file, here is the corresponding code you
should use, so that you are not missing one line...

    open (CRITICALSERVERS, "$crout") or die "can't open file \n: $!";
    my @tsm = <CRITICALSERVERS>
    close (CRITICALSERVERS);

    foreach my $line (@tsm) {
        chomp ($line);
        print $line . "\n";
    }

As you can see, it is very similar, but since we get the whole file into the
array, we can close it right after, and also we loop over the array instead
of looping and getting one line at a time until we get to the end of the
file.


Hope this helps,


Jean-Sébastien


-----Message d'origine-----
De: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Date: 2 avril, 2004 10:12
Ŕ: Guay Jean -Sébastien
Cc: Perl Beginners
Objet: RE: using strict



Guay, 

ooops..... that would be it!  someone's else's eyes are always better!
Thanks for the nice explanation!  My code is now compiling smoothly! 
What do you mean when you say "  Just use $line inside the while, and you'll
read one line at a time."  ??? 

I am doing this, aren't I? 

  FROM GUAY: [[  By the way, why are you reading one line in the whiles
parentheses, and
then slurping the rest of the file into an array inside the while?   ]] 

RESPONSE FROM DEREK : I did notice this after you pointed it out, so my goal
is to read in all the lines and put them in their own element, then print
out those specific elements.  Do you have any thoughts? 

thanks! 


Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams
614-566-4145



Guay Jean-Sébastien <[EMAIL PROTECTED]> 
04/01/2004 05:10 PM 
        
        To:        Perl Beginners <[EMAIL PROTECTED]> 
        cc:         
        Subject:        RE: using strict



Hello Derek,

When using strict, the error message should point you to the line where the
error is. It's usually pretty darn good at pointing the right line. In this
case, I bet it's this one:

>        while ( defined($line = <CRITICALSERVERS>) ) {

There is no declaration of the $line variable. Try declaring it first, like
this:

       my $line;
       while ( defined($line = <CRITICALSERVERS>) ) {

I think that's the only variable that wasn't declared. If you get any more
error messages, you can check them out and figure out what's wrong.

Just a suggestion: You should use or instead of || when checking if a file
opened correctly, because if you ever want to do some operations on the
right side, or will bind less tightly and allow your operations to work
correctly without needing parentheses to fix the precedence.

By the way, why are you reading one line in the while's parentheses, and and
then slurping the rest of the file into an array inside the while? That
means your while will only be run once, because at the next iteration you'll
already be at the end of the file... Just use $line inside the while, and
you'll read one line at a time. 

You also chomped your line, which is good. Just remember to re-add the line
ending ("\n") when printing the lines as you're doing, or else all the lines
will be printed on a single, very long line.

I'll let the experts explain what soft references and barewords are. I've
never used either, as I come from the relatively new school of programming.
As I understand, they are features of the language that are dangerous to use
in some contexts, so use strict disallows them knowing that if you really
want to use them, you'll disable strict for the block of code where you use
it, and then re-enable it.

Hope this helps,


Jean-Sébastien Guay

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to