Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
OK, so someone asked what context my ugly reference question was in yesterday, and since what I'm doing might be useful for someone else, I thought I'd post the problem and my solution to see if (a) someone else could use it or (b) I'm reinventing the wheel and there's a better way commonly known.

OK, so this page structure on a web site:

1
|_2
|  \_5
|_3
  \_4
is stored as 'treepaths', like this:
1 => 001:000:000
2 => 001:002:000
3 => 001:003:000
4 => 001:003:004
5 => 001:002:005
that way, sorting them on these values brings them out in the desired display order.

Now, I want to display a lovely graphical tree depicting this, so I have 5 graphics and assign them values:

    - a blank square (0)
|#| - a page icon (1)
 |_ - an 'L' shape icon (2)
 |  - a vertical pipe icon (4)
 |- - a "t' shape icon (6)
So to create the correct matrix, I take the following rules:
  1. create an initial matrix by sorting the treepaths and then splitting them on ':' and converting to a number
    1 0 0 
    1 2 0
    1 2 5
    1 3 0
    1 3 4
    
  2. Then, parsing from right to left,
    • change 1st non-zero element to '1'
    • change next to '2'
    • change remaining to '0'
    ie
    1 0 0 
    2 1 0
    0 2 1
    2 1 0
    0 2 1
    
  3. Finally, parse each element, going from bottom right to top left with the rule "For this element, if this element !=1 and the one below it is >=2, add 4. ie
    1 0 0 
    6 1 0
    4 2 1
    2 1 0
    0 2 1
    
Then just convert each number to the relevant icon and you have a pretty tree structure.

Or am I making a mountain out of a molehill?

cLive ;-)


In reply to representing a tree graphically... by cLive ;-)

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found