Loading ...
Sorry, an error occurred while loading the content.

Need help with Hash Values

Expand Messages
  • sxsakura
    I try to save some values,and count the words that has given by user. and write this: #! /usr/bin/perl -w while( ) { ; chomp; $words{$_} += 1; }
    Message 1 of 4 , Jul 1, 2010
    • 0 Attachment
      I try to save some values,and count the words that has given by user.
      and write this:
      #! /usr/bin/perl -w
      while(<>)
      {
      <STDIN>;
      chomp;
      $words{$_} += 1;
      }

      foreach (sort keys %words)
      {
      print "$_ has appeared $words{$_} times\n";
      }

      But when type these:
      hello
      hello
      hi
      hi
      nihao
      nihao
      hey
      hei

      It returns:
      hello has appeared 1 times
      hey has appeared 1 times
      hi has appeared 1 times
      nihao has appeared 1 times

      Obviously,some words like "hello",appears 2 times,but the program only counted 1 time.

      But,if write like this:
      #! /usr/bin/perl -w
      @words=<STDIN>;
      foreach (@words)
      {
      $counts{$_} += 1;
      }
      foreach (sort keys %counts)
      {
      print "$_ appeared $counts{$_} times\n";
      }

      It will works quite well.
      Do anyone know why I can't save the @word value?
    • Sakura Kinomoto
      Thanks,It solved~~ ________________________________ From: Shlomi Fish To: perl-beginner@yahoogroups.com Sent: Thu, July 1, 2010 7:29:26
      Message 2 of 4 , Jul 2, 2010
      • 0 Attachment
        Thanks,It solved~~



        ________________________________
        From: Shlomi Fish <shlomif@...>
        To: perl-beginner@yahoogroups.com
        Sent: Thu, July 1, 2010 7:29:26 PM
        Subject: Re: [PBML] Need help with Hash Values

        On Thursday 01 Jul 2010 14:14:07 sxsakura wrote:
        > Em....While doing the homework that gives from the "Learning Perl" books.
        > The homework is "Give out some strings (a word per line) and count the word
        > that has been given" so I typed:
        > #! /usr/bin/perl -w
        >
        > while(<>)
        > {
        > <STDIN>;

        You don't need this statement (the "<STDIN>;" one). It just skips a line.
        Without it, it should be fine.

        Regards,

        Shlomi Fish

        > chomp;
        > $words{$_} += 1;
        > }
        >
        > foreach (sort keys %words)
        > {
        > print "$_ has appeared $words{$_} times\n";
        > }
        >
        > and run it,and these are what i typed and what it gave back:
        > hello
        > hello
        > hi
        > hi
        > nihao
        > nihao
        > hey
        > hei
        > hello has appeared 1 times
        > hey has appeared 1 times
        > hi has appeared 1 times
        > nihao has appeared 1 times
        >
        > Obviously,the words"hello","hi","nihao" has appeared 2 times,but the
        > program only counted 1 time,why? And if I use a array of values to read
        > the input data and then count it,it will goes well like:
        > @word=<STDIN>;
        > foreach (@word)
        > {
        > $counts{$_} += 1;
        > }
        > why~?
        > need help,thanks

        --
        -----------------------------------------------------------------
        Shlomi Fish http://www.shlomifish.org/
        Funny Anti-Terrorism Story - http://shlom.in/enemy

        God considered inflicting XSLT as the tenth plague of Egypt, but then
        decided against it because he thought it would be too evil.

        Please reply to list if it's a mailing list post - http://shlom.in/reply .


        ------------------------------------

        Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.htmlYahoo! Groups Links






        [Non-text portions of this message have been removed]
      Your message has been successfully submitted and would be delivered to recipients shortly.