CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Fun With Perl: Perl Golf:
A fun one.

 



Coderifous
journeyman

Oct 23, 2002, 5:08 AM

Post #1 of 5 (36873 views)
A fun one. Can't Post

I saw this posted as a question in another forum, and thought that it would make for a fun round of perl golf here.

This was the original poster description of his problem ( a snippet anyways ):


Quote


from the command line :

./script.pl this is a test

output:
t:3
h:1
i:2
s:3
a:1
e:1



From this I define the course: code must produce a character:count (new line delimited) output for each unique character present in the argument string. Output doesn't have to be in any particular order, but it's cooler if it's "in order" in relation to the original arg string. And even though the example above doesn't count spaces, I'd be curious to see two different versions of the code. For me it was the difference of using a join() or not. So there are two holes here: Version with spaces, and version that doesn't count spaces. Alright... have fun.

My answers: (to be crushed I'm sure)


Code
 $_="@ARGV";/./&&print$/.$&,':',s,$&,,g while($_); #Displays Spaces
$"='';$_="@ARGV";/./&&print$/.$&,':',s,$&,,g while($_); #skips em'



Have fun!

--jim


mhx
Enthusiast / Moderator

Nov 10, 2002, 6:05 AM

Post #2 of 5 (36834 views)
Re: [Coderifous] A fun one. [In reply to] Can't Post

Hi Jim,

so, here are my attempts. With spaces included:


Code
s;;@ARGV;;printf"$&:%d 
",s,$&,,gwhile/./


Without spaces included:


Code
s;;@ARGV;;printf"$&:%d 
",s,$&,,gwhile/\S/


The scores are 40 for the version with spaces (your score: 50) and 41 for the version without spaces (your score: 55).

Cheers,
mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



goober99
Novice

Sep 21, 2004, 11:41 AM

Post #3 of 5 (34196 views)
Wow! Can you explain that. [In reply to] Can't Post

I am interested in doing some Perl golf, and I thought this challenge looked interesting. Then I made the mistake at looking at your awesome solution. I am still new to Perl, but I really enjoy it. There is no way I could have come up with a solution that short.

Being new to Perl, I don't understand how your solution works. I run it on my computer, and sure enough, it worked, but I want to know how. I know this is an old thread, but is there someone who can disect Coderifous' entry step by step for me?

-Matthew Miller
http://www.swingthesickle.org


davorg
Thaumaturge / Moderator

Sep 22, 2004, 1:33 AM

Post #4 of 5 (34180 views)
Re: [goober99] Wow! Can you explain that. [In reply to] Can't Post

The module B::Deparse is always a good way to start understanding code like this. It takes a piece of Perl code, parses it and redisplays it in "canonical" form.


Code
$ perl -MO=Deparse -e 's;;@ARGV;;printf"$&:%d 
> ",s,$&,,gwhile/\S/'
s//@ARGV/;
printf "$&:%d\n", s/$&//g while /\S/;
-e syntax OK


So the code is equivalent to

Code
s//@ARGV/; 
printf "$&:%d\n", s/$&//g while /\S/;

Which is obviously much clearer :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


goober99
Novice

Sep 22, 2004, 12:43 PM

Post #5 of 5 (34175 views)
Re: [davorg] Wow! Can you explain that. [In reply to] Can't Post

Yes, that is much clearer. Thanks!

-Matthew Miller
http://www.swingthesickle.org

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives