Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^4: Looking for pointers or optimizations.

by thmsdrew (Scribe)
on Aug 21, 2012 at 14:20 UTC ( #988753=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Looking for pointers or optimizations.
in thread Looking for pointers or optimizations.

So if I put in while (<$fh>) it breaks my my $guess = <> further down in my code. I made that my $guess = <STDIN> and it works fine.


Comment on Re^4: Looking for pointers or optimizations.
Re^5: Looking for pointers or optimizations.
by 2teez (Priest) on Aug 21, 2012 at 14:57 UTC

    So if I put in while (<$fh>) it breaks my my $guess = <> further down in my code. I made that my $guess = <STDIN> and it works fine.

    both while(<>){..} and $guess = <> reads from STDIN. Atleast, that is obvious from your code. So, to make while(<fh>){.. work, you have to do this:

    my ($words_file) = @ARGV; open( my $fh, "<", $words_file ) or die "can't open file: $!"; my @words; while (<$fh>) { ... } ## later you use chomp(my $guess = <STDIN>); ...
    Please, pay attention to the DIRECTION of the "arrow"
    • ">" is 'Writing to' ( OUTPUT )
    • "<" is 'Read from' ( INPUT )
    NOTE: Like I mentioned previously, you didn't close the your filehandle!!!

      Hej, You probably didn't see this (marked with ####)
      open(my $fh, "<", $ARGV[0]) or die "cannot open input file: $!"; my @words = <$fh>; close $fh or die "cannot close input file: $!"; ############## my $words = @words;
      On the other hand, I don't understand the last line - you read a file into array with my @words = <$fh>; and then you assign it to a scalar variablemy $words = @words;.

      OK, you probably have your own reasons for that but you could manipulate array as good as scalar. And you could read a file directly in a scalar variable. my $words = <$fh>;

        you could read a file directly in a scalar variable.
        my $words = <$fh>;
        Not true. Have you tried it? In fact, you can, but in a different way.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        And you could read a file directly in a scalar...

        Depending on the current value of the input record separator, you might only get one record. In fact, you'll probably only get one record.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://988753]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-08-23 03:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (172 votes), past polls