my %dup; # duplicate charachters hash print "
##
# start by printing the opening table token
print "\n";
# now lets iterate over the lines of data
foreach my $line() {
# chomp of the \n from each line
chomp($line);
# ignore blank lines in the data
next if $line =~ m/^\s*$/;
# split the line into an array of elements
my @elements = split", ",$line;
# now iterate over the elements, if the element has
# already been seen then set it to
# which is the HTML token for a space, otherwise
# add the element to our duplicates hash
# I did this using map as shown:
# map{exists $dup{$_} ? $_= " " : $dup{$_}++ }@elements;
# in this context map is just a fancy way of
# writing a loop structure.
foreach (@elements) {
# the magical $_ is assigned to each element
# of our array @elements
# the (condition) ? do this : (else) do that
# construct is a one line version of the
# if (cond) {do this} else {do that} below
# so the map has the effect of seven lines...
if (exists $dup{$_}) {
# $_ is magical -> by changing $_
# we change this element of @elements
$_ = " ";
} else {
# define a new key of our %dup hash
# you can just do $dup{$_}++ to assign
# a value of one to $dup{$_} but this
# is more obvious
$dup{$_} = "seen, so define $_ key";
}
}
# print out the line via our sub, formating
# each element for a HTML table
print_row(@elements);
}
print "
\n";
sub print_row {
print"";
# @elements is passed to sub via @_ array, this
# print"$_ " for @_;
# is just short form for:
foreach my $element(@_) {
print"$element "
}
print" \n";
}
__DATA__
csc, tech, base
csc, comp, acm
csc, mous, base