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

  • Home
  • SEARCH
  • 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 8479313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:59:17+00:00 2026-06-10T18:59:17+00:00

If I have a multiline array and it contains for example the following integers

  • 0

If I have a multiline array and it contains for example the following integers with different numbers of integers per line:

1 1 1 2 1 1
2 1 4 1
2 1 1 3 1 6

How do I convert it to an array of counts in perl with zeros for keys with no counts? I can do it for the single array case using map, but I’m struggling with this multiline case/array of array case.

#For line 1, 1 => 5, 2 => 1
#For line 2, 1 => 2, 2 => 1, 4 => 1
#For line 3, 1 => 3, 2 => 1, 3 => 1, 6 => 1

So the result looks like this:

5 1 0 0 0 0
2 1 0 1 0 0
3 1 1 0 0 1

Thanks!

  • 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-10T18:59:18+00:00Added an answer on June 10, 2026 at 6:59 pm

    This seems to do what is required. It does the obvious thing of building an array of hashes of counts, then finds the maximum value amongst the keys of the hashes for building a display

    use strict;
    use warnings;
    
    use feature 'say';
    
    my @data;
    
    while (<DATA>) {
      next unless /\S/;
      my %counts;
      $counts{$_}++ for split;
      push @data, \%counts;
    }
    
    my $max = 0;
    for (map keys %$_, @data) {
      $max = $_ unless $max >= $_;
    }
    
    for my $row (@data) {
      say join ' ', map $row->{$_} // 0, 1 .. $max;
    }
    
    __DATA__
    
    1 1 1 2 1 1
    2 1 4 1
    2 1 1 3 1 6
    

    output

    5 1 0 0 0 0
    2 1 0 1 0 0
    3 1 1 0 0 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our coding standard at work requires the following array initialisations to have spaces in
I have a multiline textbox that contains very long text in it. How can
If I have a sorted array, how do I find the sequential numbers? Btw,
I have a multiline textbox which shouldn't accept alphabets, numbers, newline(enter key) and backspace.
I have a multiline TextBox for which any of the following inputs are valid:
I have a multiline string which is delimited by a set of different delimiters:
Is there a way to have multiline strings in VB.NET like Python a =
I have a multiline TextView in Android. It is a child of a LinearLayout.
I have a Multiline Textbox . I donot want to let user type HTML
I have a multiline text box, and I'd like to display the user's current

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.