{"id":2114,"date":"2009-06-23T19:13:03","date_gmt":"2009-06-24T02:13:03","guid":{"rendered":"http:\/\/www.sheer.us\/wordpress\/?p=2114"},"modified":"2009-06-23T19:13:03","modified_gmt":"2009-06-24T02:13:03","slug":"random-thoughts-from-scripting-perl","status":"publish","type":"post","link":"https:\/\/www.sheer.us\/weblogs\/uncategorized\/random-thoughts-from-scripting-perl","title":{"rendered":"Random thoughts from scripting (perl)"},"content":{"rendered":"<p>Here&#8217;s a couple of nifty tricks from today&#8217;s scripting adventures:<\/p>\n<p>1) This one is sort of obvious in retrospect &#8211; but, if you&#8217;re processing a long list of items and want to show a status indicator, this works rather well:<\/p>\n<p><PRE><br \/>\nfor($i=0;$i<10000;$i++) {\n    print \"$i\\r\" if(!($i % 100));\n}\n<\/PRE><\/p>\n<p>I&#8217;ve always used the !($i % 100) trick, but never the \\r, which returns the carriage to the beginning of the line. <\/p>\n<p>Also fun is:<br \/>\n<PRE><br \/>\n$|=1;<\/p>\n<p>@spinner = (&#8220;|&#8221;,&#8221;\/&#8221;,&#8221;-&#8220;,&#8221;\\\\&#8221;);<br \/>\nfor($i=0;$i<10000;$i++) {\n        print $spinner[($i % 4)] . \"\\r\";\n        select(undef,undef,undef,0.1);\n}\n<\/PRE><\/p>\n<p>2) moving a byte: handy constants to memorize are 65280, 16711680, and 4278190080, which are the second eight bits, the third eight bits, and the forth eight bits, respectively. You can do ($value &#038; 65280) >> 8, ($value &#038; 16711680) >> 16, and ($value &#038; 4278190080) >> 24, respectively, to get at bytes two, three, and four. The reverse operation is even easier: $b1 + ($b2 << 8) + ($b3 << 16) + ($b4 << 24);\n\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a couple of nifty tricks from today&#8217;s scripting adventures: 1) This one is sort of obvious in retrospect &#8211; but, if you&#8217;re processing a long list of items and want to show a status indicator, this works rather well: for($i=0;$i 8, ($value &#038; 16711680) >> 16, and ($value &#038; 4278190080) >> 24, respectively, to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/posts\/2114"}],"collection":[{"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/comments?post=2114"}],"version-history":[{"count":0,"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/posts\/2114\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/media?parent=2114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/categories?post=2114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/tags?post=2114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}