mvkarthik has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am pretty a newbie to PERL. I was going through a code snippet and this is how it looks like: push @{$hashCellID{$lv}} , $cellID; $hashCellname{$cellname}++; What does this mean ? There are no declarations as to what hashCellID is previously. Thanks, -Karthik Hi, Thanks for posting replies and helping me out. I am failing to understand that hashes always are declared as % and in my code, there is no mention of this valriable like %hashCellID / %hashCellname. Also, I have code like $hashSmsCnt{$lv}++. I dont know how a scalar variable can have index. Thanks, -Karthik

Replies are listed 'Best First'.
Re: Not getting this assignment ...
by Marshall (Canon) on Aug 03, 2010 at 02:20 UTC
    There are 2 hash tables:
    %hashCellID;
    %hashCellname;

    Each key ($lv) of the hashCellID hash table points to an array of cellID's. Presumably as new cellID's are seen for an lv, they are pushed onto the array for that lv.
    push (@{$hashCellID{$lv}} , $cellID);

    At the end of the day, each lv in the hashCellID table will correspond to an array of cellID's of unknown length. And this array can be printed like: print "@{$hashCellID{$lv}}\n";

    To find out the keys of the hashCellID hash:

    foreach my $lv (keys %hashCellID) { print "$lv = @{$hashCellID{$lv}}\n"; }
    The hashCellname just counts how often a particular cellname has occurred.
    $hashCellname{$cellname}++;
    This is basically a histogram count of cellnames.
    foreach my $cell (keys %hashCellname) { print "$cell count = $hashCellname{$cell}\n"; }
Re: Not getting this assignment ...
by toolic (Bishop) on Aug 03, 2010 at 00:56 UTC
Re: Not getting this assignment ...
by biohisham (Priest) on Aug 03, 2010 at 07:07 UTC
    You have to understand that a hash is an associative array, why associative? because each one of its indices (keys) is made to link to a value, the key is embraced within {} and the values can be assigned to a key in many different ways. Since a hash is a type of an array (arrays are essentially lists ) then list functions like reverse or sort can be performed on a hash (their behavior varies between arrays and hashes), however, a hash has its own set of functions as well, like each, keys,values. Read more about hashes at http://perldoc.perl.org/search.html?q=hash.

    What's in a hash value?, a value can be as simple as a number or a string or a single scalar, or can progressively take an advanced shape, in that case the entire hash represents an advanced data structure.

    If you have an array of 3 elements, you can access the second element directly by giving its position (index), this is called subscripting..

    @array = (0,1,2); print $array[1]; ^
    The same thing can be done to a hash, you want to access a given value, you give its position (key):
    my %hash = (fruit=>banana, veg=>'cucumber'); print $hash{fruit}; ^
    Why $ precedes a hash or an array when we're subscripting?, because in Perl, a single subscript is a single scalar so it makes sense that it is treated as such, perl gives you a warning in case you tried print @array[1]; and tells you that it is better written as print $array[1];

    Data structures can be an amalgam of arrays, hashes, scalars so that one hash key can link to a value that is a hash of different keys and these different keys in the linked-to hash can themselves again be linking to an array for instance. The flexibility is that you can define the data structure yourself and build up on it.

    Check the Tutorials for:

    While this list is not exhaustive, you can refer to Reviews->Book Reviews and avail of the Super Search utility.. Another rich resource is the Perl Documentation, which you can also access from your terminal as well by typing %perldoc at your command prompt and from there you can learn how to make use of the documentation that is packaged with your version of Perl.

    While I am just aiming to show you -in context to your question- the different types of resources that you can always refer to I have to include the Comprehensive Perl Archive Network (CPAN), this is a source of Perl modules, libraries and many other useful code.

    Hang in there and have a nice Perl journey :) ...


    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .
Re: Not getting this assignment ...
by cdarke (Prior) on Aug 03, 2010 at 12:43 UTC
    there is no mention of this valriable like %hashCellID / %hashCellname

    Perl, by default, will create variables as you use them:
    $cellname = 'fred'; $hashCellname{$cellname}++; local $, = ' '; print %hashCellname,"\n";
    will create %hashCellname and gives:
    fred 1


    However, this can cause problems - your confusion is a good example! Fortunately there is a fix, to always
    use strict; use warnings;
    Now the hash has to be explicitly created:
    use strict; use warnings; my %hashCellname; my $cellname = 'fred'; $hashCellname{$cellname}++; local $, = ' '; print %hashCellname,"\n";
    Now it is clear where %hashCellname came from.

    I have code like $hashSmsCnt{$lv}++.

    The prefix character (sigil) $ here does not mean that hashSmsCnt is a scalar, it means that the key inside { } is in scalar context. It is the braces { } which indicate that hashSmsCnt is a hash, not the first character (square brackets indicate an array). If you had a list of keys inside { } then the first character would be @, but hashSmsCnt would still be a hash.