Seitenanfang

Counting letters in Perl

Dieser Post wurde aus meiner alten WordPress-Installation importiert. Sollte es Darstellungsprobleme, falsche Links oder fehlende Bilder geben, bitte einfach hier einen Kommentar hinterlassen. Danke.


A blog comment resulting in a new post? Yes, a guy from southern Germany made me do this by trying to use the whole alphabet in his comment.

Kerstin alias Testmama started a blogger contest lately. I didn't want to but got involved and one of the challenges was an alphabet: Write something about you, your blog and your kids for every letter from A to Z. I did this and challenged my visitors to write a comment using all letters from A to Z themselfs. One guy tried to do so and I wanted to know if he succeeded.

Here is his comment:

V wie Vielen Dank für dein AlphabetInteressantes Layout Dein W gefällt mir gut, bei uns im Kindergarten gibts dafür immer “garnix”.Was habt ihr heute gemacht? garnix! was gabs zu essen? garnix! usw.Na welche Buchstaben fehlen zum Alphabet? Wenn ich jetzt Quarz schreibe, hab ich, glaub ich, alle Buchstaben untergebracht, aber das gibt keinen Sinn
Short text, isn't it? I noticed his try to do the challenge at the second reading - and now I need to know if he did it and if all letters are used. I'm a developer, searching & counting letters myself isn't something I'ld do. I two minutes to write a Perl onliner to count and about ten to fiveteen minutes to write this blog post instead of using one minute to count manually. Not really intelligent in terms of time management but the developers way ;-)

Here is my script:

perl -nle 'for (split(//)) { ++$b{lc $_} } END { print join("\n",map{"$b{$_}\t$_"} sort keys %b); }'
Maybe I'll start a Perl golf challenge out of this, but the line above just splits all input text in one-letter-pieces and counts every letters occurrences.

The first part splits up every input line (-n) into single chars (for ... split) and counts the lower-case-representation of them (++$b{lc $_}).

The END block is executed after all input is read and outputs the contents of %b:

60	 2	!5	,2	.3	?27	a16	b9	c6	d36	e4	f12	g16	h22	i1	j3	k11	l6	m24	n1	o2	p1	q16	r16	s19	t12	u2	v7	w3	x1	y4	z...plus some unreadable chars.
60 spaces and - all 26 letters. Congratulations, challenge won!

 

 

4 Kommentare. Schreib was dazu

  1. Carsten erzählte mir heute Nacht sogar noch davon, dass er alle Buchstaben zu benutzen versuchte und zeigte mir seinen Text. :)
    Ich freue mich sehr, dass wir an einigen deiner Blogartikel schuld sind. Den Artikel habe ich ihm übrigens gleich weitergeleitet, ich gehe davon aus, dass ihn das berufsbedingt interessieren könnte ;)

  2. Nice Script, and sorry that you had to invest so much time because i tried your challenge. Yesterday, when i commented your blogpost i just wanted to comment. But after i read my comment i noticed that only 2 letters was missing. So i wrote that thing about "jetzt quarz" and all 26letters was used. I was wondering that no one else tried the challenge.
    Btw: I counted the letters by myself, didnt even think about using a script or something like this :-)

  3. Sebastian

    Using an onliner was just a idea and I'm always looking for developent topics... People use way too few oneliners to save time :-)

  4. glb

    My attempt to golf :
    echo "Portez ce vieux whisky au juge blond qui fume" | \
    perl -nle '$b{lc$_}++for split//}{print"$b{$_}\t$_"for sort keys%b'
    (but it does not take care of accented characters).

Schreib was dazu

Die folgenden HTML-Tags sind erlaubt:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>