Wednesday, January 9, 2008

Checking the Existence of an Element

You can use the defined() function to check if an array element exists before you assign a value to it. This ability is very handy if you are reading values from a disk file and don't want to overlay values already in memory. For instance, suppose you have a disk file of Jazz Artist addresses and you would like to know if any of them are duplicates. You check for duplicates by reading the information one address at a time and assigning the address to an associative array using the Jazz Artist name as the key value. If the Jazz Artist name already exists as a key value, then that address should be flagged for follow up.

Because we haven't talked about disk files yet, we'll need to emulate a disk file with an associative array. And, instead of using Jazz Artist's address, we'll use Jazz Artist number and Jazz Artist name pairs. First, we see what happens when an associative array is created and two values have the same keys, element1.pl.

createPair("100",  "Miles Davis");

createPair("200", "Keith Jarrett");

createPair("100", "John Coltrane");



while (($key, $value) = each %array) {

print("$key, $value\n");

};



sub createPair{

my($key, $value) = @_ ;



$array{$key} = $value;

};

This program prints:

100, John Coltrane

200, Keith Jarrett

This example takes advantages of the global nature of variables. Even though the %array element is set in the createPair() function, the array is still accessible by the main program. Notice that the first key, value pair (100 and Miles Davis) are overwritten when the third key, value pair is encountered. You can see that it is a good idea to be able to determine when an associative array element is already defined so that duplicate entries can be handled. The next program does this, element2.pl.

createPair("100",  "Miles Davis");

createPair("200", "Keith Jarrett");

createPair("100", "John Coltrane");

while (($key, $value) = each %array) {

print("$key, $value\n");

};



sub createPair{

my($key, $value) = @_ ;



while (defined($array{$key})) {

$key++;

}



$array{$key} = $value;

};

This program prints:

100, Miles Davis

101, John Coltrane

200, Keith Jarrett

You can see that the number for John Coltrane has been changed to 101.

Monday, January 7, 2008

How to Create a Bullet List in Microsoft Excel

To Add a Bullet to an Existing Text Entry

1.Position the insertion point at the beginning of your text entry.
2.Type a symbol that you want to use as a bullet. To create the bullet character, press ALT+0149 (type 0149 on the numeric keypad).

You may want to include a space after the character so that the bullet will not be next to the text. Note that the bullet is an extended character and may not be available with all fonts.

Some other examples of characters you can use include: >, /, ~, !, and others.


To Create a Custom Text Format That Includes Bullets

1.Select the cell or range of cells that you want to apply bullets to.
2.On the Format menu, click Cells.
3.On the Number tab, click the Text category, and then click the Custom category.

Microsoft Excel places an at sign (@) in the Type box.
4.In the Type box, place the insertion point before the @, and type the symbol that you want to use as a bullet. To create the bullet character, press ALT+0149 (type 0149 on the numeric keypad).

You may want to include a space after the symbol so that the bullet will not be next to the text. Note that the bullet is an extended character and may not be available with all fonts.

Some other examples of characters you can use include: >, /, ~, !, and others.
5.Click OK.

Friday, January 4, 2008

installing module

> perl Makefile.PL
> make
> make test
> make install
perl -MCPAN -e 'install '

isArray() in Perl

foreach $item (@array){
if(ref($item) eq 'ARRAY'){
#It's an array reference...
#you can read it with $item->[1]
#or dereference it uisng @newarray = @{$item}
}else{
#not an array in any way...
}
}

ordering hash

use Tie::IxHash;
tie %HASH, "Tie::IxHash";
# manipulate %HASH
@keys = keys %HASH; # @keys is in insertion order

#Tie::InsertOrderHash does not allow you to change values in your hash and keep the original ordering. Thus, as mentioned above, updating the days in February will cause February to now appear after December when listing the keys.
#If you want to be able to change values without changing the ordering then Tie::IxHash may be a better option.

delete duplicate hash value's

my %seen;
for my $key (keys %students) {
my $value_key = "@{[values %{$students{$key}}]}";
if (exists $seen{$value_key}) {
delete $students{$key};
}
else {
$seen{$value_key}++;
}
}
undef %seen;

Finding oldest file in directory

my $xml_file = (reverse sort{(stat $a)[10] cmp (stat $b)[10]} glob "$dir_name/*.*")[0];