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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:27:28+00:00 2026-06-17T13:27:28+00:00

I searched in Google , but I did not find anything useful in it.

  • 0

I searched in Google, but I did not find anything useful in it.

Even though there are many tutorials for Perl, I did not find any tutorial which would mention a hash which has different values for each and every key?
Is such a thing possible in Perl?

For example, can there be a hash like which has 2 keys (a, b) where:

$myhash{"a"}=1;
$myhash{"b"}=[ 'hamnet', 'shakespeare', 'robyn', ];

Is the above possible?


I tried this:

#!/usr/bin/perl

use strict;
my %x;

$x{"a"}="b";
$x{"b"}=['c','d'];

foreach (keys %x)
{
  print $_."\n";
  print "$x{$_}";
}

but it is outputting:

a
bb
ARRAY(0x1ece50)

I am confused about how to access the elements of this hash.

I would like to tell you all that, even though I know Perl a bit, I am a complete novice regarding hashes.


OK, I found one thing — to access the array inside the hash, I need to do:

@{$x{"b"}}

But as I have told you, the value of a hash can be either an array or a scalar value, so for accessing the above hash, I need to initially identify the type of the value and then access it! How can I do this?
That is, how can I identify whether a value for a key is either a scalar, an array or a hash?

  • 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-17T13:27:29+00:00Added an answer on June 17, 2026 at 1:27 pm

    “Even though there are many tutorials for Perl, I did not find any tutorial which would mention a hash which has different values for each and every key? Is such a thing possible in Perl?”

    You might’ve tried looking at the tutorials included in your Perl distribution, which include the Perl Data Structures Cookbook (perldsc). It includes a chapter on “More Elaborate Records”, which gives examples on how to create and used a hash whose values have different types.

    “But as I have told you, the value of a hash can be either an array or a scalar value, so for accessing the above hash, I need to initially identify the type of the value and then access it! How can I do this? That is, how can I identify whether a value for a key is either a scalar, an array or a hash?”

    Since you seem to be a unfamiliar with the use of references and the ref operator, you may also want to take a look at Mark’s very short tutorial about references (perlreftut).

    Note that all of these tutorials (and many more) already come with your Perl distribution, and you can access them by typing e.g.

    perldoc perlreftut
    

    on the command line.
    (On some systems, you may need to install an extra package to enable the perldoc command. This is highly recommended if you intend to do any Perl programming at all.)

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

Sidebar

Related Questions

I have searched Google for a converter but I did not find anything. Is
I searched google and SO, but did not find a solution. I have an
I have searched all over the Google and StackOverFlow, but still did not find
I've searched the iPhone documentation and on google but have not found any information
I searched pretty much everywhere on google, but I didn't find anything useable. I
Hy there I searched a lot on google, but couldn't find an answer. Maybe
I've searched google and SO for a solution to this problem but have not
I have searched n googled everything possible related to this but did not quite
I have searched but i did not get what i wanted. I am developing
I searched on Google and Wikipedia a lot, but I could't find answers for

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.