0

Is there a Perl equivalent to the following Python code?

names['name'] = ['Bob', 'Bill', 'Terry']
obj = {'Students': [ {'Names': name} for name in names['name']]}

This'll end up looking like something like this

EDIT: As Tim Roberts pointed out: The result would look like this

[ {'Names': "Bob" }, { "Names": "Bill" },. { "Names": "Terry" } ]

I've seen Perl one liners that do different things, but not this.

3
  • 1
    Your Python does something significantly different to the desired result. Commented Aug 2, 2021 at 16:36
  • Hang on a minute, that's a useful result, but that's NOT what your code would produce. You would produce a whole list dicts, each with one element: [ {'Names': "Bob" }, { "Names": "Bill" },. { "Names": "Terry" } ] Commented Aug 2, 2021 at 16:37
  • Please show complete input, working code and expected output, along with your translation attempt. Where are you stuck? If you're working in Python, please label things correctly. obj and arr aren't what you think they are. I recommend arr => dct and obj => result_dct or something like that. Commented Aug 2, 2021 at 16:40

2 Answers 2

2

You want a map inside of your data structure. It's not a one-liner. It's just normal code.

my $obj = {
  'Students' => [
    map { 'Names' => $_ }, @{ $names{name} }
  ]
};

I have used the expression form map (map EXPR, LIST) with a comma (,) after the expression that is being evaluated. The curly braces ({}) are the hash reference constructor. Alternatively, you could use the block form (map BLOCK LIST), which would have an extra pair of curlies.

    map { { 'Names' => $_  } } @{ $names{name} }

If you want a plain hash rather than a reference, substitute like this.

my %obj = (
# ...
);
Sign up to request clarification or add additional context in comments.

10 Comments

Note that arr would be a hash in Perl, as it's associative -- just as it is in Python as a dict.
Not sure if the OP will care about the distinction, but there's also my %obj = ( ... ) instead of my $obj = { ... }, depending on whether you want a hash or a hash reference.
$arr{name} is a scalar. For a proper LIST to pass to map, use @{$arr{name}}.
"Note that arr would be a hash in Perl" : The question was probably edited and arr is now longer mentioned.. so this answer is confusing
Changed arr to names to match changes in OP
|
1

Here is another approach for the same:

use Data::Dumper;
my @names = ('Bob', 'Bill', 'Terry');

my %out_dict;

my @students_dict = ();
foreach my $name (@names) {
    push(@students_dict, {'Names' => $name});
}

$out_dict{'Students'} = \@students_dict;
print Dumper \%out_dict;

Output:

$VAR1 = {
          'Students' => [
                          {
                            'Names' => 'Bob'
                          },
                          {
                            'Names' => 'Bill'
                          },
                          {
                            'Names' => 'Terry'
                          }
                        ]
        };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.