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

Well Monsigneurs,
I thought I had it but my results are not what I expected. This sub does a SELECT in the mySQL db and comes creates a hash with the day (of the month) as key and the title (of an event) as value. Thing is that some days have multiple events so I wanted to concatenate the additional titles onto the end of the existing value. The code below is where I am now. It gives me 1 title, and not the one I was getting so I am triggering the else conditional but no matter what I do (including $events{ $ref->{$day} } = undef or ="boogers" ) seem to have no effect. Can some one point out what is going wrong and how I can concatenate the values of identical keys?
TIA
jg
sub month_events { $sth = $dbh->prepare("SELECT * FROM hno_calendar WHERE year = $yea +r AND month = $month"); $sth->execute(); while (my $ref = $sth->fetchrow_hashref()) { if ( !exists $events{ $ref->{day} } ) { $events{ $ref->{day} } = $ref->{title}; } else { $events{ $ref->{$day} } .=$ref->{title}; } } }
_____________________________________________________
Think a race on a horse on a ball with a fish! TG

Replies are listed 'Best First'.
Re: Concatenating Identical Key's Values
by Kanji (Parson) on Apr 04, 2002 at 04:39 UTC

    If your code was cut n' pasted, your problem probably lies in your use of $day in the else as you use day (string, not a scalar) beforehand.

    Also, you can simplify things by doing away with if/else entirely ...

    while( my $ref = $sth->fetchrow_hashref ) { $events{ $ref->{'day'} } .= $ref->{'title'}; }

        --k.


      Kanji++. Sometimes I feel like I must be code blind! Thanks very much!
      jg
      _____________________________________________________
      Think a race on a horse on a ball with a fish! TG
        If you had used "use strict;" Perl would have caught that for you. See perldoc strict.
        Syntax hiliting also sometimes helps to spot such things, but use strict should be used nevertheless.