sort_hash —
| Attribute | Pos. | Req. | Default | Description |
|---|---|---|---|---|
| interpolate | 0 | interpolate output? | ||
| hide | 0 | Hide the tag return value? |
Interchange 5.7.0:
Source: lib/Vend/Interpolate.pm
Lines: 3095
sub tag_sort_hash {
my($opts, $list) = (@_);
$opts =~ s/^\s+//;
$opts =~ s/\s+$//;
#::logDebug("tag_sort_hash: opts=$opts list=" . uneval($list));
my @codes;
my $key = 'code';
my ($start, $end, $num);
my $glob_opt = 'none';
my @opts = split /\s+/, $opts;
my @option; my @bases; my @fields;
for(@opts) {
if(/^(\w+)(:([flnr]+))?$/) {
$key = $1;
$glob_opt = $3 || 'none';
next;
}
if(/^([-=+])(\d+)-?(\d*)/) {
my $op = $1;
if ($op eq '-') { $start = $2 }
elsif ($op eq '+') { $num = $2 }
elsif ($op eq '=') {
$start = $2;
$end = ($3 || undef);
}
next;
}
my ($base, $fld, $opt) = split /:/, $_;
push @bases, $base;
push @fields, $fld;
push @option, (defined $Vend::Interpolate::Sort_field{$opt} ? $opt : 'none');
}
if(defined $end) {
$num = 1 + $end - $start;
$num = undef if $num < 1;
}
if (! defined $list->[0]->{$key}) {
logError("sort key '$key' not defined in list. Skipping sort.");
return $list;
}
my $i;
my $routine = 'sub { ';
for( $i = 0; $i < @bases; $i++) {
$routine .= '&{$Vend::Interpolate::Sort_field{"' .
$option[$i] .
'"}}(' . "\n";
$routine .= "tag_data('$bases[$i]','$fields[$i]', \$_[0]->{$key}),\n";
$routine .= "tag_data('$bases[$i]','$fields[$i]', \$_[1]->{$key}) ) or ";
}
$routine .= qq!0 or &{\$Vend::Interpolate::Sort_field{"$glob_opt"}}!;
$routine .= '($a->{$key},$_[1]->{$key}); }';
#::logDebug("tag_sort_hash routine: $routine\n");
my $code = eval $routine;
die "Bad sort routine\n" if $@;
#Prime the sort? Prevent variable suicide??
#&{$Vend::Interpolate::Sort_field{'n'}}('31', '30');
use locale;
if($::Scratch->{mv_locale}) {
POSIX::setlocale(POSIX::LC_COLLATE(),
$::Scratch->{mv_locale});
}
@codes = sort {&$code($a,$b)} @$list;
if($start > 1) {
splice(@codes, 0, $start - 1);
}
if(defined $num) {
splice(@codes, $num);
}
#::logDebug("tag_sort_hash routine returns: " . uneval(\@codes));
return \@codes;
}