![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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.
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.
no subject
Date: 2012-06-18 03:40 am (UTC)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.
no subject
Date: 2012-06-18 03:30 pm (UTC)Ha, and having just dealt with split and join that seems trivial. (Split on space, and if there's anything in the second half, split on /, divide, and add.)
no subject
Date: 2012-06-18 03:33 pm (UTC)I'm having to relearn a lot of PHP to contribute to MediaWiki. It's odd.
Edit: Also sad.
no subject
Date: 2012-06-18 11:59 pm (UTC)no subject
Date: 2012-06-20 03:56 am (UTC)I started as a software tester in 2001 and had some free time at one point. "I could learn Perl," I thought. So I did some poking around and decided to maybe check back in a year or two and learn Perl 6 when they'd finished with it. How long could it take, really?
no subject
Date: 2012-06-20 12:39 pm (UTC)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.
no subject
Date: 2012-06-18 03:07 am (UTC)no subject
Date: 2012-06-18 03:31 pm (UTC)no subject
Date: 2012-06-18 04:26 pm (UTC)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]
no subject
Date: 2012-06-21 04:45 pm (UTC)*looks it up*
Okay, that's pretty bloody useful. Will have to remember it.
no subject
Date: 2012-06-21 05:15 pm (UTC)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. :)