Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5978825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:34:20+00:00 2026-05-22T21:34:20+00:00

I would like to iterate over a hash on the server-side, and send it

  • 0

I would like to iterate over a hash on the server-side, and send it over to the client in the sorted order using JSON.

My question is:

When I am in my foreach-loop and have the key and complex value (see how my hash looks like at the bottom), how do I insert it in to the JSON string?

Here is how I do that

use JSON;
my $json = JSON->new;
$json = $json->utf8;

...

# use numeric sort
foreach my $key (sort {$a <=> $b} (keys %act)) {

  # somehow insert $key and contents of $act{$key} into JSON here

}

# my $json_string;
# my $data = $json->encode(%h);
# $json_string = to_json($data);

# # return JSON string
# print $cgi->header(-type => "application/json", -charset => "utf-8");
# print $json_string;

print Dumper \%act looks like this

$VAR1 = {
          '127' => {
                     'owners' => [
                                   'm'
                                 ],
                     'users' => [
                                  'hh',
                                  'do'
                                ],
                     'date_end' => '24/05-2011',
                     'title' => 'dfg',
                     'date_begin' => '24/05-2011',
                     'members_groups' => [],
                     'type' => 'individuel'
                   },
          '276' => {
                     ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:34:21+00:00Added an answer on May 22, 2026 at 9:34 pm

    And the JSON builtin sort does not enough?

    see: http://metacpan.org/pod/JSON#sort_by

    Sorting is supported only with JSON:PP (Perl, not XS – AFAIK)

    so:

    use JSON::PP;
    use warnings;
    use strict;
    
    my $data = {
            'aaa' => {
                    a => 1,
                    b => 2,
            },
            'bbb' => {
                    x => 3,
            },
            'a2' => {
                    z => 4,
            }
    };
    
    my $json = JSON::PP->new->allow_nonref;
    #my $js = $json->encode($data); #without sort
    my $js = $json->sort_by(sub { $JSON::PP::a cmp $JSON::PP::b })->encode($data);
    print "$js\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Map[Long, String] which I would like iterate over in descending order
I am using Ruby on Rails 3.0.7 and I would like to iterate over
i would like to iterate over the items of a List<T> , except the
In DataSet.Tables[0].Columns[0] we have a DataType property. Now, I would like to iterate over
I have two arrays of objects. I would like iterate over each of these
I would like to iterate over a Set and remove the elements from the
I would like to iterate over the data rows stored in a Zend_Db_Table_Rowset object
I would like to iterate over the outputs of an unknown function. Unfortunately I
How can I iterate over a wildcard generic? Basically I would like to inline
I would like to iterate over the below and output a string that combines

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.