in reply to Re: Logic for sorting of this given array
in thread Logic for sorting of this given array

THanks for the help, it is usefull. But i have another problem that i need to pass more parameters in my array along with the level and department. it could be more than 3-4 now my array is like this : my @array = ({ level => 1,department => 'Office of President', linkdetails = "something"},
{ level => 2, department => 'Recruiting',linkdetails = "something1" },
{ level => 2, department => 'Software',linkdetails="something2"},
etc.....
how will i pass all these information along with the department ?. Actually i want to make on resultant array of
hashes that will contain all the values so that i can
pass to HTML template abd can print there. So level is
also needed so that while printing in HTML page i can multiply the level into number of spaces.
my resultant array of hashes after sorting look like this :

my @array = ({ level => 1, department => 'Office of President',},
{ level => 2, department => 'Pipe line',link="",},
{ level => 2, department => 'Recruiting',link=""},
{ level => 2, department => 'software',link="" },
{ level => 3, department => 'MIS',link="" },
{ level => 3, department => 'System Operations',link="" },
);

need ur help in this ... Thanks
  • Comment on Re: Re: Logic for sorting of this given array

Replies are listed 'Best First'.
Re: Re: Re: Logic for sorting of this given array
by CombatSquirrel (Hermit) on Aug 29, 2003 at 06:55 UTC
    Some pieces of advice:
    • Enclose array definitions, etc. in <code>-blocks
    • It's not link="", but link => "", or you'll get a syntax error
    • Capitalisation increases legibility ;-)
    To the code:
    #!perl use strict; use warnings; use Data::Dumper; sub returnParsedRec { my ($array, $index, $level) = @_; my $levelhash = {}; my $otherfields; while ($index < @$array) { last if ($array->[$index]->{'level'} < $level); if ($array->[$index]->{'level'} == $level) { $otherfields = {}; for (keys %{$array->[$index]}) { $otherfields->{$_} = $array->[$index]->{$_} unless ($_ eq 'level' or $_ eq 'department'); } $levelhash->{$array->[$index]->{'department'}} = [ $otherfields, returnParsedRec($array, $index + 1, $lev +el + 1) ]; } ++$index; } return $levelhash; } sub unParseRec { my ($levelhash, $result, $level) = @_; my $tmphash; for (sort keys %$levelhash) { $tmphash = $levelhash->{$_}->[0]; $tmphash->{'department'} = $_; $tmphash->{'level'} = $level; push @$result, $tmphash; unParseRec($levelhash->{$_}->[1], $result, $level + 1); } return $result; } sub returnParsed { return returnParsedRec(shift, 0, 1); } sub unParse { unParseRec(shift, [], 1); } my @array = ( { level => 1,department => 'Office of President', linkdetails => "some +thing"}, { level => 2, department => 'Recruiting',linkdetails => "something1" } +, { level => 2, department => 'Software', linkdetails => "something2"},) +; my $struct = returnParsed(\@array); print Dumper($struct); #print Dumper \@array; print "\n" . "=" x 70 . "\n\n"; print Dumper(unParse($struct));
    Be aware that HTML should not be formatted with spaces, look for lists (<ul>, <ol>, <dl>) and tables.
    Hope this helped.
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.