Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to Perl Need Help with Creating .JS array

Status
Not open for further replies.

fooobee

Technical User
Nov 13, 2003
33
US
I was able to create a .js array file from CMS that contains content needed to build a hierarchal tree menu. From this array, I needed to create an array that the tree menu uses to populate itself.

This is the .js file that comes from CMS. (below is what it needs to look like).
Also, some rules to help.

Rules
remove #p242_2393 framgments where the classes = chap or ahead. +-
if a chap has no ahead or bhead following it then we need to add {format:{isFolder:true}} (see introduction)
Also, notice the brakets ] and when they should or shouldn't close. Like if Ahead has B head you keep the Ahead open and close after bhead.
The highest level of the tree is bhead within Ahead, we don't need ahead under any bhead.



var pcor_sections = [ 0

,'<p class="chap"><a href="chf-ES.htm#P6_36" target="_top">Executive Summary</a></p>'

,'<p class="ahead"><a href="chf-ES-cmc-00.htm#P52_1438" target="_top">What are the key parameters of the CHF market?</a></p>'

,'<p class="chap"><a href="Pharmacor-Chapter-Head.htm#P89_11313" target="_top">Introduction</a></p>'

,'<p class="chap"><a href="CHF-EP-cmc.htm#P210_16143" target="_top">Etiology and Pathophysiology </a></p>'

,'<p class="ahead"><a href="CHF-EP-cmc-02.htm#P242_29404" target="_top">Etiology</a></p>'

,'<p class="bhead"><a href="CHF-EP-cmc-02.htm#P13_3767" target="_top">Causes of Chronic Heart Failure</a></p>'

,'<p class="bhead"><a href="CHF-EP-cmc-02.htm#P18_5924" target="_top">Clinical Characterization of Chronic Heart Failure</a></p>'

,'<p class="ahead"><a href="epi-mock-up-cmc-03.htm#P62_16890" target="_top">United States</a></p>'

,'<p class="chap"><a href="chf-ct.htm" target="_top">Current Therapies</a></p>'

,'<p class="ahead"><a href="chf-ct-cmc-00.htm" target="_top">Diuretics</a></p>'

];



Needs to be exactly like this…
var TREE_NODES = [

f(["Executive Summary", "chf-ES.htm", null,
f(["What are the key parameters of the CHF market?", "chf-ES-cmc-00.htm", null]),
]),


f(["Introduction", "Pharmacor-Chapter-Head.htm", null,{format:{isFolder:true}}]),


f(["Etiology & Pathophysiology", "CHF-EP-cmc.htm", null,
f(["Etiology", "CHF-EP-cmc-02.htm", null,
f(["Causes of Chronic Heart Failure", "CHF-EP-cmc-02.htm#P13_3767", null]),
f(["Clinical Characterization of Chronic Heart Failure", "CHF-EP-cmc-02.htm#P18_5924", null])
]),
f(["United States", "epi-mock-up-cmc-03.htm#P62_16890", null]),
]),


f(["Current Therapies", "chf-ct.htm", null,
f(["Diuretics", "chf-ct-cmc-00.htm", null]),
]),

];
 
OK, and whats the connection to perl? And is this class/school work?
 
Other questions would be:

Have you tried doing this yourself?
Could you post the code you have already?

But the answers to Kevin's questions are really more important to start with.

- George
 
I tried in js but I don't know reg exp to well and I don't know perl to well either. Here is my attempt with perl. It needs to match exactly, but it doesn't.

#!/usr/bin/perl

#upload to your cgi-bin
##set permissions on build_treenodes.pl to 755
##set permissions on data DIR to 777
##set permissions on newtreenodes.js to 777


$fileto_open = "data/Sections.txt";
open(DATA, "$fileto_open") || die (print "Error reading to $fileto_open");
@all_lines = <DATA>;
close(DATA);


$x=1;
foreach $line (@all_lines){

next unless($line =~ m/<h\d+>/g);
if($line =~ m/h1/gi){
$header_line = 1;
$pad = "\t]),\n";
}
else{
$header_line = '';
$pad = "\t";
}
$line =~ s/%20/-/gi;
$line =~ s/href\s*=\s*\"([^\"]+)\"/$1/gi;
$link= $1;
$link =~ s/(.*)\#.*/$1/g;
$line =~ s/.*\<h\d\>.*?top\"\>(.*)\s*\<\/a.*\r/$1/gi;
$line =~ s/\d+\.//gi;
chomp($line);
$pad = "\n"if $x==1;
$newline = qq~$pad\f(["$line", "$link", null]),\n~ ;
push(@treenodes, $newline)unless($line =~ m/<img src/);
$x++;
}
$ending = <<HTML;
];
// Don't try this at home...
var realRedrawAllTrees = RedrawAllTrees;
RedrawAllTrees = function () {
var i;
for (i = 1 ; i <= lastIdAssigned ; ++i) {
var node = tree1.nodeByID(i);
if (node) {
tree1.ensureVisible(node.index);
}
}
RedrawAllTrees = realRedrawAllTrees;
RedrawAllTrees();
};

HTML

unshift(@treenodes, qq~var TREE_NODES = [~);
push(@treenodes,$ending);
print qq~@treenodes\n~;


$fileto_open = "data/newtreenodes.js";
open(DATA, ">$fileto_open") || die (print "Error reading to $fileto_open");
print DATA @treenodes;
close(DATA);
 
if we help, do we get the qualification?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top