in reply to How to Map a scalar to a key that is an array?

This is a classic "XY Problem." (Google It.™) You have a problem. But you've already decided now that you need to solve that problem by "Map," which of course requires a "key" which of course can't be "an array" until you (the rabbit-hole journey begins here ...) "concoct a way to do that," etc. The problem being that "the rabbit-hole problem" now becomes the focus of your attention instead of your original problem. "Therefore, STEP BACK!" "THERE'S MORE THAN ONE WAY TO DO IT!™"
  • Comment on Re: How to Map a scalar to a key that is an array?

Replies are listed 'Best First'.
Re^2: How to Map a scalar to a key that is an array?
by jdporter (Paladin) on Aug 01, 2019 at 15:48 UTC
    Google it.

    Or you could even search for it here: XY Problem

    I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.
      I did. Its an interesting read. As a FYI for anyone else having this question, my final program looked Thus:
      #!/usr/bin/perl #use strict; #use warnings; #use Data::Dumper; my %data; { local $/ = ''; while (<>) { chomp; my ($key, $data) = split /\n/; $data{$key} = $data; } } #print %data; my $key; # my $data; foreach $key (keys %data) { # print "$key\n"; print "$key,$data{$key}\n"; }
      "Two Wheels good, Four wheels bad."
        my %data = do { local $/ = ''; map { chomp; split /\n/, $_, 2 } <> };
        my %data = map /^(.*?)\n(.*)\n/s, do { local $/ = ''; <> };
        #use strict; #use warnings;
        Why do you comment out these two lines? It is usually a very bad idea.

        Otherwise, the more idiomatic way to write your foreach loop would be to declare $key within the loop:

        foreach my $key (keys %data) { print "$key,$data{$key}\n"; }

        Hi,

        Using the data from your OP, the above code prints:

        arn:aws:iam::12345678901:role/Role2-Role2,"Alexa for Business" arn:aws:iam::11111111111:role/ADFS-MyRoleName,"Alexa for Business"
        Is that really what you wanted? Your original post stated

        "I would like to create a hashmap so I can count the # of items under each of the 'arn'"

        • Your split() call is throwing away everything but the first line in each section. If you want all remaining lines, use the third argument of split: my ($key, $data) = split(/\n/, $line, 1).
        • Calling your results hash by the same name you call the value to be added to it, is unnecessary complication of your life, even if Perl can handle it.

        If what you want is what you said you want, I would use:

        use strict; use warnings; use feature 'say'; use List::Util 'max'; my %count; local $/ = "arn:aws:iam::"; while (my $line = <DATA>) { chomp $line; $line =~ s!$/!! if $. == 1; next if $line eq ''; my ($key, @values) = split(/\n/, $line); $count{$key} = scalar @values; } my @keys = keys %count; my $longest = max map { length } @keys; for my $key (sort @keys) { say sprintf("%-${longest}s : %3d", $key, $count{$key}); } __DATA__ arn:aws:iam::11111111111:role/ADFS-MyRoleName "Alexa for Business" "AWS Certificate Manager" "AWS Certificate Manager Private Certificate Authority" "AWS Amplify" "Manage - Amazon API Gateway" "AWS App Mesh" "Amazon AppStream 2.0" "AWS AppSync" "Amazon Athena" "AWS Auto Scaling" arn:aws:iam::12345678901:role/Role2-Role2 "Alexa for Business" "AWS Certificate Manager" "AWS Certificate Manager Private Certificate Authority" "AWS Amplify" "Manage - Amazon API Gateway" "Application Auto Scaling" "AWS App Mesh" "Amazon AppStream 2.0" "AWS AppSync"
        Output:
        11111111111:role/ADFS-MyRoleName : 10 12345678901:role/Role2-Role2 : 9

        Hope this helps!


        The way forward always starts with a minimal test.
Re^2: How to Map a scalar to a key that is an array?
by karlgoethebier (Abbot) on Aug 01, 2019 at 10:13 UTC

    Caveat lector. See also.

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re^2: How to Map a scalar to a key that is an array?
by symgryph (Sexton) on Aug 01, 2019 at 13:55 UTC
    This is an excellent point. What I actually wanted to do was 'count' the # of items that are assigned to the 'key' value. I have been burned by not being specific enough in the past, so I was trying to frame in a way that showed I had put some thought in the question.
Re^2: How to Map a scalar to a key that is an array?
by 1nickt (Canon) on Aug 02, 2019 at 13:48 UTC

    You could learn how to use map instead of trying to warn others away from it all the time because you don't understand it ...


    The way forward always starts with a minimal test.