eep?

Jun. 17th, 2012 04:34 pm
jazzfish: Windows error message "Error 255: Too many errors." (Too many errors)
[personal profile] jazzfish
I just wrote a few lines of Perl for the first time in over a year. (I have a tiny script that, given an unsorted text file, sorts it alphabetically. I needed to sort a list of the form number: thing, but first I needed to add leading zeros to the numbers so it would stop sorting 256 before 13312.)

I am by no stretch of imagination a good programmer. It took me about an hour to figure out how to do this (split, sprintf, join, if you care) and work out the syntax. My recollection is that it then usually takes anywhere from five to thirty minutes to debug the obvious syntax errors and less obvious 'it did what' errors before a script will do what I expect it to.

It ran right the first time.

I am equal parts exuberant and terrified.

Date: 2012-06-18 03:40 am (UTC)
rbandrews: (Default)
From: [personal profile] rbandrews
That's always a cool feeling. :)

It's been... at least seven years since I wrote any Perl. I remember doing it in college but I don't remember exactly when. So seven to nine years.

The last Perl-ish task I had to do was this: take a file that has lines like "6 5/8" and return a file with lines like "6.625". Only it also has to deal with having lines like "8" in it, not every line has a fraction.

Date: 2012-06-18 03:33 pm (UTC)
rbandrews: (Default)
From: [personal profile] rbandrews
I did a regex: try to match it against "\d+ \d+/\d+", if it matches that then divide, otherwise if it matches just "\d+" then use it as given. Same general idea. :)

I'm having to relearn a lot of PHP to contribute to MediaWiki. It's odd.

Edit: Also sad.
Edited Date: 2012-06-18 03:35 pm (UTC)

Date: 2012-06-18 11:59 pm (UTC)
yomikoma: Yomikoma reading (Default)
From: [personal profile] yomikoma
I use perl at work - it's still a viable language and according to my coworkers who just got back from YAPC::NA, there are still a lot of companies hiring perl hackers.

Date: 2012-06-20 12:39 pm (UTC)
yomikoma: Yomikoma reading (Default)
From: [personal profile] yomikoma
Ha ha ha!
Though much of the cool stuff from Perl 6 has been pulled into Perl 5 at this point, so there's some interesting stuff to learn if you're interested.

Date: 2012-06-18 03:07 am (UTC)
From: [identity profile] jameshroberts.livejournal.com
I'm always worried when I have a program run right the first time.

Date: 2012-06-18 04:26 pm (UTC)
mneme: (Default)
From: [personal profile] mneme
Excellent. It's always fab when a program compiles and runs right the first time. Rare, but still awesome.

For your edification [since, well, there's a reason I get paid to write perl, and some of it is a pretty good knowledge of the language], the way I'd do it is a lot simpler.

1. In unix, sort numerically is sort -n.

But with pure perl, I'd probably do this:

#!/usr/bin/perl

use strict;

my @lines = <>;
print map $_->[0], sort { $a->[1] <=> $b->[2] || $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] } map [$_,/^(\d+):(.*)/], @lines;

END
############################################

The double map is unnecessary, but is a "Schwartzian Transform" -- a common perl optimization on complex sorts that makes sure you only do your data extraction once per element rather than n(log n) times per element.

Just to add a number of zeroes (and lead into a different script) I'd do:

#!/usr/bin/perl -p

s/^(\d+):/sprintf("%010d:",$1)/e

END

[replace 10 with however you want to pad]

Date: 2012-06-21 05:15 pm (UTC)
mneme: (Default)
From: [personal profile] mneme
Map is...one of the things that makes a list oriented language cool to work with.

I first got the idea of what makes map a really cool construct when hearing about APL. Now, I've never programmed in APL -- and Perl's map doesn't have the -really- cool feature of APL's vector operations (it doesn't paralellize). But there are still a -whole- lot of operations that are better expressed as a series of list operations than in terms of what they do to each individual item.

When dealing with perl, the array/hash slicing facility (@res = @hash{@keys} or @res = @array[1,2,3,@otherkeys]) are also very key, for similar reasons. I mean, really, what's easier to read (once you understand slicing)?

for($i = 0; $i<@keys, $i++) {
$hash{$keys[$i]} = $values[$i];
}

or

@hash{@keys} = @values;

Combining this with applications of map is an exercise for the reader. :)

Profile

jazzfish: Jazz Fish: beret, sunglasses, saxophone (Default)
Tucker McKinnon

Most Popular Tags

Adventures in Mamboland

"Jazz Fish, a saxophone playing wanderer, finds himself in Mamboland at a critical phase in his life." --Howie Green, on his book Jazz Fish Zen

Yeah. That sounds about right.

Expand Cut Tags

No cut tags