Skip to content

Instantly share code, notes, and snippets.

@belden
Created May 29, 2015 18:32
Show Gist options
  • Save belden/f1cf4db89270cd43daad to your computer and use it in GitHub Desktop.
Save belden/f1cf4db89270cd43daad to your computer and use it in GitHub Desktop.
Shows how to correctly de-sparsify a Perl array for storage into a postgresql 'array' data type
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my @sparse;
$sparse[0] = 'start';
$sparse[3] = undef;
$sparse[5] = 'end';
my @storeable1 = map { $sparse[$_] } grep { defined $sparse[$_] } 0..$#sparse;
my @storeable2 = map { $sparse[$_] } grep { exists $sparse[$_] } 0..$#sparse;
print Dumper +{
with_defined => \@storeable1,
with_exists => \@storeable2,
};
__END__
$VAR1 = {
'with_defined' => [
'start',
'end'
],
'with_exists' => [
'start',
undef,
'end'
]
};
@belden
Copy link
Author

belden commented May 29, 2015

The bits to know here are:

  1. If you try to store @sparse into a Postgresql ARRAY column, you'll get an exception (potentially a core dump, it's been a few years since I've done this): the sparse array doesn't get serialized from Perl -> Postgresql in a way that's friendly to Postgresql.
  2. If you use defined to de-sparsify your array, then you'll throw away information: undef values will get pulled out, so won't turn into NULL values at the correct relative position within the Postgresql ARRAY.

So use exists when desparsifying your arrays instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment