Developer OneNote

Emma's OneNote for Microsoft Windows, Office and Programming

Tag: Code Snippets

Perl Replace String in File

23 June, 2010 | Category: Development

This code snippet demonstrates how to replace string in file using perl. This perl script takes in an input file, replaces the all string foo with bar.

my $file = $ARGV[0];
my $filetmp = "$ARGV[0].tmp";
open (INPUT, "< $file") or die("Unable to open $file");
open (TMP, "> $filetmp") or die("Unable to open $filetmp");
while(<INPUT>) {
    if(/foo/) {
        s/foo/bar/;   # replace foo with bar   
    }
    print TMP $_;
}
close(INPUT);
close(TMP);
rename $filetmp, $file;

Perl Last Element of Split

8 June, 2010 | Category: Development

There is an easier way to get last element on a split operation, save split result to an array and use $array[-1] and this will access last element of the arrays.

Example

$strings=”foo,bar,test”;
my @fields = split(/,/, $strings);
print $fields[-1] ;  # the last str in $strings

Perl Backslash in Regular Expression

8 June, 2010 | Category: Development

This note is for a trick to use \Q and \E  to escape characters for regular expression. You will find it very useful when you do string substitution and your pattern contains characters like slashes or backslashes .

Read more »

Perl Escape Backslashes in String Substitution

8 June, 2010 | Category: Development

This note is for a trick to use \Q and \E  to escape characters for regular expression. You will find it very useful when you do string substitution and your pattern contains characters like slashes or backslashes .

Read more »

Perl Add Directory to Path

8 June, 2010 | Category: Development

Say you have a directory d:\bin and you want to add it to path environment at the beginning of your Perl script. Following code will do the trick.

my $dir="d:\\bin";
$ENV{PATH}.=";$dir";

Then $dir will be included in new $ENV{PATH}

Perl Redirect Command Output

29 May, 2010 | Category: Development

In Perl, you can execute external commands using system() or “. However, system and “ does not redirect command output to console and this results people who runs your perl script can’t see it. This also make debug much harder. Perl does not have a build in switch that equals to batch scripts’ “@echo on”, however this can be worked around by creating a ExecuteCommand subroutine.

sub ExecuteCommand {
my $cmd= $_;
my @cmdoutput = `$cmd`;
for $line (@cmdoutput) {
print $line;
}

Now just change your code from system($command) or `$command` to ExecuteCommand($command) and you will see all command output are redirected to console. Read more »

How to Validate URL in C#

27 May, 2010 | Category: Development

I searched google for how to valicate URL in C# and most results say using a regular expression. Eventually I found Uri.TryCreate which is a built in method in C# to check if a string is a valid URL

Uri uri = null;

if (!Uri.TryCreate(url, UriKind.Absolute, out uri) || null == uri)

    //Invalid URL

    return false;

}

 

Reference:

http://msdn.microsoft.com/en-us/library/ms131572(v=VS.90).aspx

SpellChecker in WPF4 TextBox

4 May, 2010 | Category: Development

TextBox and RichTextBox in WPF4 has the built in SpellChecker functionality. It’s currently available in following four languages

  • #LID 1033 – English
  • #LID 3082 – Spanish
  • #LID 1031 – German
  • #LID 1036 – French

Enable SpellChecker functionality on TextBox or RichTextBox is as easy as just setting SpellCheck.IsEnabled to True on the controls.

   <TextBox SpellCheck.IsEnabled="True" />

Read more »

Use coalescing operator to write more readable code

27 April, 2010 | Category: Development

Tired of code like this?

public string Foo

{

get { return foo; }

set

{

if (value == null)

value = String.Empty;

foo = value;

}

}

public string Bar

{

get { return bar; }

set

{

if (value == null)

value = String.Empty;

bar = value;

}

}

Use coalescing operator to make your code a bit more readable.

Read more »

C# reading from and write to text file

11 March, 2010 | Category: Development

CSharp sample for reading from a text file and writing to a text file.

//Reading from a text file      

System.IO.StreamReader srFile = new System.IO.StreamReader(@"c:\foo.txt");

string str = srFile.ReadToEnd();

srFile.Close();

 

//Writing to a text file

string lines = "foobar";

System.IO.StreamWriter swFile = new System.IO.StreamWriter(@"c:\bar.txt");

swFile.WriteLine(lines);

swFile.Close();