Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2004
    Posts
    1

    Lightbulb Unanswered: Newbie needs help

    I'm pretty new to perl, and I'm trying to wrap my head around a Plus - Minus drop tree. I have code that works for a normal hash, but I need to add an additional two levels to the tree. Here's the code that works for a two-level tree:

    Javascript functions:
    Code:
    <script>
    	function tree_expand_toggle(data_type) {
    			var plusminus_name = "tree_plusminus_" + data_type;
    			var obj_name = "tree_" + data_type;
    
    			var plusminus_obj = document.getElementById(plusminus_name);
    			var obj = document.getElementById(obj_name);
    
    			if (obj.style.display == "none") {
    				plusminus_obj.innerText = "-";
    				obj.style.display = 'block';
    			} else if (obj.style.display == "block") {
    				plusminus_obj.innerText = "+";
    				obj.style.display = 'none';
    			}
    		}
    </script>
    Pearl code:
    Code:
    push @{$uploaded_data{"HighQ Entry"}},"Corn - 2001";
    
    push @{$uploaded_data{"Blend"}},"Blend 1";
    push @{$uploaded_data{"Blend"}},"Blend 2";
    push @{$uploaded_data{"Blend"}},"Blend 3";
    
    push @{$uploaded_data{"Lab Analysis"}},"2003-10-01";
    	
    foreach my $data_type (sort keys %uploaded_data) {
    	my $js_data_type = $data_type;
    	$js_data_type =~ s/ /_/g;
    
    	print "<li>";
    	if (scalar(@{$uploaded_data{$data_type}}) > 1) { # if array with multiple values
    		print "<a id='tree_plusminus_$js_data_type' style='width:8'>+</a>";
    		print "<a href=\"javascript:tree_expand_toggle('$js_data_type')\">$data_type</a>";
    		print "<ul id='tree_$js_data_type' style='display:none'>";
    		foreach my $value (sort @{$uploaded_data{$data_type}}) {
    			print "<li><a href=\"javascript:alert('$value')\">$value</a>";
    		}
    		print "</ul>";
    	} elsif (scalar(@{$uploaded_data{$data_type}}) == 1) { # if array with one value
    		my $value = $uploaded_data{$data_type}[0];
    		print "<a style='width:8'>&nbsp;</a>";
    		print "<a href=\"javascript:alert('$value')\">$data_type ($value)</a>";
    	} else { # if not array. only one value
    		my $value = $uploaded_data{$data_type};
    		print "<a style='width:8'>&nbsp;</a>";
    		print "<a href=\"javascript:alert('$value')\">$data_type ($value)</a>";
    	}
    }
    I need to pull in 4 elements from a database, I can do that, but I'm a little lost as to how to get these into the tree.

    The levels need to be
    Code:
    #	-mod[0]
    #		-prod[0]
    #			o Filename (order by date)
    #			o Filename (order by date)
    #		-prod[1]
    #	-mod[1]
    Can anyone help? I'm having a bit of a hard time understanding multilevel hashing as it applies here.

  2. #2
    Join Date
    Feb 2004
    Posts
    4

    Lightbulb

    instead of
    Code:
    push @{$uploaded_data{"HighQ Entry"}},"Corn - 2001";
    push @{$uploaded_data{"Blend"}},"Blend 1";
    push @{$uploaded_data{"Blend"}},"Blend 2";
    push @{$uploaded_data{"Blend"}},"Blend 3";
    push @{$uploaded_data{"Lab Analysis"}},"2003-10-01";
    how about
    Code:
    my %uploaded_data = (
      "HighQ Entry" => ["Corn - 2001"],
      "Blend" => ["Blend 1","Blend 2","Blend 3"],
      "Lab Analysis" => ["2003-10-01"],
    );
    or, algorithmically,
    Code:
    my %uploaded_data = ();
    while ( my $line = <> ) {
      chomp($line);
      my ($key,$dat) = split(/\t/,$line,2);
      $uploaded_data{$key} = [ split /\t/,$dat ];
    }
    then you can just
    Code:
    foreach my $mod( sort keys %uploaded_data ) {
      # do what you like with $mod
      foreach my $prod( @{$uploaded_data{$mod}} ){
        # do what you like with $mod and $prod
      }
    }
    See Chapter 4 of Programming Perl (nested data structures; lists of hashes, hashes of lists...)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •