0

I am new to Perl. I am trying to identify how to print the size of hash of array. Below are snippets of my code.

my %map = ();    // Initialization

while ($line = <>) {
    chomp($line);
    // Logic to split the $line
    push( @{$map{$first_var}}, $rest );       
}

// Print the amp
foreach my $value (sort keys %map) {
    print "$value: @{$map{$value}}\n";
}

On printing I get output in below format

valA: (num1 num2 num3 num4)
valb: (num2 num4)
valC: (num1 num3 num4)

I wish to identify the how many elements are associated with each key (valA, valB,valc).

I tried: print "Number of nodes in the facility : scalar @{$map{$facility1}}\n";

However it gives out as

valA: scalar (num1 num2 num3 num4)
valb: scalar (num2 num4)
valC: scalar (num1 num3 num4)

I wish the output to show 4, 2, 3 for valA, valB,valC respectively. I maybe misunderstanding but scalar provides length of array.

Thanks

3
  • 1
    When you're asking a question on Stack Overflow, you should always run the exact code in your question and make sure that any output matches up. Commented Mar 3, 2016 at 0:43
  • 1
    // isn't a comment in perl. Your code cannot work. Commented Mar 3, 2016 at 13:08
  • Sorry about the //. I added it in Stackoverflow editor so as to make code readability better. Commented Mar 7, 2016 at 18:02

2 Answers 2

2

You need to change your print statement to:

print "Number of nodes in the facility : " . scalar(@{$map{$facility}}) . "\n";
Sign up to request clarification or add additional context in comments.

Comments

2

Function calls and expressions are not interpolated within double-quoted strings*, so you'd need to do one of the following:

  • Break out of string context (e.g. concatenation)
  • Store the result in a variable before printing and use the variable instead
  • Use printf

Here's an illustration of each technique:

use strict;
use warnings;

my %hash = (
    foo => [1, 2, 3],
    bar => [qw(a b c d e f g)],
    baz => [0 .. 99],
);

for my $key (keys(%hash)) {
    # concatenation
    print "Number of elements for $key: " . @{$hash{$key}} . "\n";

    # temporary variable
    my $count = @{$hash{$key}};
    print "Number of elements for $key: $count\n";

    # printf
    printf("Number of elements for $key: %d\n", scalar(@{$hash{$key}}));
}

Results:

Number of elements for bar: 7
Number of elements for bar: 7
Number of elements for bar: 7
Number of elements for baz: 100
Number of elements for baz: 100
Number of elements for baz: 100
Number of elements for foo: 3
Number of elements for foo: 3
Number of elements for foo: 3

*There's a hack that you can use to force this, but it'll severely hinder the readability of your code, and it might cause future maintainers of your code to wish ill upon you:

print "Number of elements for $key: ${\(scalar(@{$hash{$key}}))}\n";

Or the equally bad equivalent using an array reference:

print "Number of elements for $key: @{[scalar(@{$hash{$key}})]}\n";

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.