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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:48:43+00:00 2026-06-13T02:48:43+00:00

I have the question in Perl:Input 5 mobile set details (Invoice number, Model ,

  • 0

I have the question in Perl:Input 5 mobile set details (Invoice number, Model , Company, Memory , Price , Quantity ). Print all the details, total amount (quantity * price) , total quantity , number of mobiles from each company.

My script is:

#!/usr/bin/perl

my %mobset = (
    '1' => {
        'Invoice No' =>'3456',
        'Model'      =>'S9900',
        'Company'    =>'Sonyericcson',
        'Memory'     =>'1GB',
        'Price'      =>'8000',
        'Qnty'       =>'1',
    },
    '2' => {
        'Invoice No' => '3457',
        'Model'      => 'S3322',
        'Company'    => 'Samsung',
        'Memory'     => '2GB',
        'Price'      => '9000',
        'Qnty'       => '2',
    },
    '3' => {
        'Invoice No' => '3458',
        'Model'      => 'N4140',
        'Company'    => 'Nokia',
        'Memory'     => '512MB',
        'Price'      => '5000',
        'Qnty'       => '2',
    },
    '4' => {
        'Invoice No' => '3459',
        'Model'      => 'S4636',
        'Company'    => 'Samsung',
        'Memory'     => '256MB',
        'Price'      => '6000',
        'Qnty'       => '1',
    }, 
    '5' => {
        'Invoice No' => '3460',
        'Model'      => 'S7854',
        'Company'    => 'Samsung',
        'Memory'     => '128MB',
        'Price'      => '7000',
        'Qnty'       => '1',
    }
);

print "All the mobile set details are as follows:\n";
foreach my $id(sort keys %mobset) {

    print "Mobile SlNo. = $id, Invoice No. = $mobset{$id}{'Invoice No'}, Model No. = $mobset{$id}{'Model'}, CompanyName = $mobset{$id}{'Company'}, Memory = $mobset{$id}{'Memory'}, Price = $mobset{$id}{'Price'}, Quantity = $mobset{$id}{'Qnty'}\n";

    $totqty += $mobset{$id}{'Qnty'};
    $totprice += $mobset{$id}{'Price'};
}

print "Total Quantity of mobile set is $totqty\n";
$totamt = $totqty * $totprice;
print "Total Amount of mobile set is Rs.$totamt\n";

print "The company names of mobile are:\n";
foreach my $id(sort keys %mobset) {

    print "$mobset{$id}{'Company'}\n"; 
}

$name = SonyericcsonSamsungNokiaSamsungSamsung;
my @names = ($name =~ m/([A-Z][a-z]+)/g);
join(',',@names);
my %count;

foreach (@names) {

    if (exists $count{$_}) {

        $count{$_}++;
    } 
    else {

        $count{$_} = 1;
    }
}

print "The number of mobiles from each company are:\n";
foreach (keys %count)  {

    print "$_ \t = $count{$_}\n";
}

My output is:

All the mobile set details are as follows:
Mobile SlNo. = 1, Invoice No. = 3456, Model No. = S9900, CompanyName = Sonyericcson, Memory = 1GB, Price = 8000, Quantity = 1
Mobile SlNo. = 2, Invoice No. = 3457, Model No. = S3322, CompanyName = Samsung, Memory = 2GB, Price = 9000, Quantity = 2
Mobile SlNo. = 3, Invoice No. = 3458, Model No. = N4140, CompanyName = Nokia, Memory = 512MB, Price = 5000, Quantity = 2
Mobile SlNo. = 4, Invoice No. = 3459, Model No. = S4636, CompanyName = Samsung, Memory = 256MB, Price = 6000, Quantity = 1
Mobile SlNo. = 5, Invoice No. = 3460, Model No. = S7854, CompanyName = Samsung, Memory = 128MB, Price = 7000, Quantity = 1
Total Quantity of mobile set is 7
Total Amount of mobile set is Rs.245000
The company names of mobile are:
Sonyericcson
Samsung
Nokia
Samsung
Samsung
The number of mobiles from each company are:
Sonyericcson = 1
Nokia = 1
Samsung = 3

But I am getting the o/p for number of mobiles because I did hardcode the mobile names which I am not supposed to. How can I solve the code?

  • 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-13T02:48:44+00:00Added an answer on June 13, 2026 at 2:48 am

    There is really no need for the hard code. You know the name – its in the hasref you are walking. Just build up a count array based on the Company value

    my %count;
    foreach my $id (sort keys %mobset) {
        ++$count{$mobset{$id}->{Company}};
    }
    
    print "The number of mobiles from each company are:\n";
    foreach (keys %count)  {
    
        print "$_ \t = $count{$_}\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about Perl from the script below. If the input for
I have written a Perl script for the following bioinformatics question, but unfortunately there
I have the question in Perl:Read a series of last names and phone numbers
I have a Perl question like this: Write a Perl program that will read
This may be a basic question. I have written a small perl script to
Apologies for a simple question, but I'm very new to Perl! I have an
Hey all, I have a question about perl objects and threading. My program is
I have just started learning Perl scripting language and have a question. In Perl,
I have a question with the following Code: #!/usr/bin/perl use strict; use warnings; my
I have a perl script that prepares files for input to a binary program

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.