Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > data to hash

Reply
Thread Tools

data to hash

 
 
George Mpouras
Guest
Posts: n/a
 
      07-29-2011
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]
}
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-30-2011
On Sat, 30 Jul 2011 00:27:48 +0300, George Mpouras <> wrote:

>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
In 'HashMap.put', "if (e.hash == hash && eq(k, e.key))" ? Red Orchid Java 3 01-30-2006 07:04 PM
hash of hash and complex data structure ngoc Ruby 4 05-27-2005 08:54 AM
standard library for hash table storage and hash algorithm Pieter Claassen C Programming 1 08-04-2004 03:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57