Print perl hash value in foreach loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print perl hash value in foreach loop
# 1  
Old 05-08-2016
Print perl hash value in foreach loop

Experts - Any advice on how to get a hash value in a foreach loop?
Values print correctly on standalone print statements, but I can't access value
in foreach loop.
See sample code below and thanks in advance.

Code:
foreach my $z (sort keys %hash) {
  for $y (@{$hash{$z}}) {
    print "$z ->";			# Prints key1
    print "$y ->";			# Prints Value1
    print $hash1{"$y"},"\n";		# Prints NULL <-- Why is this NULL??
  }
}

print $hash1{"Value1"};			# Prints HashValue <-- I want this value in foreach

# 2  
Old 05-08-2016
Quote:
Originally Posted by timj123
Experts - Any advice on how to get a hash value in a foreach loop?
Values print correctly on standalone print statements, but I can't access value
in foreach loop.
See sample code below and thanks in advance.

Code:
foreach my $z (sort keys %hash) {
  for $y (@{$hash{$z}}) {
    print "$z ->";			# Prints key1
    print "$y ->";			# Prints Value1
    print $hash1{"$y"},"\n";		# Prints NULL <-- Why is this NULL??
  }
}

print $hash1{"Value1"};			# Prints HashValue <-- I want this value in foreach

Could you provide the output of the following?

Code:
use Data::Dumper;

# After the hash is populated.
print Dumper \%hash;
print Dumper \%hash1;

# 3  
Old 05-08-2016
It's similar to the following:

Code:
          'Key1' => [
                      ' Value1',
                      ' Value2',
                      ' Value3',
                      ' Value4',
                      ' Value5',
                      ' Value6',
                      ' Value7',
                      ' Value8',
                      ' Value9'
                    ],

---------- Post updated at 04:56 PM ---------- Previous update was at 04:43 PM ----------

Missed %hash1, see below and thanks!

Code:
$VAR1 = {
          'Value1' => 'HashValue1',
          'Value2' => 'HashValue2',
          'Value3 => 'HashValue3',
          'Value4' => 'HashValue4',
          'Value5' => 'HashValue5',
          'Value6' => 'HashValue6',
          'Value7' => 'HashValue7',
          'Value8' => 'HashValue8',
          'Value9' => 'HashValue9'
                 };

# 4  
Old 05-08-2016
Quote:
Originally Posted by timj123
It's similar to the following:

Code:
          'Key1' => [
                      ' Value1',
                      ' Value2',
                      ' Value3',
                      ' Value4',
                      ' Value5',
                      ' Value6',
                      ' Value7',
                      ' Value8',
                      ' Value9'
                    ],

Notice that 'space Value1' is not the same that 'Value1'. Is that a copy and paste from your output?
If that is the case, $hash1{$y} or $hash1{"$y"} is undefined, since it would be equivalent to $hash1{' Value1'} and the key dereference should be $hash1{'Value1'}

Last edited by Aia; 05-08-2016 at 07:13 PM.. Reason: Add explanation
# 5  
Old 05-08-2016
It's not exact copy/paste, it's similar data. Not that my data is top secret, I just can't post the actual online.

Actual is more along the lines:

Code:
$VAR1 = {
               'Key1' => [
                              ' Value-1',
                              ' Value-2',
                              ' Value-3',
                              ' Value-4',
                             ],

The values do really have a "space" in front. I didn't see that until you requested the dumper output.

they also have a "-" in them as well. Not sure if that is causing the issue.

But when doing a standalone print of:
Code:
print $hash1{"Value-1"};

Seems to work fine.
# 6  
Old 05-08-2016
Quote:
Originally Posted by timj123
It's not exact copy/paste, it's similar data. Not that my data is top secret, I just can't post the actual online.

Actual is more along the lines:

Code:
$VAR1 = {
               'Key1' => [
                              ' Value-1',
                              ' Value-2',
                              ' Value-3',
                              ' Value-4',
                             ],

The values do really have a "space" in front. I didn't see that until you requested the dumper output.

they also have a "-" in them as well. Not sure if that is causing the issue.

But when doing a standalone print of:
Code:
print $hash1{"Value-1"};

Seems to work fine.
The '-' is not a problem since the key is a string; what is a problem is that ' Value-1' is not the same that 'Value-1', since the the space in front counts as part of the key.

There is no $hash1{' Value-1'}, but there is a $hash1{'Value-1'}
Correct what it would become the keys, in the array, in %hash

Last edited by Aia; 05-08-2016 at 07:25 PM.. Reason: Add correction
This User Gave Thanks to Aia For This Post:
# 7  
Old 05-08-2016
Thanks Aia!
I was stumped, big time. I appreciate your time looking into this for me.
I also wanted to tell you that I learn a lot from your posts on this forum.
This User Gave Thanks to timj123 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to print hash of hash in table format

Hi, I have a hash of hash where it has name, activities and count i have data like this - $result->{$name}->{$activities} = $value; content of that are - name - robert tom cat peter activities - running, eating, sleeping , drinking, work i need to print output as below ... (3 Replies)
Discussion started by: asak
3 Replies

2. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

3. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

4. Programming

PERL, search and replace inside foreach loop

Hello All, Im a Hardware engineer, I have written this script to automate my job. I got stuck in the following location. CODE: .. .. ... foreach $key(keys %arr_hash) { my ($loc,$ind,$add) = split /,/, $arr_hash{$key}; &create_verilog($key, $loc, $ind ,$add); } sub create_verilog{... (2 Replies)
Discussion started by: riyasnr007
2 Replies

5. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

6. Shell Programming and Scripting

Perl question - print last element in a hash?

I am modifying someone else's code. There is a foreach statement printing the contents of a hash. Can someone give me an example of printing the last element in a hash? The output currently is A B C D E I want the output to be E (1 Reply)
Discussion started by: streetfighter2
1 Replies

7. UNIX for Advanced & Expert Users

Perl loop txt and check if a hash key

Hi, The task i have to do is to 1- create a database contains the Names .run the query and store results in hash make the Name field is the hash key 2- in the same time i have a txt which i will loop through it word by word and check for each word if a hash key ( compare it with the Names in... (0 Replies)
Discussion started by: eng_shimaa
0 Replies

8. UNIX for Dummies Questions & Answers

Foreach loop to run a perl script on multiple files

Hi, I have thousands of files in a directory that have the following 2 formats: 289620178.aln 289620179.aln 289620180.aln 289620183.aln 289620184.aln 289620185.aln 289620186.aln 289620187.aln 289620188.aln 289620189.aln 289620190.aln 289620192.aln.... and: alnCDS_1.fasta (1 Reply)
Discussion started by: greptastic
1 Replies

9. Shell Programming and Scripting

Print Entire hash list (hash of hashes)

I have a script with dynamic hash of hashes , and I want to print the entire hash (with all other hashes). Itried to do it recursively by checking if the current key is a hash and if yes call the current function again with refference to the sub hash. Most of the printing seems to be OK but in... (1 Reply)
Discussion started by: Alalush
1 Replies

10. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies
Login or Register to Ask a Question