views:

120

answers:

2

I want to convert the code by Giuseppe Maxia for DBIx::SQLCrosstab to Java. I have done the bulk of the conversion, however I am stuck at these in the code listed below in DBIx::SQLCrosstab::Format.

Some pointers on the listed code will be helpful.

# find initial columns without sublevels
my @header_columns =();
#@header_columns = map {$_->{alias}} @{$self->{rows}};

if ($tree_depth> 1) {
    $tree->walk_down({
        callback => sub {
            my $node=shift;
            return 1 if $node->address eq "0";
            if ($node->descendants) {
                $_[0]->{_end_hc} = 1;
                return 1
            }
            push @header_columns, $node->name
                unless $_[0]->{_end_hc};
            my $cur_depth = ($node->address =~ tr/://) -1;
            $node->attributes->{rowspan} = $tree_depth - $cur_depth ;
            #print STDERR  $node->name," ",
            #    $node->attributes->{rowspan},
            #    "\n";
        },
        _end_hc => 0
    });
}
else {
    my $recs_rows = $#{$self->{recs}};
    COL:
    for my $col ( 0.. $#{$self->{recs}->[0]} ) {
        my $all_numeric =1;
        for my $row( 0.. $recs_rows) {
            my $value = $self->{recs}[$row][$col];
            $value = 0 unless defined $value;
            unless (($value =~ /^[0-9.]+$/)) 
            {
                push @header_columns,
                    ($tree->daughters)[$col]->name;
                $all_numeric =0;
                next COL;
            }
        }
        last COL if $all_numeric;
    }
}
+1  A: 

Do you have any specific questions about the code, or how to convert a specific clause? Your question is too vague to answer.

Jonathan
I am sorry but the issue is I am not understanding the Perl syntax.
Jude
you've written a question, posted it as answer, and it's really a commentary.
_ande_turner_
sudo, I am new to this site..I will get better with time. Thanks for your patience !!
Jude
+2  A: 

OK so there's an explained out code :

my @header_columns =(); # initialise an empty array

if ($tree_depth> 1) {
    $tree->walk_down({ # walk_down is a general function to walk down a tree...
        callback => sub { # first parameter is a closure, an anonymous function
            my $node=shift;
            return 1 if $node->address eq "0"; # apparently it looks for the top of tree
            if ($node->descendants) {
                $_[0]->{_end_hc} = 1;
                return 1
            }
            push @header_columns, $node->name
                unless $_[0]->{_end_hc};
            my $cur_depth = ($node->address =~ tr/://) -1;
            $node->attributes->{rowspan} = $tree_depth - $cur_depth ;
        }, # closure ends here
        _end_hc => 0
    });
}
else { # parsing a leaf of the tree
    my $recs_rows = $#{$self->{recs}}; # $self is a multidimensional array
               # $self->{recs} is an array of SQL rows
    COL:
    for my $col ( 0.. $#{$self->{recs}->[0]} ) { # this loops over columns and rows
        my $all_numeric =1;
        for my $row( 0.. $recs_rows) {
            my $value = $self->{recs}[$row][$col];
            $value = 0 unless defined $value;
            unless (($value =~ /^[0-9.]+$/)) 
            {
                push @header_columns,
                    ($tree->daughters)[$col]->name;
                $all_numeric =0;
                next COL;
            }
        }
        last COL if $all_numeric;
    }
}

Some more explanations :

if ($node->descendants) { # this is a function or method call
     $_[0]->{_end_hc} = 1; return 1  # @_ is the array containing all
                                                          # parameters for the current code block, it's a reference
}

my $cur_depth = ($node->address =~ tr/://) -1;  
# remove ':' from the returned string, then substract 1 
# (perl has weak typing, it allows treating any string of numbers as integers)

COL: # this is a label, used for goto() or more often an exit point when you break out of a for/while loop.
wazoox
Can you explain if ($node->descendants) { $_[0]->{_end_hc} = 1; return 1 }and my $cur_depth = ($node->address =~ tr/://) -1; in the if blockin the if block AlsoCOL: in the else block
Jude
Thanks a lot wazoox for your explanations.
Jude