Baby's First Perl Script

Jul 08, 2005 18:27

Alright, maybe my second ...

while (<>) { push @{$ws_count{+split}}, $_; }

while (($num, $words) = each %ws_count)
{
print "\n----- \#$num -----\n\n";
foreach (@$words) { print; }
}

Though honestly, the first line is the impressive bit, the rest is just nice output formatting ( Read more... )

Leave a comment

Comments 17

willworker July 8 2005, 23:41:49 UTC
Run through the input, counting the words on each line. Plug each line into the array in an array of hashes corresponding to the correct number of words on the line. Then, run through the hash printing out ----- 15 ----- (where 15 is the number of words on the line), followed by a listing of all the lines with that many words on it.

BTW, instead of "foreach (@$words) {print;}" you can use "print foreach(@$words);" to save a couple characters.

Steve

Reply

willworker July 8 2005, 23:43:42 UTC
s/array of hashes/hash of arrays/;

I know what I meant, I just didn't type it that way.
Steve

Reply

iluvsheep July 9 2005, 04:12:53 UTC
Now that I know my journal is read by perl geeks, I will have to make things a bit harder ;-).

I should have just included the first line. The rest of it just makes it too easy to figure it all out.

Reply

willworker July 9 2005, 08:16:29 UTC
Actually, I figured it out based on that line alone. I will admit that it wasn't clear at first what you were doing (automagically creating array refs as necessary) with all those {}'s, but it wasn't half bad once I figured out that the push() was writing to those arrays; I was wondering where the arrays were coming from.

::use[s] strict;::
Steve

Reply


lwoody2k July 9 2005, 00:00:47 UTC
That's not too bad, but I think a line like
if(/\s*(.*):\s*([^=]*);\s*(.*)/)
is much more outrageous. And I could go on with silly examples, but I use (or used) that in code.
I missed the part about all lines with same number of words, but got the res.

Reply

lwoody2k July 9 2005, 00:01:11 UTC
rest. Not res. Yeah...

Reply

iluvsheep July 9 2005, 04:11:46 UTC
Playing a bit too much NS are we?

Reply

lwoody2k July 11 2005, 22:59:07 UTC
Actually, no. WoW is my current crack of choice (or need....) and last time I tried to play NS I couldn't connect to a server. Meh, WoW is Fun and Exciting (tm) for now. And maybe if I don't get bored of it for long enough, they'll finish porting NS to source. Drool.

Also, since I don't want to respond separately to the other comment, I know it's a mere regexp, but it's much less english-y. I have some really bad lines ($_->[0][0][0] = something for example), but I think posting any of the interesting ones does something like break an NDA and makes me lose my soul. Or something.

Reply


fitek July 10 2005, 21:36:18 UTC
I hope you didn't really use that code.

One day, I will run into a syntax bending open source developer and will not be able to restain myself as I strangle him. Bystanders will pull me off as the police arrive, but they'll be unable to calm my rage. As the men in white suits taser me, I'll scream about code readability and reuse. When I escape the asylum, I'll become Codeman. My mission: somewhere in this world, syntax abuse is being committed. I will be there to stop it.

The golden age of man will begin, bringing in an era of peace and prosperity. Until the next Bush is elected.

Reply

iluvsheep July 12 2005, 15:58:25 UTC
I can neither confirm nor deny these claims of having actually used this code. (Especially following such a stirring anecdote of righteousness and vigilante justice).

Reply


Leave a comment

Up