Thursday, January 13, 2011

links for the technial replies

http://www.systemverilog.in/forum/showthread.php?tid=155
http://www.systemverilog.in/forum/showthread.php?tid=143

Saturday, January 1, 2011

Why Associative Arrays (Hashes, as they are called in Perl) ??

I have gone through the topic “Associative Arrays” many times in System Verilog LRM and was wondering what was so great about it. We can have index other than integers type....characters, strings, or even arrays. So what???

One of my colleagues also have given example for this: For selecting 4 ports of a SoC; he used an associative array, but with indexes of type integer only....

The example below which I came across gives correct idea of Associative arrays and importance of it.
Searching a pattern in a list and finding the number of occurrences of it......

Inputfile.txt                       Outputfile.txt

test1                Pass           test1       2
test2                Pass           test2       2
test1                Fail           test4       1
test3                Fail
test1                Fail
test4                Pass
test1                Pass
test2                Pass 

We need to search how many times a test passed and then print it. See the below code, how easily it can be done with the help of associative arrays(hashes).


#!/usr/bin/perl
open(INPUT_FILE, "inputfile.txt");
while($inputline = <INPUT_FILE>)
{
   @word = split(/\s+/, $inputline);
   if($word[1] = "PASS")
   {
      $test = $word[0];
      $associative_array{$test}+= 1;
   }
}

foreach $test (sort keys(%associative_array))
{
   print "$test: $associative_array{$test}\n";
}

Here we need not worry about keeping track of the word count and total entries in the list. The word we are looking itself is made the index and this reduces the complexity.