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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:04:57+00:00 2026-05-27T08:04:57+00:00

I have a hash with the following key/value pair 4 => model1 2 =>

  • 0

I have a hash with the following key/value pair

4 => model1
2 => model2 

I want the following string created from the above hash

4 X model1 , 2 X model2

I tried the following

my %hash,
foreach my $keys (keys %hash) {
    my $string = $string . join(' X ',$keys,$hash{$keys});
}
print $string;

What I get is

4 X model12Xmodel2

How can I accomplish the desired result 4 X model1 , 2 X model2?

  • 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-05-27T08:04:58+00:00Added an answer on May 27, 2026 at 8:04 am

    You could do:

    my %hash = (4 => "model1", 2 => "model2");
    my $str = join(", ", map { "$_ X $hash{$_}" } keys %hash);
    print $str;
    

    Output:

    4 X model1, 2 X model2
    

    How it works:

    map { expr } list evaluates expr for every item in list, and returns the list that contains all the results of these evaluations. Here, "$_ X $hash{$_}" is evaluated for each key of the hash, so the result is a list of key X value strings. The join takes care of putting the commas in between each of these strings.


    Note that your hash is a bit unusual if you’re storing (item,quantity) pairs. It would usually be the other way around:

    my %hash = ("model1" => 4, "model2" => 2);
    my $str = join(", ", map { "$hash{$_} X $_" } keys %hash);
    

    because with your scheme, you can’t store the same quantity for two different items in your hash.

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

Sidebar

Related Questions

I have the following hash: {:charge_payable_response=>{:return=>700, :ns2=>http://ws.myws.com/}} How can I get the value of
Can you please help anyone?. I have a hash value as following. I need
I have a JSON object holding the following value: @value = {val:test,val1:test1,val2:test2} I want
How to display value from Hash onto page I have a hash in my
In rails 3, how to create a Dropdown from hash I have following code
Say I have the following JSON string returned from server: { response:{ imageInstances:{ one:{
I have the following hash of countries; COUNTRIES = { 'Albania' => 'AL', 'Austria'
i have the following class test hash={} def printHash puts hash[1] puts hash[2] puts
According to MSDN , a hash function must have the following properties: If two
In perl, I have a hash that looks like the following: $hash{key1}->{a} = 1;

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.