Friday, December 22, 2006

C#: Random selection of elements in a list, with no repeats

Following code snippet shows how we can randomize selection of element in a list in effective manner:

System.Collections.Generic.List intList = new System.Collections.Generic.List();
intList.Add(1);
intList.Add(2);
intList.Add(3);
intList.Add(4);
intList.Add(5);

int size = intList.Count;
while (size>0)
{
size--;
int index = (new Random()).Next(0,size);
int elem = intList[index];
intList[index] = intList[size];
Console.WriteLine(elem.ToString());
}

Monday, December 18, 2006

Scala: A new programming paradigm

Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages.

Following are the highlights of this new language:
  • Object Oriented
  • Functional
  • Statically (strongly) typed
  • Extensible
  • Integration with .NET and Java Platforms
  • Built-in excellent pattern matching for Strings as well as other types like XML.
Please also read:
Why does the world need another programming language?

Thursday, December 14, 2006

Regex for atleast one alphabet and one number in string


This above Regular Expression uses Positive look ahead and Positive look behind to validate the conditions.

Wednesday, December 13, 2006

Perl: Free SQL Dependency Tracker

I am very impressed with RedGate SQl Depenency Tracker . This tool helped me lot in performing impact analysis of database object changed. Following Perl script is crude version of this tool. For given database object, it tracks dependent Stored Procedures, Functions, Views and Triggers drilled down to 3 levels. The only problem with this script it requires ODBC connection to Database.


#!/usr/bin/perl
use strict;
use DBI;

my $dbh = DBI->connect( "DBI:ODBC:localhost", 'sa', 'sa', { PrintError => 0 } );
die "Unable for connect to server $DBI::errstr"
unless $dbh;

my $search_object = $ARGV[0];

my $query = qq {
SELECT Name FROM dbo.sysobjects WHERE Xtype in ('P', 'FN', 'IF', 'V', 'TR', 'TF')
};

#print "Executing query: $query\n";
my $sth = $dbh->prepare($query)
or die "Couldn't prepare statement: " . $dbh->errstr;

$sth->execute or die "Couldn't execute statement: " . $sth->errstr;

my @object_array;
my %dbobjects;
while ( my @row = $sth->fetchrow_array ) {
if ( $row[0] !~ /^(dt_sys)/ ) {
push @object_array, $row[0];

}
}
$sth->finish;

foreach my $object (@object_array) {
my $text_query = qq {sp_helptext $object};
my $sth = $dbh->prepare($text_query)
or die "Couldn't prepare statement: " . $dbh->errstr;
$sth->execute or die "Couldn't execute statement: " . $sth->errstr;
my $text = '';
while ( my @row_text = $sth->fetchrow_array ) {

foreach (@row_text) {
$_ =~ s/--.*$//;
$text .= $_;
}

}
$text =~
s#/\*[^*]*\*+([^/*][^*]*\*+)*/("(\\.[^"\\])*"'(\\.[^'\\])*'.[^/"'\\]*)#defined $2 ? $2 : ""#gse;
$text =~ s/create.*?AS//msgi;
$dbobjects{$object} = $text;
$sth->finish;

#print $text, "\n--------------------------------------\n";
}

my @Level1 = FindDepenency($search_object);
foreach (@Level1) {
print $_, "\n";
my @Level2 = FindDepenency($_);
foreach (@Level2) {
print "------$_\n";
my @Level3 = FindDepenency($_);
foreach (@Level3) {
print "\t\t------$_\n";
}
}
}

sub FindDepenency {
my $key = shift;
my @return_arr;
foreach my $dbo ( keys %dbobjects ) {
next if ( lc($dbo) eq lc($key) );
if ( $dbobjects{$dbo} =~ /$key/msig ) {
push @return_arr, $dbo;
}
}
return @return_arr;
}

# Disconnect the database from the database handle.
$dbh->disconnect;

Thursday, December 07, 2006

Perl: Finding duplicate files

Sometimes we need to track duplicates files (same file with different name or path) in directory hierarchy. Following PERL script finds duplicate file in given directory.


#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use Digest::MD5;

###########################################################

# find_dups(@dir_list) -- Return an array containing a list
# of duplicate files.
###########################################################
sub find_dups(@)
{
# The list of directories to search
my @dir_list = @_;

# If nothing there, return nothing
if ($#dir_list < 0) {
return (undef);
}

my %files; # Files indexed by size

# Go through the file tree and find all
# files with a similar size
find(sub {
-f &&
push @{$files{(stat(_))[7]}}, $File::Find::name
}, @dir_list
);

my @result = (); # The resulting list

# Now loop through the list of files by size and see
# if the md5 is the same for any of them
foreach my $size (keys %files) {
if ($#{$files{$size}} < 1) {
next;
}
my %md5; # MD5 -> file name array hash

# Loop through each file of this size and
# compute the MD5 sum
foreach my $cur_file (@{$files{$size}}) {
# Open the file. Skip the files we can't open
open(FILE, $cur_file) or next;
binmode(FILE);
push @{$md5{
Digest::MD5->new->addfile(*FILE)->hexdigest}
}, $cur_file;
close (FILE);
}
# Now check for any duplicates in the MD5 hash
foreach my $hash (keys %md5) {
if ($#{$md5{$hash}} >= 1) {
push(@result, [@{$md5{$hash}}]);
}
}
}
return @result
}

# my @dups = find_dups(@ARGV);
my @dir = ('C:\tmp');
my @dups = find_dups(@dir);

foreach my $cur_dup (@dups) {
print "Duplicates\n";
foreach my $cur_file (@$cur_dup) {
print "\t$cur_file\n";
}
}

Tuesday, December 05, 2006

Video of Lifetime

This is video of a lifetime...http://www.metacafe.com/watch/277085/everyone_must_see_this/

lyrics -

Everybody's free to use sunscreen....Ladies and Gentlemen of the class of ’99 If I could offer you only one tip for the future, sunscreen would beit. The long term benefits of sunscreen have been proved byscientists whereas the rest of my advice has no basis more reliablethan my own meanderingexperience…I will dispense this advice now. Enjoy the power and beauty of your youth; oh nevermind; you will notunderstand the power and beauty of your youth until they have faded.But trust me, in 20 years you’ll look back at photos of yourself andrecall in a way you can’t grasp now how much possibility lay beforeyou and how fabulous you really looked….You’re not as fat as youimagine. Don’t worry about the future; or worry, but know that worrying is aseffective as trying to solve an algebra equation by chewingbubblegum. The real troubles in your life are apt to be things thatnever crossed your worried mind; the kind that blindside you at 4pmon some idle Tuesday. Do one thing everyday that scares you Sing Don’t be reckless with other people’s hearts, don’t put up withpeople who are reckless with yours. Floss Don’t waste your time on jealousy; sometimes you’re ahead, sometimesyou’re behind…the race is long, and in the end, it’s only withyourself. Remember the compliments you receive, forget the insults; if yousucceed in doing this, tell me how. Keep your old love letters, throw away your old bank statements. Stretch Don’t feel guilty if you don’t know what you want to do with yourlife…the most interesting people I know didn’t know at 22 what theywanted to do with their lives, some of the most interesting 40 yearolds I know still don’t. Get plenty of calcium. Be kind to your knees, you’ll miss them when they’re gone. Maybe you’ll marry, maybe you won’t, maybe you’ll have children,maybeyou won’t, maybe you’ll divorce at 40, maybe you’ll dance the funkychicken on your 75th wedding anniversary…what ever

Regex for Good and bad values


Above regex matches GOOD and doesnot match if string contains BAD.