Saturday, June 2, 2012

Sorting Perl Hash of Hash of Array for Date fields with amount data.

We have to print out the entries of a database in year-wise, month-wise and day-wise hierarchy.

The best and easiest( that is with a bit of perl advanced data structures understanding)  way I have found with a little of investigation, is by using Perl's Advanced Hash and Array data structures.
The Perl's sort command/inbuilt sorting algorithm was also used to easily sort arrays and Hash keys.


The task in hand is, I have date and amount columns  in the data base with values as below:


2012-06-01 22:47:04  999

2012-05-02 22:47:04  228

2012-06-09 22:47:04  222

2014-05-01 22:47:04  333

2014-08-01 22:47:04  444

2015-05-01 22:47:04  555

2015-04-03 22:47:04  111


Then I need to lay them out in this order

2012 - Year

         05 - Month

              02 - Day

               228

        06 - Month

              01 - Day

              999

              09 - Day

              222

2014 - Year

        05 - Month

            01 - Day

            333

        08 - Month

            01 - Day

            444

2015 - Year

        04 - Month

             03 - Day

             111

        05 - Month

            01 - Day

            555


First I access these rows from the database and segregate out the values in to Hash of Hash of Hash of Array with the below while-loop:       
 while (my $h = $sth->fetchrow_hashref())
{
    ($year, $month, $day) = split(/\-/, $h->{pgdate});
    $day =~ s/([0-9]) .*/$1/;
    push @{ $dateHsh{$year}{$month}{$day} }, $h->{pgid};
}


Now I have created the data structure  %dateHsh.
Finally in order to print the formatted layout where I have chosen to print it out in a nested HTML Definition List with the below foreach loop:

foreach my $syear ( sort keys %dateHsh) {
    print "<dl>";
    print "<dt>$syear - Year </dt><dd></dd>";
    for my $smonth ( sort keys %{ $dateHsh{$syear} } ) {
        print "<dl><dt>$smonth - Month </dt><dd></dd>";
        for my $sday ( sort keys %{ $dateHsh{$syear}{$month} } ) {
            print "<dl><dt>$sday - Day </dt><dd></dd></dl>";
            for my $val ( my @sorted = sort { $a <=> $b } @{ $dateHsh{$syear}{$smonth}

{$sday} } ) {
                print "<dl><dt>$val</dt><dd></dd></dl>";
            }
        }
    }
    print "</dl></dl>";
}


Summary:
With a little bit of programming knowledge we can quickly retrieve/pull data from databases put them into a orderly in-memory data structure and then rearrage the in-memory datastructure(s) for printing it out to what ever format we require.

Tags: perl  sort data structure

Wednesday, August 24, 2011

Installing OBIEE 10.1.3.4.0 on Oracle XE

  1. Put in the WindowsXP Prof. CD in CDRom.
  2. Goto Control/Panel -> Add/Remove Windows Components -> Select the Internet Information Services(IIS) checkbox, then select Next
  3. Install the j2sdk-1_5_0-beta-windows-i586.exe java executable.
  4. Install the WindowsInstaller-KB893803-v2-x86.exe executable.
  5. Configure the microsoft loop back adapter.
  6. Download and install the 10g OracleXE OracleXEUniv.exe executable.
  7. Execute the setup.exe in the CD: E:\obiee\Server\Oracle_Business_Intelligence folder
  8. Open the command prompt and enter below: sqlplus system/welcome1@XE where welcome1 is given as password.
  9. Tthen enter below to create user SUPPLIER2
  10. create user SUPPLIER2 identified by SUPPLIER2 default tablespace
  11. users;
  12. grant dba to SUPPLIER2;
  13. Unzip D46077GC10__setup_files_win2000.zip to get SUPPLIER2.dmp C:\temp
  14. Then in command prompt cd to C:\temp and run: IMP SUPPLIER2/SUPPLIER2@XE FILE=SUPPLIER2.dmp FULL=Y

Monday, April 18, 2011

Compiling shttpd_1.17.c on Solaris X64

when facing below issues:


$ gcc shttpd_1.17.c -o shttpd
Undefined first referenced
symbol in file
bind /var/tmp/ccjlfsyW.o
recv /var/tmp/ccjlfsyW.o
send /var/tmp/ccjlfsyW.o
getsockname /var/tmp/ccjlfsyW.o
accept /var/tmp/ccjlfsyW.o
listen /var/tmp/ccjlfsyW.o
socket /var/tmp/ccjlfsyW.o
setsockopt /var/tmp/ccjlfsyW.o
connect /var/tmp/ccjlfsyW.o
getpeername /var/tmp/ccjlfsyW.o
inet_ntoa /var/tmp/ccjlfsyW.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

$ gcc shttpd_1.17.c -lsocket -lnsl -o shttpd
$ ./shttpd -p 8080 -c .pl

Saturday, April 16, 2011

To resolve a host domain name using a different nameserver

C:\>nslookup
Default Server: UnKnown
Address: 10.0.0.1

> server ns71.domaincontrol.com
Default Server: ns71.domaincontrol.com
Address: 216.69.185.46

> coreunix.com
Server: ns71.domaincontrol.com
Address: 216.69.182.46

Friday, March 5, 2010

How to find multiple matches of same string in perl

This is one way:

$count += () = $teststring =~ /(test)/g;

Saturday, September 13, 2008

Vim under cursor word search


To search for a word under the cursor in the current file you
can use either the "*" or "#" keys.

The "*" key will search for the word from the current cursor
position to the end of the file. The "#" key will search for the
word from the current cursor position to the top of the file.

Thursday, July 3, 2008

Using WGET to download files through http_proxy

setup the env then execute the wget command as below:

export http_proxy=http://www-proxy.company.com:80


wget --output-document=jdk-1_5_0_15-linux-i586.bin http://cds.sun.com/is-bin/jdk-1_5_0_15-linux-i586.bin