Wednesday, July 20, 2011

Unstructured, Procedural, Modular Programming

1. Unstructured Programming

They are simple programs consists only one main program. “Main Program” stands for a sequence of commands or statements which modify data which is global throughout the whole program.

image

2. Procedural Programming

With procedural programming you are able to combine returning sequences of statements into one single place.Procedure call is used to invoke the procedure. After the sequence is processed, flow of control proceeds right after the position where the call was made.

image

Now a program can be viewed as a sequence of procedure calls. The main program is responsible to pass data to the individual calls, the data is processed by the procedures and, once the program has finished, the resulting data is presented. Thus, the flow of data can be illustrated as a hierarchical graph, a tree, as shown bellow.

image

Now we have a single program which is divided into small pieces called procedures.

3. Modular Programming

With modular programming procedures of a common functionality are grouped together into separate modules.

image

Sunday, July 17, 2011

Quick Guide to Regular Expressions

Introducing Regular Expressions – Fastest learning Method

Regular Expressions (or regex) are string patterns which used for text processing. ASP, .NET, C#, Java and many other languages support RegEx. Simply it contains string text and special instructions-statements.
RegEx is a minilanguage that has a special syntax and instructions that you must learn and I like to discuss them step by step.
It is important to note that syntax is the easiest part of mastering regular expressions. The real challenge, however, is learning how to apply that syntax, how to dissect problems into solvable regex solutions. That is something that cannot be taught by simply following exercises, but like any language, mastery comes with practice.


RegEx Lesson 1 – Matching Characters
RegEx Lesson 2 – Matching Several Characters
RegEx Lesson 3 – Repeating Matches

Friday, July 15, 2011

RegEx Lesson 3 – Repeating Matches – Quick Guide to Regular Expressions

3.1 Matching one or more characters
To match one or more instances of a character, simply append a + character.
Eg: matching an email
T: askamith@fmail.com
RegEx: \w+@\w+\.\w+
R: askamith@fmail.com


3.2 Matching Zero or More Characters
use * metacharacter, it is placed right after a character or a set and it will match zero or more instances of the character or set.
T: Amith Ah Amh A3465654747ucrfjerghggh
RegEx: A.*h
R: Amith Ah Amh A3465654747ucrfjerghggh43566dcs

last regex match result reminds us that we have to limit the exact match. its done using (?).


3.2 Matching Zero or One Character - ?
use ? metacharacter for matching zero or one char right after a character.
T: The URL is http://www.askamith.wordpress.com/, to connect
securely use https://www.askamith.wordpress.com/ instead.
RegEx: https?://
R: The URL is http://www.askamith.wordpress.com/, to connect
Securely use https://www.askamith.wordpress.com/ instead.


3.3 Range Interval matching - {n1,n2}
T: 10-6-2004
RegEx: \d{1,2}[-\/]\d{1,2}[-\/]\d{2,4}
R: 10-6-2004


3.4 Preventing Over Matching using Lazy Quantifiers - *?
T: living in <b>SL</b> and <b>LK</b>.
RegEx: <b>.*</b>
R: living in SL and LK.
The reason for this is that metacharacters such as * and + are greedy; they look greatest possible match as opposed to the smallest. You can prevent this by using lazy versions of these quantifiers (they matches the fewest characters instead of the most).
T: living in <b>SL</b> and <b>LK</b>.
RegEx: <b>.*?</b>
R: living in SL and LK.


3.5 Match Word Boundaries
\b is used to match the start or end of a word.
T: The cat scattered his food all over the room.
RegEx: \bcat\b
R: The cat scattered his food all over the room.

RegEx Lesson 2 – Matching Several Characters – Quick Guide to Regular Expressions

2.1 Matching one of Several Characters
T: amith smith gmith
RegEx: [ag]mith
R: amith smith gmith


[Rr]eg[Ee]x ,Match both RegEx or regex.
2.2 Using Character set ranges

    [ns]a[0123456789] == [ns]a[0-9]
    • A-Z matches all uppercase characters from A to Z.
    • a-z matches all lowercase characters from a to z.

    note:
    When you use ranges, be careful not to provide an end range that is less than the start range (such as [Z-A]).

    Multiple ranges may be combined in a single set.
    [A-Za-z0-9] ,This pattern is shorthand for:
    [ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890]

    Eg: validation RegEx for css color codes;
    T: BGCOLOR="#336633" TEXT="#FFFFFF"
    RegEx: #[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]
    R: It will check the color code are in hexadecimal range.
    BGCOLOR="#336633" TEXT="#FFFFFF"


2.3 Anything but not this; (using the ^ metacharacter)
The pattern used in this example is the exact opposite of the one used previously. [0-9] matches all digits (and only digits). [^0-9] matches anything by the specified range of digits. As such,

[ns]a[^0-9]\.xls matches sam.xls but not na1.xls, na2.xls, or sa1.xls.


2.4 Dealing with meta characters with escaping(\):
There may be some special cases where metacharacters wont behave their normal way. For example it cant be used . to match. or [ to match [. So we have to specially pointed out what is the exact role of those meta characters in a Regular Expression.


    T: myArray[0]
    RegEx: myArray\[0\]
    R: myArray[0]


So any metacharacter can be escaped by preceding it with a blackslash (\).


2.5 Matching Whitespace Characters:
When you are performing regular expression searches, you’ll often need to match nonprinting whitespace characters within your text.
Metacharacter Description
[\b] Backspace
\f Form feed
\n Line feed
\r Carriage return
\t Tab
\v Vertical tab
Matching Specific Character types
\d Any digit (same as [0-9])
\D Any nondigit (same as [^0-9])
\w Any alphanumeric character in upper or lower case and underscore (same as [a-zA-Z0-9_])
\W Any nonalphanumeric or underscore character (same as [^a-zA-Z0-9_])
\s Any whitespace character (same as [\f\n\r\t\v])
\S Any nonwhitespace character (same as [^\f\n\r\t\v])

RegEx Lesson 1 – Matching Characters - Quick Guide to Regular Expressions

I want to remind you that RegEx contains plain text and expressions (can contain only plain text). RegEx are highly case sensitive.

1.1 Matching literal text:
Text: Hello, my name is Amith. Welcome
RegEx: Amith
Result: Hello, my name is Amith. Welcome


1.2 For Matching Any Character - .

    T : cat cnt sat
    RegEx: c.t
    R: cat cnt sat

1.3 Matching special characters
A . has a special meaning in regex. If you need a . in your pattern, you need a way to tell regex that you want the actual . character and not the regex special meaning of the . character. To do this, you escape the . by preceding it with a \(backslash) . \is a metacharacter (a fancy way of saying a character with a special meaning, in contrast to the character itself). Therefore, . means match any character, and \. means match the . character itself.

    T : cat cnt.a sat.txt
    RegEx: sat\.txt
    R: cat cnt.a sat.txt

Tuesday, July 12, 2011

C# Client Application to add new posts to blogspot.com (automated stuffs)

lang: c#

download: Google Data API 21.7mb

install the msi. normaly it contents several libraries. but for this app we need only using Google.GData.Client;
add reference to ur solution.


using System.Net;
using Google.GData.Client;


public static void Main()
{
string posttitle="How to make a cup of tea";
// body contains html
string body = "<p>sample paragraph<img src="http://www...."/><a href></a></p>";
//string[] label
string[] label={"tea","cup of tea","askamith"};

bool abc = AddPost(posttitle, body, label);

}



public static bool AddPost(string title, string bodyHTML, string[] labels)
{
Service service = new Service("blogger", "askamith-global");
service.Credentials = new GDataCredentials("your blogspot gmail-address", "password");
AtomEntry newPost = new AtomEntry();
newPost.Title.Text = title;
newPost.Content = new AtomContent();
newPost.Content.Content = bodyHTML;
newPost.Content.Type = "html";
foreach (string label in labels)
{
AtomCategory cat = new AtomCategory();
cat.Scheme = new Uri("http://www.blogger.com/atom/ns#");
cat.Term = label;
newPost.Categories.Add(cat);
}
AtomEntry response = null;
try
{
response = service.Insert(new Uri("http://www.blogger.com/feeds/xxxxxxxxxxxxx/posts/default"), newPost);
}
catch (GDataRequestException exception)
{
if (exception.ResponseString == "Blog has exceeded rate limit or otherwise requires word verification for new posts")
{
return false;
}
else
{
throw exception;
}
}
if (response == null)
{
throw new Exception("Something went wrong");
}
return true;
}



for new Uri("http://www.blogger.com/feeds/xxxxxxxxxxxxx/posts/default")

xxxxxxxxxxxxxxx= your blogger id (it will be shown in the url in adding a new post page in ur blogger dashboard)

http://www.blogger.com/post-create.g...xxxxxxxxxxxxxx

any questions regarding this. comment those bellow. its 100% working. try it. thanks

MySQL command Help – How to Remove Duplicate Data or Rows

How to remove duplicate data or rows from a MySQL data table by using a single command or a query?

If the output of a MySQL query contains duplicate data or rows, you can eliminate or remove those by using the DISTINCT keyword before the selected column name.

Mysql>
SELECT firstname FROM address;


Then you need unique firstname (remove duplicate firstnames):

Mysql>
SELECT DISTINCT firstname FROM address;


DISTINCT can be used with multiple columns also:

Mysql>
SELECT DISTINCT firstname,city FROM address;


>>Next thing is, if you want to permanent delete the duplicate data, you can use:

Mysql>>
DELETE FROM table1
USING table1,table1 AS vtable
WHERE (NOT table1.id=vtable.id)
AND (table1.field_name=vtable.filed_name);


This code tells mysql that there is a table called ‘table1’, then it tells that we use ‘table1’ and a new virtual table ‘vtable’ with the values of table1. Then 3rd row tells mysql to not to compare records itself. Then finaly we command there should not be records with the same field_name.