Tuesday, July 29, 2008

Regular Expression


/^.+[^\.].*\.[a-z]{2,}$/i

It is used to check FQHN (fully qualified host name). It is understood as -

^ begin with
.+ one or more characters
[^\.] one character not '.' dot (that is why a.ca wrong, and ab.ca correct)

.* zero or more characters (to ignore all dots in the middle and focus on last one only)
\. dot (actually last dot)
[a-z]{2,} two or more letters in the alphabet
$ end of the string

For instance, a.ca (NO), ab.ca (YES), ab.a.ca (YES)

Parsing Regexp is recursive for best match, not just parsed once


String str = "serial 5555555555555555 roger test5 AP3610";
Pattern pattern = Pattern.compile("serial[ ](\\S+)[ ](.*)[ ](\\S+)");
Matcher matcher = pattern.matcher(str);

if (matcher.find()) {
for (int i = 0; i <= matcher.groupCount(); i++) {
System.out.println("Group" + i + ":" + matcher.group(i));
}
}

// The result is:
// Group0:serial 5555555555555555 roger test5 AP3610
// Group1:5555555555555555
// Group2:roger test5
// Group3:AP3610

\S A non-whitespace character
Note that group2 can contains whitespace.

Wednesday, July 23, 2008

assigned by value?


$myArray = array('key1'=>array('1', '2'), 'key2'=>array('3', '4'));
$subArray = $myArray['key1'];
array_push($subArray, '5');

The trick is that new value '5' is ONLY added to $subArray, but not to $myArray['key']. PHP's assignment operator '=' assigns value by copy. So $subArray is not really a sub-array of $myArray, it is a separate array. Assignment by reference is also supported, using the $var = &$othervar; syntax.

The same for the 'foreach' loop

foreach($myArray as $subArray)

As of PHP 5, foreach ($arr as &$value) is supported. This will assign reference instead of copying the value.

Monday, July 21, 2008

Utility Functions

1. Sorting with user defined comparator

In PHP -


usort($bp_radios, create_function('$a, $b',
'return $a["radio_index"] - $b["radio_index"];'));


In JS -

tabRF_apList[i]['radios'].sort(function(a,b) {
return a['radio_index'] - b['radio_index']});

2. Array <=> String with delimiter

In PHP -

$radio = implode('/', $radioNameArray); // {'a', 'b'} => 'a/b'
$radioNameArray = explode('/', $radio);

In JS -

var radio = radioNameArray.join('/');
var radioNameArray = radio.split("/");

Tuesday, July 15, 2008

"" == 0

An empty string is a length 1 array containing '\0' (which is equal to zero). Javascript automatically casts single length strings to characters and vice versa. '\0' == 0 therefore "" == 0.

To strictly compare, use === operator.

PHP:


if (strpos($S_COMPANY_NAME,"Extreme") === false)

strpos return false if not found, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". So we have to use === to test it.

Wednesday, July 9, 2008

Overloading

In Javascript. As for differing amounts of arguments, Javascript really doesn't care about that either.

function bla(a, b, c)
is pretty much the same as
function bla()
except that in the former, a = arguments[0], b = arguments[1], c = arguments[2] in the namespace of the function.

PHP function support variable-length argument list too. And we can set default argument value.


function getFormatedRadioMode($radioMode, $showoff = true) {
   global $ap_radioModeTable;
   ...
}

Tuesday, July 8, 2008

dynamic variable

This article talks about different ways to compose dynamic Javascript variable -


eval('name' + i + ' = "Marco"'); // compose a string with variable as eval's argument
window['name' + i] = 'Marco';

And the PHP counterpart is:

${'name'.$i} = 'Marco';
echo 'ap_bVNSRadio_${vname}';