Need some basic hash help.

They have: 62 posts

Joined: May 2000

OK, I basically need the hash equivalent of the array push function. So, in other words, a way to add a hash pair to the end of a hash.

They have: 161 posts

Joined: Dec 1999

You need to read the 'perldata' documentation. A good place to start is http://www.perldoc.com/.

There is no "beginning" or "end" of a hash -- at least, not one that you are thinking of, or have control over. Hashes return their data in an order that is determined by Perl's source code, not you.

That being said, there is no push() equivalent for hashes. Just create a new key-value pair:

my %hash = (
  EFnet => 'irc.core.com',
  DALnet => 'liberty.nj.us.dal.net',
);
$hash{EFnet} = 'irc.prison.net';  # changing existing value
$hash{Undernet} = 'us.undernet.org';  # adding new pair
'

They have: 62 posts

Joined: May 2000

This is sorta off topic, but don't you have to '' those hash keys Japhy? Or am I wrong again?

Your answer does make sense, I guess I just didn't think out of the box. I mean, it does seem logical that hashes would have a push, doesn't it?

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

why?
you don't access them by numbers (usually, I guess you could give them # names...), so how would perl know what to name the next 'element'?

They have: 161 posts

Joined: Dec 1999

If you read the 'perldata' documentation, you'll see:

an identifier within such curlies is forced to be a string,
as is any single identifier within a hash subscript. Our
earlier example, $days{'Feb'} can be written as $days{Feb}
and the quotes will be assumed automatically. But anything
more complicated in the subscript will be interpreted as an
expression.

[...]

It is often more readable to use the => operator between
key/value pairs. The => operator is mostly just a more
visually distinctive synonym for a comma, but it also
arranges for its left-hand operand to be interpreted as a
string--if it's a bareword that would be a legal identifier.
This makes it nice for initializing hashes:

  %map = (
    red   => 0x00f,
    blue  => 0x0f0,
    green => 0xf00,
  );
'

They have: 850 posts

Joined: Jul 1999

I wrote a few examples that will hopefully help you better understand hashes.

<strong>example 1</strong>
my %person_vidcard;

$person_vidcard{Frank} = 'Nvidia TNT 2';
$person_vidcard{Alex} = 'Nvidia TNT';
$person_vidcard{Xena} = 'Nvidia Geforce 2';
$person_vidcard{Guy} = 'Voodoo 3';

#Voodoo 3's suck, so we will delete Guy from the list
delete $person_vidcard{Guy};
foreach(sort keys %person_vidcard)
{
print "$_ has a $person_vidcard{$_}\n";
}

output:
Alex has a Nvidia TNT
Frank has a Nvidia TNT 2
Xena has a Nvidia Geforce 2

<strong>example 2</strong>
my %name_age = (
Donna => 45,
Sammy => 32,
Mike  => 22
);
#It is Mike's birthday.  If he is found in the hash
#1 more year will be added to his age
if(defined $name_age{Mike})
{
print "Mike is ".++$name_age{Mike};
}

output:
Mike is 23
'

They have: 161 posts

Joined: Dec 1999

Technically, you should be using exists() to see if a key-value pair is found in a hash:

%hash = ( foo => undef );
if ($hash{foo}) { print 1 }          # not shown
if (defined $hash{foo}) { print 2 }  # still not shown
if (exists $hash{foo}) { print 3 }   # shown
if ($hash{bar}) { print 4 }          # not shown
if (defined $hash{bar}) { print 5 }  # still not shown
if (exists $hash{bar}) { print 6 }   # still not shown
'

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

thanks japhy, I never knew about those functions... very usefull.

They have: 62 posts

Joined: May 2000

I'm not a total hash/Perl novice, I just sometimes miss obvious stuff.

And the Perl manpages, while a good source of data, I feel that they are VERY confusing and hard to understand. That's why I prefer to ask the people hear about stuff.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.