[2000/09/03]

[PQ][Perl Quiz 2000-09-03 No.0060]の返信

眠かったので、内容としてはこれだけしか書かなかった。 もうちょっと書いておけば良かったと思った。

#!/usr/bin/perl
@rhymes = (
    "Humpty Dumpty sat on a wall,",
    "Humpty Dumpty had a great fall;",
    "All the King's horses, and all the King's men",
    "Cannot put Humpty Dumpty together again.",
);

for (@rhymes) {
  s/[^\w']/ /g;
  for (split) {
    $h{lc $_}++;
  }
}

for (sort keys %h) {
  print "$_ $h{$_}\n";
}

同じものをRubyでも書いてみました。

#!/usr/bin/ruby
rhymes = [
  "Humpty Dumpty sat on a wall,",
  "Humpty Dumpty had a great fall;",
  "All the King's horses, and all the King's men",
  "Cannot put Humpty Dumpty together again.",
]

h = Hash.new 0

rhymes.collect{|s|
  s.gsub(/[^\w']/, ' ').split
}.flatten.each{|s|
  h[s.downcase] += 1
}

h.keys.sort.each{|s|
  puts "#{s} #{h[s]}\n"
}

RDtool

RDtoolが使えなかったので公開できず。


上へ indexへ
copyright © 2000 ZnZ