#!/usr/bin/perl -w use strict; use warnings; use CGI; use JSON qw (encode_json); my $cgi = CGI->new; my $quoted = quotemeta $cgi->param('term'); my %hash = ( 1 => 'Erna', 2 => 'Erwin', 3 => 'Markus', 4 => 'Heidi Petra', 5 => 'Test Quoting "mit" Anführungszeichen', 6 => 'Test mit Plus r+x', 9 => 'Suchbegriff/Debug: "' . $quoted . '"', ); my @json_array; foreach my $id ( sort {lc $hash{$a} cmp lc $hash{$b}} grep {$hash{$_} =~ m/$quoted/i || $_ == 9} keys %hash ) { push @json_array,{ id => $hash{$id}, label => $hash{$id}, value => $hash{$id}, }; } print STDOUT $cgi->header . JSON->new->latin1->encode(\@json_array);