CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
"exists" if you just have hash reference?

 



perlFun
Novice

Jun 19, 2013, 10:29 AM

Post #1 of 12 (632 views)
"exists" if you just have hash reference? Can't Post

say I have a key $key and a hash reference $hashRef (I got $hashRef from a subroutine which returns a reference to a hash). Now say I want to check that $key is indeed a key for the hash referenced by $hashRef. if I do the following:

my $hashRef = stupid_sub();#hashRef is a reference to a hash

if(exists $hashRef{$key}){
...do stuff
}

I get the error:

Global symbol "%hashRef" requires explicit package name at ... (etc)


Now if I do

my %hashRef = stupid_sub();

this compiles fine. But stupid_sub is a subroutine which only returns a reference to a hash so I think this shouldn't work in reality. Is there a way to, in that same line, actually get the hash that's being referenced? so I don't have to do:

my $hashRef = stupid_sub();
my %hash = %$hashRef;
if(exists $hash{$key}} {
... stuff
}


thanks again for all the help


(This post was edited by perlFun on Jun 19, 2013, 10:30 AM)


Laurent_R
Veteran / Moderator

Jun 19, 2013, 10:46 AM

Post #2 of 12 (630 views)
Re: [perlFun] "exists" if you just have hash reference? [In reply to] Can't Post


Code
my %hashRef = stupid_sub();


That is wrong, a hashref is a scalar not a hash.

If I understand your problem, to access the data in the hash refered to by the hashref, you can write something like:


Code
print $hashref->{$key};

Code



      
    


perlFun
Novice

Jun 19, 2013, 10:51 AM

Post #3 of 12 (628 views)
Re: [Laurent_R] "exists" if you just have hash reference? [In reply to] Can't Post

Sorry I think I asked the question wrong. I understand that my %hashRef = stupid_sub() is incorrect. I was just saying, if I write that line of code, perl says it compiles correctly, so my assumption is that the reason I'm getting the original error is because it won't let me use exists on a reference to a hash.


I was wondering how I could condense the following code:

my $hashRef = stupid_sub();
my %myHash = %$hashRef;

in to one line, as in, can I kind of automatically dereference the hash reference I'm getting? Or alternatively, how could I use "exists" on a hash reference? Can I dereference it inline? (as in, I want to use exists (somehow dereference $hashRef to get the actual hash){$key}


(This post was edited by perlFun on Jun 19, 2013, 10:53 AM)


FishMonger
Veteran / Moderator

Jun 19, 2013, 11:10 AM

Post #4 of 12 (625 views)
Re: [perlFun] "exists" if you just have hash reference? [In reply to] Can't Post


Code
my %hash = %{ stupid_sub() };



(This post was edited by FishMonger on Jun 19, 2013, 11:10 AM)


perlFun
Novice

Jun 19, 2013, 11:34 AM

Post #5 of 12 (620 views)
Re: [FishMonger] "exists" if you just have hash reference? [In reply to] Can't Post

Thank you so much! In general, if I have an expression that will be a reference, can I do this where I put curly brackets around it to dereference? (or I'm assuming arrays use hard brackets?) Like say the following hash %hash contains references to arrays. Could I do something like

my @arr = @[ $hash->{$aKey} ]


(This post was edited by perlFun on Jun 19, 2013, 11:36 AM)


Laurent_R
Veteran / Moderator

Jun 19, 2013, 12:00 PM

Post #6 of 12 (615 views)
Re: [perlFun] "exists" if you just have hash reference? [In reply to] Can't Post

For constructing an arrayref, you would use square brackets, as shown in this example under the Perl debugger:


Code
  DB<10> $arrayref = [jan, feb, mar]; 

DB<11> x $arrayref
0 ARRAY(0x80359eb8)
0 'jan'
1 'feb'
2 'mar'



FishMonger
Veteran / Moderator

Jun 19, 2013, 12:08 PM

Post #7 of 12 (613 views)
Re: [perlFun] "exists" if you just have hash reference? [In reply to] Can't Post

To dereference you use the curly braces.


perlFun
Novice

Jun 19, 2013, 12:12 PM

Post #8 of 12 (612 views)
Re: [Laurent_R] "exists" if you just have hash reference? [In reply to] Can't Post

I apologize for not asking the questions well enough :/ I have no problems getting the array refs, hash refs, etc. In fact that's the issue, I have to use the subroutines and they always just return references. my issues are say, inline, getting the info I need from the reference. For example, I'm having yet another issue. I need the following:


my ($Node, $params) = @_; #$params is because the user will be passing in a reference to a hash

later I need to see if a key value in the hash referenced by $params matches something else but if I go


if( $curr_Params{$param} ne $params->{$param} ) {
...stuff
}

($curr_Params is an actual hash). But if I go $ %{$params} -> {$param} I also get an error there. I don't know how to dereference this inline


FishMonger
Veteran / Moderator

Jun 19, 2013, 12:26 PM

Post #9 of 12 (610 views)
Re: [perlFun] "exists" if you just have hash reference? [In reply to] Can't Post

If $params is a hash ref and $param is a scalar who's value is a key in $params, then you'd dereference that hash value with this:

Code
$params->{$param}


If that's not what you're needing/asking, then please provide more details.

A short but complete script demonstrating your problem would help us to understand what you need and be able to provide you with a proper answer.


perlFun
Novice

Jun 19, 2013, 12:43 PM

Post #10 of 12 (607 views)
Re: [FishMonger] "exists" if you just have hash reference? [In reply to] Can't Post

no that's exactly what I'm asking. That's what I had in my code and it was giving error. But I just came back to my desk and my connection had reset and I started a new putty, restored the swp file, and now when I compile it says syntax ok. so i'm not sure what the issue was I'm so confused as to why this error was there. But at least I know that does indeed work!


BillKSmith
Veteran

Jun 19, 2013, 12:45 PM

Post #11 of 12 (606 views)
Re: [perlFun] "exists" if you just have hash reference? [In reply to] Can't Post

Is this what you are looking for?

Code
use strict; 
use warnings;
my $hashref = {a=>1, b=>2};
print "a = $hashref->{a}\n" if exists $hashref->{a};
print "There is no key 'c'\n" if !exists $hashref->{c};
/code]

OUTPUT:

Code
a = 1 
There is no key 'c'

Good Luck,
Bill


Laurent_R
Veteran / Moderator

Jun 19, 2013, 2:15 PM

Post #12 of 12 (599 views)
Re: [perlFun] "exists" if you just have hash reference? [In reply to] Can't Post

I do not know if this helps clarify things, but I just add a similar example under the Perl debugger using an anonymous arrayref:


Code
  DB<1> $c = "the quick brown fox jumps over the lazy dog."; 

DB<2> print [ split / /, $c ]->[3];
fox


But you don't really need an arrayref here, an anonymous array can also work:


Code
  DB<4> print (( split / /, $c )[4]); 
jumps


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives