Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: alphabet counting

by sauoq (Abbot)
on Jun 02, 2012 at 13:44 UTC ( #974013=note: print w/ replies, xml ) Need Help??


in reply to alphabet counting

You are printing inside your loop.

You want to accumulate your values and then only print them after you are done.

As an aside, it would be more perlish if you were using a hash to accumulate your values... you could change all your ifs to a single line.

And it's probably not a good idea to call the filehandle IN when you are using it for output.

-sauoq
"My two cents aren't worth a dime.";


Comment on Re: alphabet counting
Re^2: alphabet counting
by Anonymous Monk on Jun 02, 2012 at 14:09 UTC

    i have problem with hash printing !!!

      i have problem with hash printing !!!

      You could guess that any such problems could be easily resolved... did you try something like this:

      print "$k => $h{$k}\n" for my $k (sort keys %h);
      ?

      -sauoq
      "My two cents aren't worth a dime.";

        Hmm...

        my %h = ( a => 1, b => 2, c => 3 ); print "$k => $h{$k}\n" for my $k (sort keys %h);

        Output:

        syntax error at D:\progs\perl\junk.pl line 5, near "$k (" Execution of D:\progs\perl\junk.pl aborted due to compilation errors.

        Maybe you meant something like:

        print "$_ => $h{$_}\n" for sort keys %h;

        Or the (no doubt) more newbie-friendly:

        for my $k ( sort keys %h ) { print "$k => $h{$k}\n"; };

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-09-18 02:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (104 votes), past polls