(1) For example:
use strict;
my @a = ('A', 'B', [1, 2, ['C', 'D'], ['X',
[11, 13, 15], 'Y'], [30]], [], 400);
print DepthFirstTraversal(@a);
exit 0;
sub DepthFirstTraversal {
my $list = @_;
my $result = "";
foreach (@_) {
if (ref $_ eq 'ARRAY') {
$result .= DepthFirstTraversal(@{$_});
}
else {
$result .= "\"$_\" ";
}
}
$result;
}
(2) For example,
perl -ne "print $_ unless $w{$_}; $w{$_}++;" somefile.txt >somefile_out.txt
(3) For example,
use strict;
# h3q3.pl
# by Kwok-Bun Yue
# This program reads a text file of discussion
board
# header and print out part of the HTML code
for
# listing the message headers in a tree structure.
#
my $HEADER_FILE = "h3q3dat.txt";
my $SEPARATOR = '!!';
# array of children nodes (arrays)
# Store information of all children nodes.
For examples, if nodes 3, 5 and
# 7 are children of node #1, then
# $children[1] = ["3!!name of 3!!email of
3!!title of 3",
# "5!!name of 5!!email
of 5!!title of 5",
# "7!!name of 7!!email
of 7!!title of 7",
my @children = ();
# Read data file to populate @children.
open DATA, "<$HEADER_FILE" || die "Can't
open file $HEADER_FILE";
while ($_ = <DATA>) {
chomp;
my ($id, $parent, $name, $email, $title)
= split /$SEPARATOR/;
if ($children[$parent]) {
push @{$children[$parent]}, "$id$SEPARATOR$name$SEPARATOR$email$SEPARATOR$title";
}
else {
$children[$parent] = ["$id$SEPARATOR$name$SEPARATOR$email$SEPARATOR$title"];
}
}
close DATA;
if ($children[0]) {
# Have at least one node.
printTree(0);
}
# Print the current subtree with the given
root node recursively.
sub printTree {
my $rootNode = shift;
print "<ul>\n";
for (my $i=0; $i< @{$children[$rootNode]};
$i++) {
my ($child, $name, $email, $title)
= split /$SEPARATOR/, ${$children[$rootNode]}[$i];
print "<li><a href=\"mailto:$email\">$name</a>:
$title</li>\n";
printTree($child) if $children[$child];
}
print "</ul>\n";
}
exit 0;