Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pushing elements onto a hash 1

Status
Not open for further replies.

jimberger

Programmer
Jul 5, 2001
222
GB
Hello,

Is they any way of pushing elements onto a hash. For example push($element,@array).

Thanks

Jim
 
Where is the hash in your equation?

Do you mean, can a single hash-key point to an array of values?

$hotdog_station{condiments} = ['mustard','ketchup','relish'];

Or if you have an array of these items:

$hotdog_station{condiments} = [@array_of_condiments];

...

Or do you mean how do you add elements to a hash... in which case the answer is simple: just asign it.

$hash_name{new_key} = "new value";

Or do you mean: how do you push elements onto an anonymous array reference available as a hash-value.

push @{$hotdog_station{condiments}}, "chili";

Please clarify.

--jim
 
i want to assign values to a hash dymnically...

I have a loop....

foreach $x (@somearray){

## i want to push elements onto my hash
## if i do %hash = ('$hotdog','$x);
## the value %hash will be overwritten each ## time

## i want to use
push (%hash{$hotdog},$x);
$hotdog = $hotdog + 1;

##but this dosent work...
}

my hash will then look like this %hash ('1','somearray1',
'2','somearray2')

etc..

thanks for your help

jim
 
An example of how to push elements:
Code:
my @rules = (
"optional:ketchup",
"requires:mustard",
"requires:onion",
"optional:salt",
"requires:bread",
"optional:pepper" );

foreach( @rules ) {
    ( $rule, $value ) = split /:/;
    push @{ $hotdog{$rule} }, $value;
}

foreach $rule ( sort keys %hotdog ) {
    print "$rule -> @{ $hotdog{$rule} }\n";
}
Returns:
Code:
optional -> ketchup salt pepper
requires -> mustard onion bread
Cheers, Neil :)
 
You "push" elements onto an *array* - not onto a hash.

Here's the output of 'perldoc -f push':

push ARRAY,LIST
Treats ARRAY as a stack, and pushes the values of
LIST onto the end of ARRAY. The length of ARRAY
increases by the length of LIST. Has the same
effect as

for $value (LIST) {
$ARRAY[++$#ARRAY] = $value;
}

but is more efficient. Returns the new number of
elements in the array.


Seems like you may not have a firm understanding of what a hash is versus what an array is. My suggestion is to read up on arrays and hashes - the "Learning Perl" book by Schwartz & Christiansen is great for that. Or you can do

perldoc perldata

to read the perldocs on Perl's data structures - you'll find good info in there for arrays and hashes.

HTH. Hardy Merrill
 
Dude. I'm still really unclear on exactly what you are desiring.

You have an array of values.

You want to populate a hash with those values.

Correct?

Question: An array is simply a list of values. A hash is like two lists, where one list is the hash-keys, and the other list is the hash-values.

How do you want your array to map into the hash? Are you basically just trying to root out double entries in an array? If that is the case, then the array-value will be the hash-key, and it will point to a count of occurences.

Or is the array-value going to become the hash value, and something else is the key... like a digit. So you could say:

print $hash{1}; # would show "mustard"

Maybe this is what you really want:

for(@array){
$hash{$_}++;
}

This will gather all the unique values in your array and make them hash-keys pointing to a numerical value representing how many times they occured in the array.
So if

@array = qw/jim jim jim neil hardy jim/;
for(@array){
$hash{$_}++;
}

print "$_ => $hash{$_}" for keys %hash;


That code snippet would output:

jim => 4
neil => 1
hardy => 1

Or some variation there of. The 'order' is not gauranteed.

??????

--jim



 
Another example of having a hash key point to an array:
Code:
my @sample_data = (
"boys:ted:bill:fred",
"girls:jane:amy",
"cats:tom:tigger",
"dogs:spot:buster",
);

foreach( @sample_data ) {
 my( $key, @values ) = split /:/;
 $names{ $key } = \@values;
}

foreach( sort keys %names ) {
  local $" = ' and ';
  print "$_ => [ @{ $names{$_} } ]\n";
}
Returns:
Code:
boys => [ ted and bill and fred ]
cats => [ tom and tigger ]
dogs => [ spot and buster ]
girls => [ jane and amy ]
Cheers, Neil :)
 
You don't "push" elements onto a hash, you simply create them:
Code:
$hash{'firstname'} = "Tracy";
$hash{'lastname'} = "Dryden";
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Apologies if that was not obvious from my code ;-) I am pushing to an (anonymous) array, stored as an reference in the value component of the hash...
 
Thanks all for your responses,

Coderifous - that was exactly what i needed! thanks

bascially the code was to tokenize a document and count the occurances of a word...which your code works fine on - cheers mate!

jim

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top