data to hash

Discussion in 'Perl Misc' started by George Mpouras, Jul 29, 2011.

  1. 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]
    }
     
    George Mpouras, Jul 29, 2011
    #1
    1. Advertising

  2. George Mpouras

    Guest

    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
     
    , Jul 30, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Red Orchid
    Replies:
    3
    Views:
    1,092
  2. Pieter Claassen
    Replies:
    1
    Views:
    1,151
    CBFalconer
    Aug 4, 2004
  3. rp
    Replies:
    1
    Views:
    585
    red floyd
    Nov 10, 2011
  4. ngoc
    Replies:
    4
    Views:
    184
    Robert Klemme
    May 27, 2005
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    672
    David A. Black
    Jul 2, 2008
Loading...

Share This Page