waytoperl has asked for the wisdom of the Perl Monks concerning the following question:

Hello Masters,

I've been trying to come up with a logic for a problem, that im not able to figure out flow diagram. Its been 2 days. I really seeking help.

Task: I have a hash table which i created from csv file. Using hash table keys/values, need to replace certain blocks. In csv file there is repetition data in column 2. Column 2 is referred as Hash->values. Need to replace certain a set of logic to each repetitive data.

Example:

Column1: set of names_random, defined as {name1, name2, name3, name4,.....name200}

Column2: groups column1 data belong to, defined as {group1, group2, group1, group4, group2, group7, group4....group5}

Each data of column1 is associated to a group in column2.

Since these data are stored in hash table, what is the good logic to read column2 and place certain bunch of logic.

I'm trying to figure out and confused like do i need to store hash table and create a subroutine to read hash data from a file and access hash->values by pattern matching, if pattern matches replace these logic by entering to for loop or while loop.

I dont know where to start from. Please help by providing feedback through good way to implement logic.

Thanks in advance.

  • Comment on read hash table from a subroutine or retrieve previously stored hash table from a file

Replies are listed 'Best First'.
Re: read hash table from a subroutine or retrieve previously stored hash table from a file
by Kenosis (Priest) on Nov 19, 2013 at 20:23 UTC

    If I'm understanding you correctly, perhaps the following will be helpful:

    use warnings; use strict; use Data::Dumper; my ( %hashTable, %groupedHash ); while (<DATA>) { my ( $key, $val ) = split; $hashTable{$key} = $val; } groupByCol2( \%hashTable, \%groupedHash ); print Dumper \%groupedHash; sub groupByCol2 { my ( $hashTableRef, $groupedHashRef ) = @_; for my $key ( keys %$hashTableRef ) { push @{ $$groupedHashRef{ $$hashTableRef{$key} } }, $key; } } __DATA__ name1 group1 name2 group2 name3 group1 name4 group4 name5 group2 name6 group7 name7 group4 name8 group5

    Output:

    $VAR1 = { 'group4' => [ 'name7', 'name4' ], 'group2' => [ 'name2', 'name5' ], 'group7' => [ 'name6' ], 'group1' => [ 'name1', 'name3' ], 'group5' => [ 'name8' ] };

    Send two hash references to the subroutine groupByCol2--the first a reference to your 'hash table' with repeating values and the second to a hash for a hash of arrays (HoA). The second has holds the 'grouped' names.

      Thanks Kenosis. I've started working on my logic. I tried with you code, but it didnt parse keys and value correctly. Insted got output like:

      Output:

      is mixed with column1 and column2 data.

      $VAR1 = { 'group4' => [ 'group2','name7', 'name4','group7', ], 'group5' => [ 'name8' ] };

        Then, somehow, entires from column 2 (the groups' column) became keys in your 'hash table.' You need to discover how this happened, since the groupByCol2 subroutine merely uses your 'hash table's' values as keys and associates them with references to anonymous arrays which contain name entires.

Re: read hash table from a subroutine or retrieve previously stored hash table from a file
by GrandFather (Saint) on Nov 19, 2013 at 20:05 UTC

    I can't be bothered to read your node because you couldn't be bothered to format it nicely, but in answer to your title: use YAML.

    True laziness is hard work
Re: read hash table from a subroutine or retrieve previously stored hash table from a file
by waytoperl (Beadle) on Nov 19, 2013 at 20:19 UTC

    Well, stressed of coding with google search, understanding working of perl language is fun.

    I've decided to use hash table and parsing %$hash_ref into while loop search for column2 repetitive values. Next use for loop to create a loop until every repetitive value is parsed and concatenated to existing outfile. Next, start iteration to find other repetitive word from column2 (values).

    Good part is Learning Perl with Monks feedback makes Perl easy.

      Moving forward, I tried to initially just print hash table using hashref. Please help me try to understand. I know somehow I'm referring to array address, instead of actual value stored in memory.

      Expected Output:

      C_WREN => Group1 CAL_CLK => Group1 RX_IBIAS_2_25U[4:0] => Group3 REF_RATE[9:0] => Group3 AVD0 => Group4 SIDDQ => Group7

      Main Program

      sub mainCSV { # Open the CSV input file open (my $infile_CSV1, '<', "$infile_CSV") or die "Unable to open +$infile_CSV: $!\n"; my %hash = (); my $hash_ref = \%hash; while (my $line = <$infile_CSV1>) { chomp; $line =~ s/\s*\z//; my @array_CSV = split /,/, $line; my $key_CSV = shift @array_CSV; if ($hash{$key_CSV}) { warn "Duplicate key '$key_CSV'"; }; $hash{$key_CSV} = \@array_CSV; } # Explicit scalar context my $size = scalar keys %hash; # Prints Number of Pins (Hash size...) print "Number of Pins: $size\n"; # Open the output file and save hash in $outfile_RX_CSV open (my $outfile2, '>', "$outfile_CSV") or die "Unable to open $o +utfile_CSV: $!\n"; print $outfile2 Dumper(\%hash); close $outfile2; print "Stored $size list of pins in $outfile_CSV file.\n"; # Method 1 to print hashref table while( my ($key, $value) = each (%$hash_ref )) { print "$key => $value\n"; } }

      Actual output:

      C_WREN => ARRAY(0x13b2eab0) CAL_CLK => ARRAY(0x13ad9ea0) RX_IBIAS_2_25U[4:0] => ARRAY(0x13b2e6a0) REF_RATE[9:0] => ARRAY(0x13b0e090) AVD0 => ARRAY(0x13a2f200) SIDDQ => ARRAY(0x13b0dd00)
        This line:

         $hash{$key_CSV} = \@array_CSV;

        Is causing your most immediate problem. You are storing the reference to the array in your hash elements. So when you print out the hash elements, you are getting a reference to an array.