Line # Revision Author
1 260 ahitrov <%once>
2
3 use JSON::XS;
4 my $json = JSON::XS->new;
5
6 </%once>
7 <%args>
8
9 $name => undef
10 $SETS => undef
11
12 </%args>
13 <%init>
14
15 return undef if (! ref($SETS));
16 return undef if (! $name);
17
18 my $FIELDS = {};
19 while (my ($element,$value) = each %{$SETS}) {
20 if ( $element =~ /^${name}_area_(\d+)$/ ) {
21 my $index = $1;
22 $value =~ s/^\s+//s;
23 $value =~ s/\s+$//s;
24 if ( defined $value && $value =~ /^\d+$/ ) {
25 $FIELDS->{$index}{area} = $value;
26 $FIELDS->{$index}{index} = $index;
27 }
28 } elsif ( $element =~ /^${name}_cost_(\d+)$/ ) {
29 my $index = $1;
30 $value =~ s/^\s+//s;
31 $value =~ s/\s+$//s;
32 if ( defined $value && ($value =~ /^\d+$/ || $value =~ /^\d+[\.\,]\d+$/) ) {
33 $value =~ s/\,/\./;
34 $FIELDS->{$index}{cost} = $value;
35 $FIELDS->{$index}{index} = $index;
36 }
37 }
38 }
39 my %FIELDS = map { $_->{area} => $_->{cost} } sort { int($a->{index}) <=> int($b->{index}) } grep { $_->{area} && defined $_->{cost} } values %$FIELDS;
40 return $json->encode(\%FIELDS);
41
42 </%init>