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 7739161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:27:09+00:00 2026-06-01T08:27:09+00:00

Given an arbitrarily nested data structure, how can I create a new data structure

  • 0

Given an arbitrarily nested data structure, how can I create a new data structure so that all the elements in it have been standardized by applying a function on all the elements depending on the type of the element. For example, I might have

$data = {
    name => 'some one',
    date => '2010-10-10 12:23:45',
    sale => [34, 22, 65],
    cust => {
        name => 'Jimmy',
        addr => '1 Foobar Way',
        amnt => 452.024,
        item => ['books', 'pens', 'post-it notes']
    }
}

and I want to convert all text values to upper case, all dates to UTC date times, find the square of all integers, round down all real numbers and add 1, and so on. So, in effect, I want to apply a different function to each element depending on the type of element.

In reality the data might arrive via a database query, in which case they are already a Perl data structure, or they might start life as a JSON object, in which case I can use JSON::from_json to convert it to a Perl data structure. The idea is to standardize all the values in the data structure based on the value type, and then spit out the Perl data structure back again as a JSON object.

I read the answers to executing a function on every element of a data structure and feel that Data::Rmap might do the trick, but can’t figure out how. Seems like Rmap works on all the keys as well, not just the values.

  • 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-06-01T08:27:10+00:00Added an answer on June 1, 2026 at 8:27 am

    It’s crazy straightforward with Data::Rmap you mentioned.

    use Data::Rmap qw( rmap );
    rmap { $_ = transform($_); } $data;
    

    Regarding the question in the comments:

    use Data::Rmap   qw( rmap );
    use Scalar::Util qw( looks_like_number );
    
    # Transforms $_ in place.
    sub transform {
       if (looks_like_number($_)) {
          if (...) {
             $_ *= 2;
          }
    
          $_ = 0+$_;  # Makes it look like a number to JSON::XS
       } else {
          ...
       }
    }
    
    &rmap(\&transform, $data);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with some binary data that I have stored in arbitrarily long arrays
We're using a framework which creates arbitrarily nested HTML data elements identified by the
Given a package, how can I automatically find all its sub-packages?
Given an arbitrary file descriptor, can I make it blocking if it is non-blocking?
Given some arbitrary SQL I would like to get the data types of the
Given four (x,y) pairs that represent the four corners of an arbitrary polygon (quadrilateral)
In mysql, I have a tree that is represented using the adjacency list model.
If I arbitrarily set the id of a new entity to 1000 , without
Given a valid, arbitrary xmlNodePtr, I would like the string representation of that node,
In a given file if I have, struct A { static int a; };

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.