in reply to extract number of times a value appears in an array

This is not homework lol.

I wish it was though, wouldn't be as stressful.

My coding

#made this events_array up... but the concept remains It will always be in a particular order, least to greatest in this cas +e. @events_aray = ("1","3","3","4","5","5"); my %days; my @appears; my @date_count; my $c = 0; foreach my $v (@events_aray) { #this foreach will split the values of the array I have populated #it will then produce a hash with keys as a date. #another array will be generated containing each dates of the event. + my ($date, $subject, $text) = split(/,/, $events_array); $days{$date} .= "<b>$subject($symbolx)<b><br><br>$text<br><br>"; $date_count[$c] = "$date"; #this stores the date of the event, thi +s will be the array I need to figure out how many times each date app +ears $c++; } #some subroutine to figure out how to retrieve the amount. #The array + that I said I needed to generate will be @appears. #this is where I need a little help on. I will continue #looking thou +gh and if resolved I will post back asap. $c = 0; foreach my $u (keys %days) { print "date appeared $appears[$c] times"; print "<br><br>$days{$u}"; $c++; }
Thats what I have so far....

thanks, John

Replies are listed 'Best First'.
Re^2: extract number of times a value appears in an array
by GrandFather (Saint) on Aug 18, 2006 at 00:55 UTC

    The count part of the problem I'd solve thus:

    use strict; use warnings; my @events_aray = (1, 3, 3, 4, 5, 5); my %days; ++$days{$_} for @events_aray; print "date $_ appeared $days{$_} times\n" for sort {$a <=> $b} keys %days;

    Prints:

    date 1 appeared 1 times date 3 appeared 2 times date 4 appeared 1 times date 5 appeared 2 times

    You may need a different sort if your values are not numbers.


    DWIM is Perl's answer to Gödel