Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Is it hash??

by dideod.yang (Sexton)
on Jun 26, 2018 at 01:18 UTC ( [id://1217414]=perlquestion: print w/replies, xml ) Need Help??

dideod.yang has asked for the wisdom of the Perl Monks concerning the following question:

I don't know what is "$test -> {'RecordType'} = UNKNOWN;". At first I think that is hash. but it is not hash becuase "print Dumper(\%test)" didn't operate....

My first question is What is "$test -> {'RecordType'} = UNKNOWN;". is it hash? or just define variable?? My second question is about below script. I understand why print "case 1", but I don't understand abour case 2. I think case 2 can't operate becasue ifcondition is false.. can you explain about that??

My last question is What is different between $test -> {'RecordType'} = UNKNOWN; and $test -> {'RecordType'} = "UNKNOWN";. as generally I define some value with "" but that list didn't use "" so I wonder what is different between them.

please help me...

################## perl script ##################### $test -> {'RecordType'} = UNKNOWN; #case 1 if($test ->{'RecordType'}){ print "case1\n"; } #case 2 if($test ->{'RecordType'} = KNOWN){ print "case2\n"; } ###########################################

2018-06-27 Athanasius added paragraph tags and fixed code tags

Replies are listed 'Best First'.
Re: Is it hash??
by AnomalousMonk (Archbishop) on Jun 26, 2018 at 01:43 UTC

    First of all, please use  <code> ... </code> tags as advised in Writeup Formatting Tips and Markup in the Monastery. To fix your OP, please see How do I change/delete my post?

    I don't know what is "$test -> {'RecordType'} = UNKNOWN;".

    $test is a hash (or associative array) reference.
    $test->{'RecordType'} accesses a key of the hash that is referenced.
    $test->{'RecordType'} = UNKNOWN; assigns a value to a key of the hash. Unfortunately, it seems as if the bareword  UNKNOWN is being assigned, which has no value. This error would have been caught if the statement
        use strict;
    had been placed at the beginning of the code.

    c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; use Data::Dumper; ;; my $test; ;; $test->{'RecordType'} = 'UNKNOWN'; ;; print Dumper $test; " $VAR1 = { 'RecordType' => 'UNKNOWN' };

    Please consider these points and we can go forward from here. (Update: Actually, these seem like pretty basic questions, so maybe take a look at perlintro and some of the articles in Getting Started with Perl first.)


    Give a man a fish:  <%-{-{-{-<

Re: Is it hash??
by Cristoforo (Curate) on Jun 26, 2018 at 02:02 UTC
    You might be using constants (here named KNOWN and UNKNOWN). If so, where they are mentioned in your code, they would be unquoted (KNOWN and not "KNOWN"). Here is an example using named constants.
    #!/usr/bin/perl use strict; use warnings; use constant {KNOWN => 1, UNKNOWN => 0}; my $test; $test -> {'RecordType'} = UNKNOWN; #case 1 if($test ->{'RecordType'}){ # if $tset->{'RecordType'} == any 'true' v +alue, i.e 1 print "case1 is known\n"; } else { print "case1 is unknown\n"; } #case 2 if($test ->{'RecordType'} == KNOWN){ # if $tset->{'RecordType'} == 1 print "case2 is known\n"; } else { print "case2 is unknown\n"; }
    The run produces this output:
    case1 is unknown case2 is unknown
    On another point, $test is not a hash but is used as a hash reference. So, it is used as $test->{RecordType} and not $test{RecordType}

    Constants are usually used to make code more clear (names instead of numbers).

    Constants are explained here.

Re: Is it hash??
by dideod.yang (Sexton) on Jun 26, 2018 at 07:44 UTC
    thanks a lot :) I fully understand your experts thanks
Re: Is it hash??
by Anonymous Monk on Jun 26, 2018 at 12:57 UTC
    You may have heard these terms batted about in conversations that you have overheard at the Monastery: arrayref, hashref ... Now you know. These are scalars which contain references to these data structures.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1217414]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-25 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found