tag:blogger.com,1999:blog-3035323255246508397.post1592341674024609833..comments2023-06-10T09:01:43.125-07:00Comments on Perl Indonesia: Given/when expressionPerl Indonesiahttp://www.blogger.com/profile/09083793321887864209noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-3035323255246508397.post-88606839702150206712010-01-05T04:26:18.414-08:002010-01-05T04:26:18.414-08:00@schelcj: Yes, given/when is merely a syntactic su...@schelcj: Yes, given/when is merely a syntactic sugar for if/elsif blocks. But it has one nicety: implicit use of smart matching. Also, hash map only works for exact string matching.Steven Haryantohttps://www.blogger.com/profile/09282042440154229218noreply@blogger.comtag:blogger.com,1999:blog-3035323255246508397.post-87077776940057030792009-12-31T19:22:41.613-08:002009-12-31T19:22:41.613-08:00i fail to see how a huge given/when block -- which...i fail to see how a huge given/when block -- which is really just another way of writing if/elsif blocks -- is any better then using a hash map.Anonymoushttps://www.blogger.com/profile/10584207824387627620noreply@blogger.comtag:blogger.com,1999:blog-3035323255246508397.post-8220275333019834582009-12-28T16:55:08.563-08:002009-12-28T16:55:08.563-08:00As a side note, I found out today that Ruby's ...As a side note, I found out today that Ruby's case statement can be used in an expression:<br /><br /><<'EOE';<br />car = "Patriot"<br /><br />manufacturer = case car<br /> when "Focus" then "Ford"<br /> when "Navigator" then "Lincoln"<br /> when "Camry" then "Toyota"<br /> when "Civic" then "Honda"<br /> when "Patriot" then "Jeep"<br /> when "Jetta" then "VW"<br /> when "Ceyene" then "Porsche"<br /> when "Outback" then "Subaru"<br /> when "520i" then "BMW"<br /> when "Tundra" then "Nissan"<br /> else "Unknown"<br />end<br /><br />puts "The " + car + " is made by " + manufacturer<br />EOE<br /><br /><br />It is a nice language after all, isn't? Now that makes it "3 things I wish we had in Perl" (besides symbols and optional semicolons).Steven Haryantohttps://www.blogger.com/profile/09282042440154229218noreply@blogger.comtag:blogger.com,1999:blog-3035323255246508397.post-2309530630183167742009-12-28T16:49:13.175-08:002009-12-28T16:49:13.175-08:00@schelcj: admittedly my example is not great, but ...@schelcj: admittedly my example is not great, but I think you're missing the point. I'm *not* trying to solve the "store sales" problem :-)Steven Haryantohttps://www.blogger.com/profile/09282042440154229218noreply@blogger.comtag:blogger.com,1999:blog-3035323255246508397.post-5225256023505677902009-12-28T16:47:50.826-08:002009-12-28T16:47:50.826-08:00@Carey: thanks for the suggestion, but I'm fai...@Carey: thanks for the suggestion, but I'm failing to see how gather/take is appropriate or superior in this case (pun intended). It doesn't give implicit smart matching, for one.Steven Haryantohttps://www.blogger.com/profile/09282042440154229218noreply@blogger.comtag:blogger.com,1999:blog-3035323255246508397.post-79333832742824346532009-12-28T11:43:40.100-08:002009-12-28T11:43:40.100-08:00why not something like:
my %store_map = (
'...why not something like:<br /><br />my %store_map = (<br /> 'Los Angeles' => {sales => 100_000, factor => 2},<br /> 'San Deigo' => {sales => 100_000, factor => 1.5},<br /> 'Anahiem' => {sales => 100_000, factor => 1},<br />);<br /><br />sub get_sales {<br /> my ($city) = @_;<br /> return $store_map{$city}->{sales} * $store_map{$city}->{factor};<br />}Anonymoushttps://www.blogger.com/profile/10584207824387627620noreply@blogger.comtag:blogger.com,1999:blog-3035323255246508397.post-89721501296825797792009-12-28T11:09:38.095-08:002009-12-28T11:09:38.095-08:00I would suggest gather/take, since you're alre...I would suggest gather/take, since you're already delving into features inspired by Perl 6. There's a module (Perl6::Gather) that gives you this feature in Perl 5. I can't say how stable it is, but it's worth investigating.Careyhttps://www.blogger.com/profile/10316232481882531170noreply@blogger.com