G
George Mpouras
Here is something I think it is interesting.
Converts data (e.g. sql queries) to trees for a user driven virtual file
system
#!/usr/bin/perl
my %hash = ();
my @array = (
[ 'a0' , 'a1' , 'a2' , 'a3' , 'a4' , 'a5' , 'a6' ],
[ 'b0' , 'b1' , 'b2' , 'b3' , 'b4' , 'b5' , 'b6' ],
[ 'c0' , 'c1' , 'c2' , 'c3' , 'c4' , 'c5' , 'c6' ],
[ 'd0' , 'd1' , 'd2' , 'd3' , 'd4' , 'd5' , 'd6' ],
[ 'e0' , 'e1' , 'e2' , 'e3' , 'e4' , 'e5' , 'e6' ],
);
foreach (@array) {
Stream_of_data_to_dynamic_hash( $_ , [1,0,5], \%hash )
}
use Data:
umper;print Data:
umper:
umper(\%hash);
# Stream_of_data_to_dynamic_hash( INPUT LIST REF, REF COLUMNS TO BECOME
KEYS, TARGET HASH REF )
#
# Convert a stream of data to a hash with user defined keys hierarchy.
#
# my %MyDB = ();
# Stream_of_data_to_dynamic_hash( ['John','Brown',11,'N. York'] , [3, 2]
, \%MyDB );
# Stream_of_data_to_dynamic_hash( ['May' ,'Green',72,'N. York'] , [3, 2]
, \%MyDB );
# print %MyDB;
#
sub Stream_of_data_to_dynamic_hash
{
my @data = $#{$_[0]} != -1 ? @{$_[0]} : return;
my @Keys = $#{$_[1]} != -1 ? @{$_[1]} : 0;
my $Ref = $_[2];
my $LastKey = $data[$Keys[-1]];
my %Del = ();
for (my $i=0; $i < $#Keys; $i++)
{
die "At second argument, the ".(1+$i)." element with value
\"$Keys[$i]\" is bigger than the total number $#data (0 offset) of items
of the 1st input list argument\n" if $Keys[$i] > $#data;
$Ref->{ $data[$Keys[$i]] } = {} unless ref $Ref->{ $data[$Keys[$i]] };
$Ref = $Ref->{ $data[$Keys[$i]] }
}
die "At second argument, the ".(1+$#Keys)." element with value
\"$Keys[$#Keys]\" is bigger than the total number $#data (0 offset) of
items of the 1st input list argument\n" if $Keys[$#Keys] > $#data;
foreach (reverse sort @Keys) { exists $Del{$_} ? ( next )
$Del{$_}=''
, splice(@data, $_, 1) ) } # Exclude used keys from the values
push @{$Ref->{$LastKey}}, [@data]
}
Converts data (e.g. sql queries) to trees for a user driven virtual file
system
#!/usr/bin/perl
my %hash = ();
my @array = (
[ 'a0' , 'a1' , 'a2' , 'a3' , 'a4' , 'a5' , 'a6' ],
[ 'b0' , 'b1' , 'b2' , 'b3' , 'b4' , 'b5' , 'b6' ],
[ 'c0' , 'c1' , 'c2' , 'c3' , 'c4' , 'c5' , 'c6' ],
[ 'd0' , 'd1' , 'd2' , 'd3' , 'd4' , 'd5' , 'd6' ],
[ 'e0' , 'e1' , 'e2' , 'e3' , 'e4' , 'e5' , 'e6' ],
);
foreach (@array) {
Stream_of_data_to_dynamic_hash( $_ , [1,0,5], \%hash )
}
use Data:
# Stream_of_data_to_dynamic_hash( INPUT LIST REF, REF COLUMNS TO BECOME
KEYS, TARGET HASH REF )
#
# Convert a stream of data to a hash with user defined keys hierarchy.
#
# my %MyDB = ();
# Stream_of_data_to_dynamic_hash( ['John','Brown',11,'N. York'] , [3, 2]
, \%MyDB );
# Stream_of_data_to_dynamic_hash( ['May' ,'Green',72,'N. York'] , [3, 2]
, \%MyDB );
# print %MyDB;
#
sub Stream_of_data_to_dynamic_hash
{
my @data = $#{$_[0]} != -1 ? @{$_[0]} : return;
my @Keys = $#{$_[1]} != -1 ? @{$_[1]} : 0;
my $Ref = $_[2];
my $LastKey = $data[$Keys[-1]];
my %Del = ();
for (my $i=0; $i < $#Keys; $i++)
{
die "At second argument, the ".(1+$i)." element with value
\"$Keys[$i]\" is bigger than the total number $#data (0 offset) of items
of the 1st input list argument\n" if $Keys[$i] > $#data;
$Ref->{ $data[$Keys[$i]] } = {} unless ref $Ref->{ $data[$Keys[$i]] };
$Ref = $Ref->{ $data[$Keys[$i]] }
}
die "At second argument, the ".(1+$#Keys)." element with value
\"$Keys[$#Keys]\" is bigger than the total number $#data (0 offset) of
items of the 1st input list argument\n" if $Keys[$#Keys] > $#data;
foreach (reverse sort @Keys) { exists $Del{$_} ? ( next )
, splice(@data, $_, 1) ) } # Exclude used keys from the values
push @{$Ref->{$LastKey}}, [@data]
}