<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: Please sir, write my code.</title>
	<link>http://ask.metafilter.com/6891/Please-sir-write-my-code/</link>
	<description>Comments on Ask MetaFilter post Please sir, write my code.</description>
	<pubDate>Thu, 29 Apr 2004 17:51:55 -0800</pubDate>
	<lastBuildDate>Thu, 29 Apr 2004 17:51:55 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: Please sir, write my code.</title>
		<link>http://ask.metafilter.com/6891/Please-sir-write-my-code</link>	
		<description>Any MeFi perl/cgi programmers feel like writing a few lines of code?&lt;br&gt;
&lt;br&gt;
I need to add a function to an existing script, which looks like a piece of cake if you know perl.&lt;br&gt;
&lt;br&gt;
Given a $newstring and a file of records, each consisting of a count and a string delimited by a space:&lt;br&gt;
&lt;br&gt;
201 this.thing&lt;br&gt;
23 that.thing&lt;br&gt;
14 someother.thing&lt;br&gt;
...&lt;br&gt;
&lt;br&gt;
I need a function to check whether $newstring already exists in the file. If so, increment the count. If not, add a new record to the file with a count of 1. Finally, sort the file in descending order by count.&lt;br&gt;
&lt;br&gt;
Maybe 5 minutes for you. Learning perl first for me. If it would be easier with fixed length counts like 0000201, that is fine, too.&lt;br&gt;
&lt;br&gt;
If this is inappropriate for Ask MeFi, my apologies. I&apos;ll take it over to answers.google.</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2004:site.6891</guid>
		<pubDate>Thu, 29 Apr 2004 17:01:00 -0800</pubDate>
		<dc:creator>Geo</dc:creator>
		
			<category>software</category>
		
			<category>perl</category>
		
			<category>script</category>
		
	</item> <item>
		<title>By: whatnotever</title>
		<link>http://ask.metafilter.com/6891/Please-sir-write-my-code#139071</link>	
		<description>#!/usr/bin/perl&lt;br&gt;
&lt;br&gt;
# the input string - this just gets it from the command line&lt;br&gt;
$newstring = $ARGV[0];&lt;br&gt;
&lt;br&gt;
# temp variables&lt;br&gt;
my $found = 0;&lt;br&gt;
my @lines;&lt;br&gt;
&lt;br&gt;
# open filename.txt for reading&lt;br&gt;
open FILE, &quot;&amp;lt;filename.txt&quot;;&lt;br&gt;
&lt;br&gt;
# for each line in the file&lt;br&gt;
while (&amp;lt;FILE&amp;gt;) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# on any line with a number, a space, and $newstring, increment the number, and remember that we found it&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (s/^(\d+)(?=\s+$newstring)/$1 + 1/e) {$found = 1;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# add this line to our array of lines&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;push @lines, $_;&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
close FILE;&lt;br&gt;
&lt;br&gt;
# if we didn&apos;t find it, make the new line&lt;br&gt;
if (!$found) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;push @lines, &quot;1 $newstring\n&quot;;&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
# sort the lines&lt;br&gt;
@lines = sort {int($a) &amp;lt;=&amp;gt; int($b)} @lines;&lt;br&gt;
&lt;br&gt;
# open the same file for writing&lt;br&gt;
open FILE, &quot;&amp;gt;filename.txt&quot;;&lt;br&gt;
# write out each line to the file&lt;br&gt;
for (@lines) {print FILE;}&lt;br&gt;
close FILE;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.6891-139071</guid>
		<pubDate>Thu, 29 Apr 2004 17:51:55 -0800</pubDate>
		<dc:creator>whatnotever</dc:creator>
	</item><item>
		<title>By: thebabelfish</title>
		<link>http://ask.metafilter.com/6891/Please-sir-write-my-code#139072</link>	
		<description>You should change whatnotever&apos;s line &lt;code&gt;s/^(\d+)(?=\s+$newstring)/$1 + 1/e&lt;/code&gt; to &lt;code&gt;s/^(\d+)(?=\s+\Q$newstring\E)/$1 + 1/e&lt;/code&gt; so that regex metachars in $newstring don&apos;t mess up the regex.&lt;br&gt;
&lt;br&gt;
In any case, next time you have a Perl question, take it on over to &lt;a href=&quot;http://perlmonks.org&quot;&gt;PerlMonks&lt;/a&gt; where they answer questions like this all the time.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.6891-139072</guid>
		<pubDate>Thu, 29 Apr 2004 17:57:33 -0800</pubDate>
		<dc:creator>thebabelfish</dc:creator>
	</item><item>
		<title>By: cmonkey</title>
		<link>http://ask.metafilter.com/6891/Please-sir-write-my-code#139073</link>	
		<description>Another way of doing it: &lt;br&gt;
&lt;br&gt;
$file = &quot;/tmp/infile&quot;; #replace with your input file&lt;br&gt;
$out = &quot;/tmp/outfile&quot;;&lt;br&gt;
$found = 0;&lt;br&gt;
&lt;br&gt;
open (IN, $file);&lt;br&gt;
open (OUT, &quot;&amp;gt;$out&quot;);&lt;br&gt;
&lt;br&gt;
while (&amp;lt;IN&amp;gt;) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;if ($_ =~ /$newstring/) { &lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;$counter, $string) = split /\s/; &lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;$counter++; &lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;$found = 1; &lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;print OUT &quot;$counter $string\n&quot;;temp&lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;} else {&lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;print OUT $_; &lt;br&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;}&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
if ($found != 1) { print OUT &quot;1 $newstring\n&quot;; }&lt;br&gt;
close(IN);&lt;br&gt;
close(OUT);&lt;br&gt;
`mv $out $file`;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.6891-139073</guid>
		<pubDate>Thu, 29 Apr 2004 18:04:07 -0800</pubDate>
		<dc:creator>cmonkey</dc:creator>
	</item><item>
		<title>By: nicwolff</title>
		<link>http://ask.metafilter.com/6891/Please-sir-write-my-code#139077</link>	
		<description>Fine, until one CGI process opens and reads the file, another reads and writes it, and then the first overwrites the second&apos;s changes... Try this (but I didn&apos;t test it!):&lt;br&gt;
&lt;br&gt;
use Tie::File;&lt;br&gt;
sub update_count {&lt;br&gt;
&amp;nbsp;&amp;nbsp;my $newstring = shift;&lt;br&gt;
&amp;nbsp;&amp;nbsp;tie( my @records, Tie::File =&amp;gt; &apos;filename&apos; )-&amp;gt;flock;&lt;br&gt;
&amp;nbsp;&amp;nbsp;s/^(\d+)(?=\s+\Q$newstring\E)/$1 + 1/e for @records;&lt;br&gt;
&amp;nbsp;&amp;nbsp;push @records, &quot;1 $newstring&quot; unless $1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;@records = sort @records;&lt;br&gt;
}</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.6891-139077</guid>
		<pubDate>Thu, 29 Apr 2004 18:12:35 -0800</pubDate>
		<dc:creator>nicwolff</dc:creator>
	</item><item>
		<title>By: Geo</title>
		<link>http://ask.metafilter.com/6891/Please-sir-write-my-code#139083</link>	
		<description>This is great! I love this place.  I don&apos;t know why perl seems so impenetrable to me.&lt;br&gt;
&lt;br&gt;
And thanks for the link to PerlMonks. That would be the place.&lt;br&gt;
&lt;br&gt;
I hope I can return the favor one day. Need any Delphi?</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.6891-139083</guid>
		<pubDate>Thu, 29 Apr 2004 18:23:10 -0800</pubDate>
		<dc:creator>Geo</dc:creator>
	</item><item>
		<title>By: nicwolff</title>
		<link>http://ask.metafilter.com/6891/Please-sir-write-my-code#139090</link>	
		<description>Whoops! You wanted descending order, so that last line should be &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;@records = reverse sort @records;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.6891-139090</guid>
		<pubDate>Thu, 29 Apr 2004 18:52:40 -0800</pubDate>
		<dc:creator>nicwolff</dc:creator>
	</item><item>
		<title>By: nicwolff</title>
		<link>http://ask.metafilter.com/6891/Please-sir-write-my-code#139094</link>	
		<description>OK, I&apos;m silly - reverse is slow and the default sort won&apos;t work - here&apos;s one I actually tested:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;@records = sort { (split &apos; &apos;, $b)[0] &lt;&gt; (split &apos; &apos;, $a)[0] } @records;&lt;br&gt;
&amp;nbsp;&lt;/&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.6891-139094</guid>
		<pubDate>Thu, 29 Apr 2004 19:01:21 -0800</pubDate>
		<dc:creator>nicwolff</dc:creator>
	</item><item>
		<title>By: nicwolff</title>
		<link>http://ask.metafilter.com/6891/Please-sir-write-my-code#139095</link>	
		<description>D&apos;oh, MeFi tagulated my flying saucer! It&apos;s:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;@records = sort { (split &apos; &apos;, $b)[0] &amp;lt;=&amp;gt; (split &apos; &apos;, $a)[0] } @records;&lt;br&gt;
&amp;nbsp;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.6891-139095</guid>
		<pubDate>Thu, 29 Apr 2004 19:03:08 -0800</pubDate>
		<dc:creator>nicwolff</dc:creator>
	</item><item>
		<title>By: costas</title>
		<link>http://ask.metafilter.com/6891/Please-sir-write-my-code#139142</link>	
		<description>OK, I just have to do the same in python:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;br&gt;
#!env python&lt;br&gt;
import sys&lt;br&gt;
&lt;br&gt;
def firstsort(this, that):&lt;br&gt;
   return cmp(this[0], that[0])&lt;br&gt;
&lt;br&gt;
infile = open(sys.argv[2], &apos;r&apos;)&lt;br&gt;
outfile = open(sys.argv[3], &apos;w&apos;)&lt;br&gt;
inlines = infile.readlines()&lt;br&gt;
outlines = []&lt;br&gt;
&lt;br&gt;
for line in inlines:&lt;br&gt;
   count = line.split(&apos; &apos;)[0]&lt;br&gt;
   string = line.split(&apos; &apos;)[1]&lt;br&gt;
   if newstring == string:&lt;br&gt;
      count += 1&lt;br&gt;
   outlines.append((count, string))&lt;br&gt;
&lt;br&gt;
outlines.sort()&lt;br&gt;
outlines.reverse()&lt;br&gt;
&lt;br&gt;
for line in outlines:&lt;br&gt;
   outfile.write(&quot;%s %s&quot; % line)&lt;br&gt;
&lt;/pre&gt;&lt;br&gt;
&lt;br&gt;
This is maximized for readability; I hope it&apos;s less impenetrable than perl :-)</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.6891-139142</guid>
		<pubDate>Thu, 29 Apr 2004 21:35:14 -0800</pubDate>
		<dc:creator>costas</dc:creator>
	</item><item>
		<title>By: Geo</title>
		<link>http://ask.metafilter.com/6891/Please-sir-write-my-code#139238</link>	
		<description>I&apos;m tempted to post an APL version. I&apos;m sure it could be done in one line.&lt;br&gt;
&lt;br&gt;
The posts have been instructive. All I need to do now is figure out the strange error messages from my ISP.&lt;br&gt;
&lt;br&gt;
Thanks everyone.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.6891-139238</guid>
		<pubDate>Fri, 30 Apr 2004 07:21:02 -0800</pubDate>
		<dc:creator>Geo</dc:creator>
	</item>
	</channel>
</rss>
