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 8871337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:58:00+00:00 2026-06-14T17:58:00+00:00

I have a Perl script that returns this hash structure : $VAR1 = {

  • 0

I have a Perl script that returns this hash structure :

$VAR1 = {
          'Week' => [
                      '1238',
                      {
                        'OUT3FA_5' => 65,
                        'OUT3A_5' => 20,
                        'OUT3-Fix' => 45,
                        'IN1' => 85
                      },
                      '1226',
                      {
                        'OUT3FA_5' => 30,
                        'OUT3A_5' => 5,
                        'OUT3-Fix' => 25,
                        'IN1' => 40
                      }
                    ]
        };

What I’d like to do is, count the total of IN1 for each week, per example in this case it’ll return:

$VAR1 = {
          'Week' => [
                      '1238',
                      {
                        'OUT3FA_5' => 65,
                        'Total_IN1' => 85,
                        'OUT3A_5' => 20,
                        'OUT3-Fix' => 45,
                        'IN1' => 85
                      },
                      '1226',
                      {
                        'OUT3FA_5' => 30,
                        'Total_IN1' => 125,
                        'OUT3A_5' => 5,
                        'OUT3-Fix' => 25,
                        'IN1' => 40
                      }
                    ]
        };

And so on for each week.

How can I do this please? Any help would be appreciated.

Here is what I tried to do but it’s not working :

my @sum_IN1 = qw(IN1); #kinda useless to use an array just for one value...
for my $num (keys %hash) {

    my $found;
    my $sum = 0;

    for my $key (@sum_IN1) {

        next unless exists $hash{$num}{$key};
        $sum   += $hash{$num}{$key};
        $found = 1;
    }

    $hash{$num}{Total_IN1} = $sum if $found;
} 
  • 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-14T17:58:02+00:00Added an answer on June 14, 2026 at 5:58 pm

    You need to keep a state variable holding the running total for each item in the array. I also suspect that you Week array is supposed to be a hash?

    use strict;
    use warnings;
    
    use Data::Dump;
    
    my $data = {
      Week => [
        1238,
        { "IN1" => 85, "OUT3-Fix" => 45, "OUT3A_5" => 20, "OUT3FA_5" => 65 },
        1226,
        { "IN1" => 40, "OUT3-Fix" => 25, "OUT3A_5" => 5, "OUT3FA_5" => 30 },
      ],
    };
    
    my $week = $data->{Week};
    
    # Sort the array entry pairs by week number
    #
    my @pairs;
    push @pairs, [ splice @$week, 0, 2 ] while @$week;
    @$week = ();
    for my $pair (sort { $a->[0] <=> $b->[0] } @pairs) {
      push @$week, @$pair;
    }
    
    # Calculate the running totals of IN1
    #
    my $total = 0;
    for my $item (@$week) {
      next unless ref $item eq 'HASH' and exists $item->{IN1};
      $total += $item->{IN1};
      $item->{Total_IN1} = $total;
    }
    
    dd $data;
    

    output

    {
      Week => [
        1226,
        {
          "IN1"       => 40,
          "OUT3-Fix"  => 25,
          "OUT3A_5"   => 5,
          "OUT3FA_5"  => 30,
          "Total_IN1" => 40,
        },
        1238,
        {
          "IN1"       => 85,
          "OUT3-Fix"  => 45,
          "OUT3A_5"   => 20,
          "OUT3FA_5"  => 65,
          "Total_IN1" => 125,
        },
      ],
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a perl script that's reading an INI file like this: [placeholder_title] Hostname
I have a Perl script that takes user input and creates another script that
I have a Perl script that uses WWW::Mechanize to read from a file and
I have a Perl script that takes text values from a MySQL table and
I have a Perl script that launches 2 threads,one for each processor. I need
I have a Perl script that requires a couple of plugins, for istance nmap.
I have a Perl script that will execute three applications. All of it have
On Computer A (my computer), I have a Perl script that accesses a file
I have a perl cgi script that's fairly resource intensive (takes about 2 seconds
I currently have a Perl CGI script that parses incoming XML requests using XML::Simple

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.