Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re^4: How to Map a scalar to a key that is an array? by 1nickt
in thread How to Map a scalar to a key that is an array? by symgryph

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-03-28 17:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found