tag:blogger.com,1999:blog-5907655605807122632.post8155094089208294717..comments2024-03-19T02:17:41.360-07:00Comments on Perl Adventures: Loops and conditionals: For eachAnonymoushttp://www.blogger.com/profile/14422352024976746995noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-5907655605807122632.post-55237373512279172032014-04-15T06:39:34.307-07:002014-04-15T06:39:34.307-07:00In very early versions of perl (maybe until someti...In very early versions of perl (maybe until sometime in perl 2) for and foreach were not interchangeable. For was the C-style variant, and foreach was the list iterator variant. They later got merged so that you could use either keyword for either purpose. I think the reason was that the C-style for loop was being recognized as mostly unused (separating it by moving the final section into a continue block was much more readable) while the iterator foreach loop was very heavily used and it was desired to have the shorter keyword be available for that purpose.<br /><br />For map example is rather confusing. You are initializing the array to the list of return values from the second print done within the map code block, which is a rather obfuscated way of getting a list of integer 1 values (that might not be one if the print fails).Anonymoushttps://www.blogger.com/profile/02956912705051808266noreply@blogger.comtag:blogger.com,1999:blog-5907655605807122632.post-42162083543109398412014-04-14T10:33:17.049-07:002014-04-14T10:33:17.049-07:00This is true, but the C-style for loop is still us...This is true, but the C-style for loop is still useful for when you want to step by a different value than one. <br /><br />Also, while newer perls know to recognize the range operator and optimize for it, old perls will create a temporary array with all those values in it, which can eat up lots of memory if your numbers are big enough. Aaron Privenhttps://www.blogger.com/profile/03917490770780098370noreply@blogger.comtag:blogger.com,1999:blog-5907655605807122632.post-1632492441972079832014-04-14T07:33:18.651-07:002014-04-14T07:33:18.651-07:00The C-style loop control can be replaced by:
for ...The C-style loop control can be replaced by:<br /> for ( 1..9 ) { say } (or say $_ if you want to be explicit). say for (1..9) also works.<br /><br />Non-numeric ranges are also possible: "say for (a..d)" works. Be careful with character-set order if you do this. Ajrhttps://www.blogger.com/profile/07411394674329761835noreply@blogger.comtag:blogger.com,1999:blog-5907655605807122632.post-14758709396195227792014-04-11T07:44:46.598-07:002014-04-11T07:44:46.598-07:00:o so I am:o so I amAnonymoushttps://www.blogger.com/profile/14422352024976746995noreply@blogger.comtag:blogger.com,1999:blog-5907655605807122632.post-36084881756460866732014-04-11T01:21:16.152-07:002014-04-11T01:21:16.152-07:00Looks like you're missing an opening brace on ...Looks like you're missing an opening brace on line 1 of the map example.Dave Crosshttps://www.blogger.com/profile/17165198648105678551noreply@blogger.com