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

Hi. I need to create a hash(I think atleast) that would point to multiple items. Example
Hash($ArtistSong1)->[1,3,5] Hash($ArtistSong2)->[2,4]
Is this possible? ($ArtistSong, and 1,2,3, etc will be derived from the DB). If it is, can someone please tell me how do I go above doing this? My final goal is, I will be searching based on ArtistSong, if a certain value exists there or not? so Example I may say, for ArtistSong1, does 1 exist? If yes, do something, else do something else. I do not know how many ArtistSongs will I have nor do I know how many values will it refer to.

Thanks soo much for you help.

Newcomer,
PJofNJ

20050324 Edit by ysth: code tags

Replies are listed 'Best First'.
Re: Hash pointing to multiple items
by RazorbladeBidet (Friar) on Mar 23, 2005 at 18:50 UTC
    You want perllol perlref. Your syntax is (close to )correct, just use code tags (preview is your friend ;) )
    # this is assuming that "ArtistSong1" is a string $Hash{ArtistSong1} = [ 1, 3, 5]
    Update: Added code comment
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
Re: Hash pointing to multiple items
by atcroft (Abbot) on Mar 23, 2005 at 19:03 UTC

    Try a hash of arrays. For example,

    my (%data); while (<DATA>) { my ($submitter, $ticket_number) = split(/\|/); push(@{$data{$submitter}}, $ticket_number); } print "Submitters, by ticket count:\n"; foreach my $sub (sort { scalar $#{$data{$a}} <=> $#{$data{$b}} } keys %data) { print $sub, ": ", scalar $#{$data{$sub}}, "\n"; } print "\n", "Submitters and tickets, ordered by submitter:\n"; foreach my $sub (sort keys %data) { print $sub, ": ", join(", ", @{$data{$sub}}), "\n"; } __DATA__ Joe|1 Joe|2 Gary|3 Gary|4 Kelly|5 Joe|6 Joe|7

    The above sample would give you something along the lines of:

    Submitters, by ticket count: Kelly: 1 Gary: 2 Joe: 4 Submitters and tickets, ordered by submitter: Gary: 3, 4 Kelly: 5 Joe: 1, 2, 6, 7

    See also perldsc, for more information on data structures in perl. Hope that helps.

    Update: 23 Mar 2005 - Fixed typo in code sample; added note regarding perldsc.

      Thanks.. That did the trick.. You're awsome..
Re: Hash pointing to multiple items
by inq123 (Sexton) on Mar 23, 2005 at 20:04 UTC
    I thought for your purpose a hash of hashes might be better? For example:

    my %hash = ( artistsong1 => { 1 => 1, 3 => 1, 5 => 1 }, artistsong2 => { 2 => 1, 4 => 1 } ); # to find for ArtistSong1, does 1 exist? if($hash{artistsong1}->{1}) { print "yes, 1 does exist for artistsong1!\n"; }
    If in fact, your set for song1, song2, song3 are always mutually exclusive, then it might make sense to reverse the data structure to:

    my %hash = ( 1 => artistsong1, 3 => artistsong1, 5 => artistsong1, 2 => artistsong2, 4 => artistsong2, 6 => artistsong3, 7 => artistsong3 ); # to find where 1 is? my $artistsong = $hash{1};
    Or of course you could also make hashes of hashes of hashes like $hash{$artist}->{$song}->{1}) && print "yes, for $artist, $song, there's 1!\n";
Re: Hash pointing to multiple items
by successor1221955 (Acolyte) on Mar 23, 2005 at 19:52 UTC
    Litterally you can't have a hash point to multiple values. The previous monks are correct in that you can point to an anonymous array of values which brings up the whole subject of nested data structures in Perl. A simplier approach would be to do the following:
    $Hash{$ArtistSong1} = '1,3,5'; $Hash{$ArtistSong2} = '2,4';
    Not being exactly sure about the meaning of your code, I assume your hash variable is %Hash and that $ArtistSong1 and $ArtistSong2 are keys to your hash. The above code turns your "multiple entries" into a single string entry which you can latter decode thus:
    my @songnumbers = split ',',$Hash{$ArtistSong1};
    Please let me know more about what your trying to do and I can help you find the data structure you need. be a humble successor
      For the sake of TIMTOWTDI .. ok. But we don't want to teach bad habits do we?


      holli, /regexed monk/