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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:29:10+00:00 2026-06-02T21:29:10+00:00

The program below should take an array and compress it so that there are

  • 0

The program below should take an array and compress it so that there are no repeated products and add up the totals, so:

A B B C D A E F
100 30 50 60 100 50 20 90

Becomes:

A 150
B 80
C 60
D 100
E 20
F 90

The code below runs and works the way I want it to:

#! C:\strawberry\perl\bin
use strict;
use warnings;

my @firstarray = qw(A B B C D A E F);
my @secondarray = qw (100 30 50 60 100 50 20 90); 
my @totalarray; 
my %cleanarray; 
my $i;

# creates the 2d array which holds variables retrieved from a file
@totalarray = ([@firstarray],[@secondarray]);
my $count = $#{$totalarray[0]};

# prints the array for error checking
for ($i = 0;  $i <= $count; $i++) {
    print "\n $i) $totalarray[0][$i]\t $totalarray[1][$i]\n";
}

# fills a hash with products (key) and their related totals (value)
for ($i = 0;  $i <= $count; $i++) {
    $cleanarray{ $totalarray[0][$i] } = $cleanarray{$totalarray[0][$i]} + $totalarray[1][$i];
}

# prints the hash
my $x = 1;
while (my( $k, $v )= each %cleanarray) {
    print "$x) Product: $k Cost: $cleanarray{$k} \n";
    $x++;
}

However before printing the hash it gives me the “Use of uninitialized value in addition (+)” error” six times. Being very new to Perl (this is my first Perl program outside of a text book), can someone tell me why this is happening? It seems like I have initialized everything…

  • 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-02T21:29:14+00:00Added an answer on June 2, 2026 at 9:29 pm

    It gives me compile errors in these lines:

    my @cleanarray;
    

    It is a hash.

    my %cleanarray;
    

    And here:

    $cleanarray{ $totalarray[0][$i] } = $cleanarray{$totalarray[0][$i]} + totalarray[1][$i];
    

    You missed the sigil of totalarray. It is $totalarray[1][$i]

    The undefined message it is because $cleanarray{$totalarray[0][$i]} doesn’t exists. Using the shorter:

    $cleanarray{ $totalarray[0][$i] } += totalarray[1][$i];
    

    will work without warnings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is the program that I wrote. /******************************************************************************* * This program reads EOF from
The program below should read in a bunch of integers from a file and
I have a method that has 2 output parameters. The method should take an
In the example console app below, you will notice that the program is attempting
I'm practising with C, writing simple programs. The little program below should just get
The program below prints: my name is:null my name is:null Someclass static init AFAIK
This program below moves the last (junior) and the penultimate bytes variable i type
(In short: main()'s WaitForSingleObject hangs in the program below). I'm trying to write a
Below program contains two show() functions in parent and child classes, but first show()
In this below program i am printing the contents of the div when i

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.