Help needed with if..exists in hash in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed with if..exists in hash in perl
# 1  
Old 05-29-2012
Help needed with if..exists in hash in perl

Hi,

So I have a hash,

Code:
%users = ("abc1" => "John Doe",
                 "xyz2" => "Mary Jane");

and a variable
Code:
my $who_user = `who am i|awk '{ print \$1F }'`;

How do I use exists to check if the variable value is present in the hash

Code:
if (exists($users{"$who_user"}))

is not giving the desired output.



Thanks!

Last edited by neil.k; 05-29-2012 at 04:32 AM..
# 2  
Old 05-29-2012
Code:
my $who_user = `whoami`

# 3  
Old 05-29-2012
@balajesuri not sure what you are trying to imply.

The variable who_user will essentially hold a value which I need to check against the keys in the hash user. Whether it's whoami or what I have defined is immaterial to my question.

And, I defined whoami as such, because I wanted to get the login user. Vanilla whoami will not give that, once you su as root or any other user.

Thanks!
# 4  
Old 05-29-2012
Code:
%users = ("abc1" => "John Doe",
                 "xyz2" => "Mary Jane");
open (WH,'whoami |');
my $who_user= <WH>;
chomp($who_user);
print $users{$who_user},"\n" if (exists($users{"$who_user"}));

# 5  
Old 05-29-2012
Pure Perl:
Code:
my $user = getlogin;

or
Code:
my $user = $ENV{"USER"};

# 6  
Old 05-29-2012
@balajesuri I am not sure if you understood my question. Thanks anyways.

The reason my if was not working is because, I had an extra space maybe in the output of my unix command. I used chomp , and it worked like a charm.
# 7  
Old 05-29-2012
@niel.k: I understood your question. My question is, why not use a pure perl functionality to get the username like "getlogin" or "$ENV{"user"}" instead of invoking a shell command like "who am i" ???

That way you won't have to invoke a shell, get the value and then chomp it. I hope I'm able to convey what I intend to.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 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 help

Hi , i have the below code its working fine when i execute in unix , but its not working in windows could you pls explain me where i am going wrong. This is the program $data = { '1' => 'one' , '2' => 'two' , 3 => 'three' }; print "hello : $data->{'1'}... (2 Replies)
Discussion started by: ragilla
2 Replies

4. 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

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 hash

Hi i am reading one file and creating Hash from the contents of it my issue is there are 3 different files in 3 different locations having same structure so for parsing these files i have one subroutine which returns hash after reading all the 3 files i need to create consolidated hash from three... (2 Replies)
Discussion started by: zedex
2 Replies

7. Shell Programming and Scripting

Perl Hash if exists

print $hash{$value} if exists $hash{$key}; would only print my top value, and not the one I want to "match".... should i not be using if exists? how would you recommend "searching" a hash file... thanks! (6 Replies)
Discussion started by: yesokay
6 Replies

8. Shell Programming and Scripting

Perl Hash

hi i have two hash achi %disk1,%disk2 with( key, value) (key1,value1) How to store it in another hash.. Plz replyyy. Regards Hari (1 Reply)
Discussion started by: Harikrishna
1 Replies

9. Shell Programming and Scripting

Perl Hash

HI I have a hash like this $hashname->{$filesystem}->{'fsname'}=$filesystem; How to get the values from this multilevel hash. Thanks in advance... :) (1 Reply)
Discussion started by: Harikrishna
1 Replies

10. Shell Programming and Scripting

Hash in perl

Hi Help me with some good links of Hash with in Hash .(Multidimensional hash).. Regards Harikrishna (1 Reply)
Discussion started by: Harikrishna
1 Replies
Login or Register to Ask a Question