![[icon]](http://www.inksome.com/userpic/18188/2) |
Makes you like your eggs on the Jersey side
|
| That the Perl operator <=> is informally referred to as "the spaceship operator." Neat! | comments: Leave a comment  |
| | Tags: | code, dynamic methods, perl | | Subject: | [CODE] Nifty Perl tricks: calling a superclass method dynamically | | Time: | 11:42 am |
|
| I was going to have to write a bunch of copy/pasted methods for this work thing, and I didn't wanna. With this, I can do it somewhat minimally.
( code behind the cut )
ETA: Even more minimally! I can define a bunch of methods from a qw//. | comments: Leave a comment  |
| | Tags: | code, geeky, perl | | Subject: | [CODE] There's a clever answer to this | | Time: | 05:45 pm |
|
| I want to break a set of 1000 or more array elements into smaller arrays of up to 1000 elements each. There's some clever way to do this in a for loop with a modulus operator, but I just can't manage to figure it out. So here's how I'm doing it, in Perl:
# @array is the array I am passed, assume it has more than 999 elements.
my $count = scalar @array; # number of array elements I got
my $size = 1000; # maximum size of subarray
my @subarrays;
for (0..($count/$size - 1)) {
my $start = $_ * $size;
my $end = $start + $size - 1;
push @subarrays, [@array[($start..$end)]];
}
my $start = $count/1000;
my $end = $count;
push @subarrays, [@array[($start..$end)]];
I'm sure one of you can post a more elegant solution. :) | comments: Leave a comment  |
| | Tags: | code, geeky, perl | | Subject: | [GEEKY] Dusting off my Perl-fu | | Time: | 10:11 am |
|
| The central issue of this little bit of code is that i'm building a hash with keys from the first line of a text file. That part wasn't bad, though I would of course appreciate any tips for doing it faster. The real kicker is in pulling the data from the file into that hash. When I knew the keys beforehand, I could just go:
# $_ here is one non-header line of the file.
# $separator is the record separator
($hash->{foo}, $hash->{bar}, $hash->{baz}) = split /$separator/;
But since I don't know the keys beforehand, I have to do this:
# $_ here is one non-header line of the file.
# @keys are the keys of the hash, pulled from the first line for the file.
# $separator is the record separator
my @split = split /$separator/;
my $i = 0;
for (@split) {
$hash->{$keys[$i]} = $split[$i];
}
And that seems a bit inelegant and overly verbose. Is there any way to do it more elegantly?
( actual code behind the cut ) | comments: Leave a comment  |
| | Tags: | code, geeky, perl | | Subject: | [CODE] Replacing things delimited by brackets | | Time: | 09:09 am |
|
| I got really tired really quickly of writing out the hyperlink for the documentation on a given table when writing my reading the database posts on Scribblit. So I decided to throw some Perl at it, as I gave up on doing it in XSL. It took a couple tries, but here is the current version of the Perl code.
( code behind the cut, WS ) | comments: Leave a comment  |
| In Java, I can do this:
public interface foo {
public void bar();
public class Factory {
public newInstance() {
return new Foo() {
public void bar() {
System.out.println("bar");
}
}
}
}
}
I.E. I can declare an interface and attach a static Factory class that spawns new anonymous classes that match that interface. Depending on conditions, I could spawn several different anonymous classes specially optimized for those conditions.
In Perl, I can't do that. There's no such thing as an interface (in Java terms) in Perl. If I wanted a single-class solutions, I would have to write the class to detect conditions on initialization and make the methods behave accordingly.
My question: is this a good thing, a bad thing, or indifferent? Discuss. | comments: Leave a comment  |
| I had it in my head that with PHP5, objects were implicitly passed by reference. contrast this with PHP4, in which objects were implicitly passed by silent copy (I don't know how deep, though). This code demonstrates that objects are still implicitly passed by copy, and that annoys me.
*sigh* After having done a lot of OO Perl for the past month and a half, coming back to PHP OOP is a bit of a letdown.
( PHP code behind the cut ) | comments: Leave a comment  |
| | Tags: | code, dimension, perl | | Subject: | [code] A little something I tossed off | | Time: | 04:09 pm | | Current Mood: | geeky |
|
| A bit of code for finding the dimension of an array or a hash. I define "dimension" here as the minimum number of coordinates required to get to a data value. In this case, all the data values are integers, because it makes things easy. :)
It's recursive, because that was easy too. :)
One more thing - it only considers the first value of the supplied reference. So if the array/hash contains a mix of references and data values at any given depth, it will return a value that's not strictly accurate.
So come one, come all, pick apart my code and show me where I can do it better. I don't mind. :) Extra points for making it consider (quickly) every element in the array/hash ref - you could even define a fractional dimension that reflects the number of "leaves" (if you think of it as some sort of tree structure) at at one depth versus the number at another depth. *ponder* That would be extra neato if that fractional dimension generalized to any number of different depths.
EDIT: Cleaned up code for easier display on LJ.
( code behind the cut, WS ) | comments: Leave a comment  |
| | Tags: | code, geeky, perl | | Subject: | [CODE] Quick-and-dirty bit of code for the wife | | Time: | 01:32 pm |
|
| She's got an audiobook of Bridge To Terabithia that she wants to put on her MP3 player. It's on 4 CDs, and it ended up getting ripped into 1 directory for each disc. Each track was numbered with respect to the disc, rather than the whole thing. hlynna can explain better. :)
Anyway, here's the code I used to rename things a bit: ( Perl code behind the cut ) | comments: Leave a comment  |
| If I perform an UPDATE query on an InnoDB table in MySQL 5.0, is it guaranteed that the next time I read data from the table, the updated data shows up in the affected rows? If not, how do I make sure that this happens?
I'm having a problem at work where even though I've done an UPDATE to a certain table via a Perl script, and the Perl script has (I think) exited before I'm reading it from PHP, the updated data are not read by the PHP application. When I go in and look using the MySQL command-line client, however, the data are there. I'd rather not have to tell the user, "If you don't see any data, hit the reload button." It's just tacky. | comments: Leave a comment  |
![[icon]](http://www.inksome.com/userpic/18188/2) |
Makes you like your eggs on the Jersey side
|
|