Monday, September 1, 2008

Loops in Perl

While

- while (test_expr)
{
statement(s); # Executed while test_expr is true
}

- Both the {} and the () are required!

- The test expression is evaluated and, if true, the statement block
is executed. This continues until the test expression is false.

- Note that the loop body may never be executed

- A compound statement

- Ex.

$i = 1;
while ($i <= 10)
{
print ("The square of $i is ", $i*$i, "\n");
$i++;
}


Until

- until (test_expr)
{
statement(s); # Executed while test_expr is false
# (Executed until test_expr is true)
}

- The test expression is evaluated and, if false, the statement block
is executed. This continues until the test expression is true.

- Note that the loop body may never be executed

- A compound statement

- Ex.

$i = 1;
until ($i > 10)
{
print ("The square of $i is ", $i*$i, "\n");
$i++;
}


Do Operator Applied To A Block

- do
{
statement(s);
}

- The {} are required!

- The statement block is executed.

- Returns the value of the last statement executed in the statement
block

- Allows the use of a statement block where an expression is required

- Can be modified with a while or until to act as a loop


Do-While

- do
{
statement(s); # Executed while test_expr is true
} while (test_expr);

- Only the {} are required!

- The statement block is executed. Then the test expression is
evaluated and, if true, the statement block is executed. This
continues until the test expression is false.

- Note that the loop body is executed at least once

- A simple statement! (Really just a modified Do-BLOCK.)

- Since it is just a simple statement, the loop control commands
(last, next, redo) desscribed later can NOT be used in a
do-while

- Ex.

$i = 1;
do
{
print ("The square of $i is ", $i*$i, "\n");
$i++;
} while ($i <= 10);


Do-Until

- do
{
statement(s); # Executed while test_expr is false
} until (test_expr);

- Only the {} are required!

- The statement block is executed. Then the test expression is
evaluated and, if false, the statement block is executed. This
continues until the test expression is true.

- Note that the loop body is executed at least once

- A simple statement! (Really just a modified Do-BLOCK.)

- Since it is just a simple statement, the loop control commands
(last, next, redo) desscribed later can NOT be used in a
do-until

- Ex.

$i = 1;
do
{
print ("The square of $i is ", $i*$i, "\n");
$i++;
} until ($i > 10);


Expression Modifiers

- Can use while and until as expression modifiers

- Similar to the while and until statement except that only an
expression can be modified, NOT a statement block

- Result is a simple statement


While Modifier

- exec_expr while test_expr;

- Exec_expr executed while the test_expr is true

- Equivalent to: while (test_expr)
{
exec_expr;
}

- The () around the test expression are NOT required here!

- The test expression is evaluated first

- A simple statement

- Ex.

# Print 10 down to 1.

$x = 11;
print "$x\n" while (--$x > 0);

# An infinite loop!

print ("Possible infinite loop here!\n") while ($x < 21);


Until Modifier

- exec_expr until test_expr;

- Exec_expr executed until the test_expr is true

- Equivalent to: until (test_expr)
{
exec_expr;
}

- The () around the test expression are NOT required here!

- The test expression is evaluated first

- A simple statement

- Ex.

# Print 10 down to 1.

$x = 11;
print "$x\n" until (--$x == 0);


For

- for (initial_expr; test_expr; increment_expr)
{
statement(s);
}

- Both the {} and the () are required!

- The initial expression is evaluated first (and only once). Then
the test expression is evaluated and, if true, the statement block
is executed. Then the increment expression is evaluated and the
test expression re-evaluated. This continues until the test
expression is false.

- Note that the loop body may never be executed

- Equivalent to:

initial_expr;
while (test_expr)
{
statement(s);
increment_expr;
}

- A compound statement

- Ex.

for ($i = 1; $i <= 10; $i++)
{
print ("The square of $i is ", $i*$i, "\n");
}


Foreach

- foreach $var (@list)
{
statement(s);
}

- Both the {} and the () are required!

- The $var variable is assigned the first value in the list @list
and the statement block executed. This is repeated for each value
in the list.

- If $var is omitted, the special default variable, $_, is used

- Note that the loop body is not executed if the list is the empty
list

- A compound statement

- Ex.

foreach $i (1..10)
{
print ("The square of $i is ", $i*$i, "\n");
}


Array Element Modification With A Foreach

- If the list used in the foreach statement is a single array
variable, you can modify each element of the array by
modifying $var each time through the loop

- Why? Because in this case, $var is really a reference to
the array element and not a copy of it.

- Ex.

foreach $x (@list)
{
# Add a newline to each element.
$x .= "\n";
}


Labeled Block

- Block with an associated name or label

- Label is an identifier similar to a variable name, but without
any special prefix character

- Recommended that labels be all uppercase to avoid conflict with
reserved words

- Label goes immediately in front of the statement containing the
block followed by a colon

- Labels have their own namespace

- Ex.

LOOP: foreach $i (1..10)
{
print ("The square of $i is ", $i*$i, "\n");
}


Last Operator

- Breaks out of the innermost enclosing loop block if used without
a label, or the specified block if used with a label

- Similar to the C break statement

- If the specified loop contains a continue block, it is skipped

- Only the while, until, for and foreach statement is considered
a loop by the last operator

- BUT a block by itself (a block which is NOT part of a larger
construct, such as a while loop or if-else statement) is
considered a loop that executes once, and the last operator can
be used to exit the block. This type of block is called a
"naked" block.

- Ex.

while (test_expr)
{
statement(s);
if (expr)
{
statement(s);
last; # Break out of the while loop
}
statement(s);
}


Next Operator

- Causes the rest of the specified loop to be skipped

- Similar to the C continue statement

- If the specified loop contains a continue block, it is executed
and then the loop conditional is evaluated

- Only the while, until, for and foreach statement is considered
a loop by the next operator

- The next operator can be used to exit a naked block. (The
difference between the next operator and the last operator in
this case, is that the last operator skips any continue block,
the next operator executes it.)

- Ex.

while (test_expr)
{
statement(s);
if (expr)
{
statement(s);
next; # Start next iteration, evaluate
# test_expr first
}
statement(s);
}


Redo Operator

- Causes the rest of the specified loop to be skipped

- BUT the loop conditional is NOT evaluated prior to the start of
the next iteration

- If the specified loop contains a continue block, it is NOT
executed

- Only the while, until, for and foreach statement is considered
a loop by the redo operator

- The redo operator can be used to restart a naked block.

- Ex.

while (test_expr)
{
statement(s);
if (expr)
{
statement(s);
redo; # Start next iteration, do NOT evaluate
# test_expr first
}
statement(s);
}


Continue Block

- Both the while loop and the until loop can also have a
continue block

- If a continue block exists, it is always executed before
the loop conditional expression is evaluated again

- Ex.

while ($line = )
{
chop $line;
next if ($line eq "END");
# Other Processing here.
}
continue
{
print "$line\n";
}

The input line is always printed, even if the next statement
is executed, since the continue block is always executed
before the loop conditional is evaluated again.

Friday, August 29, 2008

Blog in Tamil

செந்தில் ஒரு புதிய திட்டத்துக்கு தயாராகிரான். அது blog in தமிழ்.

வாழ்த்துகள், செந்தில்............

செந்தில் இனி வரும் காலங்கலில் ஆங்கிலம் பயன் படுத்துவதை குறைத்துவிடுவான்........

Friday, August 1, 2008

PicoP: Better Viewing Experiences from Mobile Devices

Microvision is working with business partners to enable better viewing experiences for mobile device consumers. Sharing photos, watching movies, and giving presentations using the small screens of today’s devices limits our ability to imagine, entertain, and share.

PicoP is an ultra miniature projection module capable of producing full color, high-resolution images but small enough and low power enough to be embedded directly into mobile devices such as cell phones, portable media players, digital cameras, portable computers and more.


For manufacturers who wish to bring to market next generation mobile devices, Microvision provides a PicoP display engine that can meet your exacting OEM requirements. PicoP display engines are engineered for OEMs and made available through our supply chain partners to meet high volume production needs.
Mobile Device with PicoP Display Engine: Mobile devices such as cell phones, portable media players, digital cameras, and laptops can be enabled with pico projection capabilities turning photos, videos, and other content into big viewing experiences that can be shared with others. Embedded pico projectors leverage Microvision's PicoP display engine which at its heart, contains Microvision's patented MEMS scanner. Other technology components include, laser light sources, optics, and electronics. These components are brought to life using Microvision’s proprietary software and expertise.

Wednesday, July 30, 2008

Different valid uses of Pointers

The following examples help to distinguish between the use of a pointer and of the pointer's value:

void main()
{
int *p, *q;

p = (int *)malloc(sizeof(int));
q = p;
*p = 10;
printf("%d\n", *q);
*q = 20;
printf("%d\n", *q);
}

The final output of this code would be 10 from line 4 and 20 from line 6. Here's a diagram:


The following code is slightly different:

void main()
{
int *p, *q;

p = (int *)malloc(sizeof(int));
q = (int *)malloc(sizeof(int));
*p = 10;
*q = 20;
*p = *q;
printf("%d\n", *p);
}

The final output from this code would be 20 from line 6. Here's a diagram:


Notice that the compiler will allow *p = *q, because *p and *q are both integers. This statement says, "Move the integer value pointed to by q into the integer value pointed to by p." The statement moves the values. The compiler will also allow p = q, because p and q are both pointers, and both point to the same type (if s is a pointer to a character, p = s is not allowed because they point to different types). The statement p = q says, "Point p to the same block q points to." In other words, the address pointed to by q is moved into p, so they both point to the same block. This statement moves the addresses.

From all of these examples, you can see that there are four different ways to initialize a pointer. When a pointer is declared, as in int *p, it starts out in the program in an uninitialized state. It may point anywhere, and therefore to dereference it is an error. Initialization of a pointer variable involves pointing it to a known location in memory.

1. One way, as seen already, is to use the malloc statement. This statement allocates a block of memory from the heap and then points the pointer at the block. This initializes the pointer, because it now points to a known location. The pointer is initialized because it has been filled with a valid address -- the address of the new block.

2. The second way, as seen just a moment ago, is to use a statement such as p = q so that p points to the same place as q. If q is pointing at a valid block, then p is initialized. The pointer p is loaded with the valid address that q contains. However, if q is uninitialized or invalid, p will pick up the same useless address.

3. The third way is to point the pointer to a known address, such as a global variable's address. For example, if i is an integer and p is a pointer to an integer, then the statement p=&i initializes p by pointing it to i.

4. The fourth way to initialize the pointer is to use the value zero. Zero is a special values used with pointers, as shown here:

p = 0;

or:

p = NULL;

What this does physically is to place a zero into p. The pointer p's address is zero. This is normally diagrammed as:


Any pointer can be set to point to zero. When p points to zero, however, it does not point to a block. The pointer simply contains the address zero, and this value is useful as a tag. You can use it in statements such as:

if (p == 0)
{
...
}

or:

while (p != 0)
{
...
}

The system also recognizes the zero value, and will generate error messages if you happen to dereference a zero pointer. For example, in the following code:

p = 0;
*p = 5;

The program will normally crash. The pointer p does not point to a block, it points to zero, so a value cannot be assigned to *p. The zero pointer will be used as a flag when we get to linked lists.

The malloc command is used to allocate a block of memory. It is also possible to deallocate a block of memory when it is no longer needed. When a block is deallocated, it can be reused by a subsequent malloc command, which allows the system to recycle memory. The command used to deallocate memory is called free, and it accepts a pointer as its parameter. The free command does two things:

1. The block of memory pointed to by the pointer is unreserved and given back to the free memory on the heap. It can then be reused by later new statements.
2. The pointer is left in an uninitialized state, and must be reinitialized before it can be used again.

The free statement simply returns a pointer to its original uninitialized state and makes the block available again on the heap.

Dynamic Data Structures: The Heap

A typical personal computer or workstation today has somewhere between 16 and 64 megabytes of RAM installed. Using a technique called virtual memory, the system can swap pieces of memory on and off the machine's hard disk to create an illusion for the CPU that it has much more memory, for example 200 to 500 megabytes. While this illusion is complete as far as the CPU is concerned, it can sometimes slow things down tremendously from the user's perspective. Despite this drawback, virtual memory is an extremely useful technique for "increasing" the amount of RAM in a machine in an inexpensive way. Let's assume for the sake of this discussion that a typical computer has a total memory space of, for example, 50 megabytes (regardless of whether that memory is implemented in real RAM or in virtual memory).

The operating system on the machine is in charge of the 50-megabyte memory space. The operating system uses the space in several different ways, as shown here:


The operating system and several applications, along with their global variables and stack spaces, all consume portions of memory. When a program completes execution, it releases its memory for reuse by other programs. Note that part of the memory space remains unused at any given time.

This is, of course, an idealization, but the basic principles are correct. As you can see, memory holds the executable code for the different applications currently running on the machine, along with the executable code for the operating system itself. Each application has certain global variables associated with it. These variables also consume memory. Finally, each application uses an area of memory called the stack, which holds all local variables and parameters used by any function. The stack also remembers the order in which functions are called so that function returns occur correctly. Each time a function is called, its local variables and parameters are "pushed onto" the stack. When the function returns, these locals and parameters are "popped." Because of this, the size of a program's stack fluctuates constantly as the program is running, but it has some maximum size.

As a program finishes execution, the operating system unloads it, its globals and its stack space from memory. A new program can make use of that space at a later time. In this way, the memory in a computer system is constantly "recycled" and reused by programs as they execute and complete.

In general, perhaps 50 percent of the computer's total memory space might be unused at any given moment. The operating system owns and manages the unused memory, and it is collectively known as the heap. The heap is extremely important because it is available for use by applications during execution using the C functions malloc (memory allocate) and free. The heap allows programs to allocate memory exactly when they need it during the execution of a program, rather than pre-allocating it with a specifically-sized array declaration.

Monday, July 28, 2008

Create Shortcut to Hibernate Windows XP instead of Shutdown

Hibernate is great because it saves the status of your Windows XP session (all the programs and documents you have open) to the hard drive, so it can automatically restore it the next time you power up. The first step to making a Hibernate shortcut is to make sure you have Hibernate turned on. Go to the Control panel then click Performance and maintenance. Next, choose Power options, and select the hibernate tab. Finally, make sure Enable hibernation is checked.

Now to actually make the shortcut right click your desktop and choose New | Shortcut. Next, type this case-sensitive command into the dialog box

Code:
rundll32.exe powrprof.dll,SetSuspendState Hibernate

click next. Give the shortcut a name like Hibernate and click finish. From now on all you need to do to enter hibernation is double click the shortcut.

Alos you can put this shortcut to Windows task scheduler so that system Hibernate can be achived at a particular time automatically.

What not to do at a workplace

Angry employees can waste time worrying over conflicts. Keeping the peace in the workplace is imperative to a good day at work.

While colleague romances may litter offices with disgruntled exes and unbearably cute sweethearts, hostility and dislike also leave the workplace reeling. Angry employees can waste time worrying over conflicts, and even lead to office violence.
John Challenger, chief executive of outplacement firm Challenger, Gray & Christmas Inc., offers the following tips on keeping the peace at work:
-Get together with co-workers outside the workplace. Fortifying an office bond with out-of-office activities helps build respect and strengthens relationships among officemates, said Challenger.
Also Watch:
-Don’t embarrass or yell at colleagues in front of others. Make conflicts private.
-Allow employees to openly discuss issues in order to avoid pent-up resentment. Challenger suggests encouraging written complaints and suggestions and calling follow-up meetings to discuss grievances.
-Personalize work spaces with homey touches. A comfortable environment helps cut down on tension.
-Be civil to unlikeable co-workers and generally considerate in the workplace, especially in shared spaces.
-Actually use vacation days. Time away can help ease stress and interoffice tensions.
-Don’t gossip maliciously or "BCC" e-mails.
-Don’t steal credit from co-workers or miss deadlines. Respond promptly to e-mails and always come prepared and on time to meetings.