M
Michael Friendly
I need to create hierarchical menu files from text input in a simple form
of comma delimited lines with fields (topic, url, description) like
Top1,,Desc1
Item1
Item11,url11,Description11
Item12,url12,Description12
Item13,url12,Description13
Item2,url2,Description2
Item21,url21,Description21
Item3,url3,Description3
Item31,url31,Description31
Item32,url32,Description32
Top2,,Desc2
Item1,url1,Description1
Item11,url11,Description11
Item12,url12,Description12
Item2,url2,Description2
Item21,url21,Description21
where the number of leading tabs in each item implicitly indicates level
in the tree. The output must be in the form of an explicitly structured,
comma-separated list, [ item, item, ... item ] where each item is either
a simple item or another list, e.g.,
[ item, [ item, item, ... item ], ... item ].
Missing fields in an item are represented by 'null', so the example
would appear as:
[
['Top1', null, null
, ['Item1', null, null
, ['Item11', 'url11', 'Description11']
, ['Item12', 'url12', 'Description12']
, ['Item13', 'url12', 'Description13']
]
, ['Item2', 'url2', 'Description2'
, ['Item21', 'url21', 'Description21']
]
, ['Item3', 'url3', 'Description3'
, ['Item31', 'url31', 'Description31']
, ['Item32', 'url32', 'Description32']
]
]
, ['Top2', null, null
, ['Item1', 'url1', 'Description1'
, ['Item11', 'url11', 'Description11']
, ['Item12', 'url12', 'Description12']
]
, ['Item2', 'url2', 'Description2'
, ['Item21', 'url21', 'Description21']
]
]
]
I'm reading the items and creating a list of hashes with the subroutine
below, but I can't figure out how to get from there to my desired
output. There must be some perl modules I can use. Can someone help?
sub read_items {
$file = shift;
open(IN, $file) || die("cant open $file\n");
$nitems = 0;
undef @items;
while( $line = <IN>) {
chomp($line);
next if $line =~ /^#/;
$line =~ s/(\t*)//;
$level = length($1); # level in tree
next unless $line;
$nitems++;
($title, $url, $desc) = split(/,\s*/, $line);
$desc =~ s/\'/\\'/g; # escape quotes in e.g., Murphy's Law
$item = {
level => $level,
title => $title,
url => $url,
desc => $desc,
};
push @items, $item;
}
close IN;
print STDERR "Read $nitems items\n";
return @items;
}
--
Michael Friendly Email: (e-mail address removed)
Professor, Psychology Dept.
York University Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html
Toronto, ONT M3J 1P3 CANADA
of comma delimited lines with fields (topic, url, description) like
Top1,,Desc1
Item1
Item11,url11,Description11
Item12,url12,Description12
Item13,url12,Description13
Item2,url2,Description2
Item21,url21,Description21
Item3,url3,Description3
Item31,url31,Description31
Item32,url32,Description32
Top2,,Desc2
Item1,url1,Description1
Item11,url11,Description11
Item12,url12,Description12
Item2,url2,Description2
Item21,url21,Description21
where the number of leading tabs in each item implicitly indicates level
in the tree. The output must be in the form of an explicitly structured,
comma-separated list, [ item, item, ... item ] where each item is either
a simple item or another list, e.g.,
[ item, [ item, item, ... item ], ... item ].
Missing fields in an item are represented by 'null', so the example
would appear as:
[
['Top1', null, null
, ['Item1', null, null
, ['Item11', 'url11', 'Description11']
, ['Item12', 'url12', 'Description12']
, ['Item13', 'url12', 'Description13']
]
, ['Item2', 'url2', 'Description2'
, ['Item21', 'url21', 'Description21']
]
, ['Item3', 'url3', 'Description3'
, ['Item31', 'url31', 'Description31']
, ['Item32', 'url32', 'Description32']
]
]
, ['Top2', null, null
, ['Item1', 'url1', 'Description1'
, ['Item11', 'url11', 'Description11']
, ['Item12', 'url12', 'Description12']
]
, ['Item2', 'url2', 'Description2'
, ['Item21', 'url21', 'Description21']
]
]
]
I'm reading the items and creating a list of hashes with the subroutine
below, but I can't figure out how to get from there to my desired
output. There must be some perl modules I can use. Can someone help?
sub read_items {
$file = shift;
open(IN, $file) || die("cant open $file\n");
$nitems = 0;
undef @items;
while( $line = <IN>) {
chomp($line);
next if $line =~ /^#/;
$line =~ s/(\t*)//;
$level = length($1); # level in tree
next unless $line;
$nitems++;
($title, $url, $desc) = split(/,\s*/, $line);
$desc =~ s/\'/\\'/g; # escape quotes in e.g., Murphy's Law
$item = {
level => $level,
title => $title,
url => $url,
desc => $desc,
};
push @items, $item;
}
close IN;
print STDERR "Read $nitems items\n";
return @items;
}
--
Michael Friendly Email: (e-mail address removed)
Professor, Psychology Dept.
York University Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html
Toronto, ONT M3J 1P3 CANADA