Actually its a simple hash but not anonymous hash.

I have expanded on Veltro's response.

Given the simple structure provided, I was able to back-engineer the hash. I then used the Data::Dumper->Dump method that allows names to be stored, following the documentation perldoc Data::Dumper.

There are slight differences in the way the storage occurs. The named hash gets stored as a hash, whereas the VAR structures are stored anonymously. Notice the arguments to Data::Dumper->Dump are each in anonymous arrays. You may need to play around with the sigils to the arguments a little to get the desired output.

#!/usr/bin/perl -T # use v5.22 for <<$datafh>> use strict; use warnings; use feature qw/state/; use Data::Dumper; my $data = get_input_data(); open my $datafh, '<', \$data or die 'not getting it'; my %HASH; while(my $line = <$datafh> ){ state $kv; state $current_key; chomp($line); if( $line =~ s/\A\$VAR\d+\s\=\s(\'|\[)// ){ $kv = $1; if( $kv eq '\'' ){ $line =~ s/\'\;\Z//; $current_key = $line; } next; }else{ next if $line =~ m/\A\s+\]\;\Z/; $line =~ s/\A\s+\'(.*)\'\,?\Z/$1/x; push @{ $HASH{ $current_key } }, $line; } } print 'Dumper with VAR',"\n"; print Dumper(\%HASH); =head output1 $VAR1 = { '3|1' => [ 'user', 'user', 'user', 'admin', 'admin', 'manager' ], '2|7' => [ 'system' ] }; =cut print 'Dumper with names',"\n"; print Data::Dumper->Dump([\%HASH],[qw(*HASH)]); =head output2 %HASH = ( '3|1' => [ 'user', 'user', 'user', 'admin', 'admin', 'manager' ], '2|7' => [ 'system' ] ); =cut sub get_input_data{ q{$VAR1 = '3|1'; $VAR2 = [ 'user', 'user', 'user', 'admin', 'admin', 'manager' ]; $VAR3 = '2|7'; $VAR4 = [ 'system' ]; }; }

fun note: output1 and output2 appear to sort in the same order after a few manual runs, although which key is top is random, they both show keys in same sorting order as each other. Perhaps an internal optimisation?


In reply to Re^3: Need to find unique values in hash by Don Coyote
in thread Need to find unique values in hash by dipit

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.