data to hash

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::Dumper;print Data::Dumper::Dumper(\%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]
}
 
S

sln

Here is something I think it is interesting.
Converts data (e.g. sql queries) to trees for a user driven virtual file
system

So, you invented something unique that has never been done before.
The only problem I see is that this:

[snip unknown complex crap]
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]
}

Stopped me in my tracks from reading it.

Next time, make a point or ask a question that pertains to something
specific.

-sln
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top