Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Building a data Hierarchy

by graff (Chancellor)
on Mar 21, 2007 at 02:01 UTC ( [id://605779]=note: print w/replies, xml ) Need Help??


in reply to Building a data Hierarchy

Firstly, most of the code was donated by "Graff", however, I want to make it available to all as it works very well.

I'm glad to see you have something working (after such a length development phase ;) But I'd just like to clarify that, at least in terms of raw byte count, I did not write "most of the code" posted above. I just provided the  while (<DATA>) loop, the following "for" loop, and the "trace_down" sub.

I think the post could be a more interesting contribution, and more people could find it useful, if that portion of your script were abstracted away from the database stuff. The basic premise is:

  • Read a file containing an unsorted list of "child/parent" records (one per line), where a child value in one record might be the parent value in one or more other records scattered throughout the list.

  • Populate a "node" hash structure, keyed by the string values found in each record, to keep track of parent and child relations among the strings.

  • Output the records in sorted order, so that for any string value that appears as both "child of" and "parent of" other values, the record containing its "child of" relation is first.

So as a general-purpose function, the while loop and for loop should be in a subroutine, whose parameters might be: input file handle, output file handle, maybe a string to use as the split regex. That and the "trace_down" sub could go into a "SortTree.pm" module, or something to that effect, to make it easier to re-use.

As for the database stuff you've posted here, it's fine that it works for you, but I expect anyone else would just have to scrape it off, because it really only works for you. Also, it's not clear to me how or why the contents of the "resultsfile" (from the DB query) end up as pipe-delimited. Is that some sort of default setting on your database server, or in Win32::SqlServer?

One last nit-pick -- you could replace all this:

my @TimeArray = localtime(time); my $year = sprintf ("%04d",$TimeArray[5]+1900); my $monthasnum = sprintf ("%02d",$TimeArray[4]+1); my $day = sprintf ("%02d",$TimeArray[3]); my $hour = sprintf ("%02d",$TimeArray[2]); my $minute = sprintf ("%02d",$TimeArray[1]); my $datestamp = "$year"."_"."$monthasnum"."_"."$day"."_"."$hour" ."_" +."$minute" ;
with this:
use POSIX; my $datestamp = strftime( "%Y_%m_%d_%H_%M", localtime );
Gotta love POSIX... (updated to add link to POSIX man page)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://605779]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 11:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found