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

Hi All,

I got the date function working, thank you all for you help. Now, i have an Array(not sure if its the best way to do this) of elements of $location;$date;$time+. This is just one element in the array and the + is the delimeter. For example when i print out the info in the array i get. Mexico City, Mexico;2/27/2002;8:30 am+Mexico City, Mexico;2/27/2002;2:00 pm+Albany, NY;10/26/2001;8:30 am+. Which contains three different elements. Now what i want to do is compare these elements to see if there are two elements that are in the same city and date, and have an AM and PM in the time. In this example there is one:
Mexico City, Mexico;2/27/2002;8:30 am
Mexico City, Mexico;2/27/2002;2:00 pm
These are both in the same city, same date, and have an AM and PM. These are courses with prices, so what i want to do then is if there is one, or two and so forth give each course a %25 discount.

Any help on how to go about this will be greatly appreciated.

Thanx,
Kiko

Replies are listed 'Best First'.
Re: Comparing elements in an Array
by dragonchild (Archbishop) on Oct 25, 2001 at 00:16 UTC
    Use split.
    sub compare { my ($elem1, $elem2) = @_; my @elem1Arr = split /;/, $elem1; my @elem2Arr = split /;/, $elem2; for (0 .. 1) { return 0 if $elem1Arr[$_] ne $elem2Arr[$_]; } my @time1Arr = split / /, $elem1Arr[2]; my @time2Arr = split / /, $elem2Arr[2]; return 0 if $time1Arr[1] eq $time2Arr[1]; return 1; }

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.