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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:37:04+00:00 2026-06-14T20:37:04+00:00

My hash looks like this : $VAR1 = { ‘1238’ => { ‘OUT3FA_5’ =>

  • 0

My hash looks like this :

$VAR1 = {
      '1238' => {
                  'OUT3FA_5' => 65,
                  'SEV' => '3_major',
                  'OUT3A_5' => 20,
                  'OUT3-Fix' => 45,
                  'IN1' => 85
                },
      '1226' => {
                  'OUT3FA_5' => 30,
                  'SEV' => '4_minor',
                  'OUT3A_5' => 5,
                  'OUT3-Fix' => 25,
                  'IN1' => 40
                },
      '1239' => {
                  'OUT3FA_5' => 56,
                  'SEV' => '4_minor',
                  'OUT3A_5' => 34,
                  'OUT3-Fix' => 22,
                  'IN1' => 94
                }]

I want my Perl script to only return the values corresponding to “SEV” = “4_minor” In this case it’ll return :

    '1226' => {
                  'OUT3FA_5' => 30,
                  'SEV' => '4_minor',
                  'OUT3A_5' => 5,
                  'OUT3-Fix' => 25,
                  'IN1' => 40
                },
      '1239' => {
                  'OUT3FA_5' => 56,
                  'SEV' => '4_minor',
                  'OUT3A_5' => 34,
                  'OUT3-Fix' => 22,
                  'IN1' => 94
                }

But when I use grep like this :

my $Sev_Logged = "4_minor";
my $node_hash = {
Week => [
  grep {  $hash{$_}{'SEV'} eq $Sev_Logged } %hash,
   ]
    };

# Print in json format
my $json = encode_json \%$node_hash;
print $json;

It only returns :

{"Week":["1226","1239"]}

And I want it to return all the other data corresponding to each of the weeks found containing SEV=”4_minor”.
How can I do this?
Any help would be much appreciated.

  • 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-14T20:37:05+00:00Added an answer on June 14, 2026 at 8:37 pm

    When you hand a %hash over that way to grep then Perl interpretes the hash as an even-numbered list of keys and values — resulting in the grep {...} block being called for once for each key and once for each value (instead of being called for each pair of key&value).

    Try something like this:

    { map { ($_ => $hash{$_} } grep { $hash{$_}->{SEV} eq $Sev_Logged } keys %hash }
    

    The grep keeps those hash keys for which the SEV field matches what you want. The map afterwards reconstructs a hash (because at that moment we only have the keys returned from grep), and finally we convert it to a hash reference.

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

Sidebar

Related Questions

I have an Array of Hashes in a Hash that looks like this: $VAR1
I have loop and each iteration of Hash looks like this: [1, {:clid=>1, :nvz=>4,
I have a hash that looks like this h1 = {4c09a0da6071a593f051de32=>[4c09a0da6071a593f051de32, Cafe Bistro, 37.78458803130115,
I have @hash that looks like this: [1, {:clid=>1, :nvz=>4, :tip=>IP, :name=>Mark, :record=>some text}]
I have a hash that looks like this: { bmw => { id =>
I have a hash which looks like this: items: item: attribute_a: cheese attribute_b: bacon
How do I get 1 from a hash that looks like this? {{1=>2} =>
I'm trying to hash a file using SHA1. The result looks like this: B7-DB-B9-93-E7-2F-6F-EB-6D-CD-CC-A8-DE-D2-F1-01-6E-8A-53-BC
I have a Ruby hash that looks like this: h = {side1=>[green, Green], side2=>[verde,
I have a nested hash table that looks like this: my %myhash = (

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.