Thursday, March 13, 2008

Perl sort, use<->require, Browser passed HTTP header

1) perldoc -f sort
gives overview on sort function.

@sorted = sort {$a cmp $b} @str_list; # sort strings
@sorted = sort {$a <=> $b} @num_list; # sort nums
functions passed have to return -1,0,1 as '<=>' and 'cmp' operators do.

$a and $b are global variables, as described.

effects of application of numerical to strings:

$ perl -e '
> @s=("19","12","22","21","5");
> @s=map {++$_} @s;
> print @s; print "\n";
> print sort {$b cmp $a} @s;
> '
201323226
623222013
~$ perl -e '
@s = (19,12,22,21,5);
@s=map {++$_} @s;
print @s; print "\n";
print sort {$b <=> $a} @s;
'
201323226
232220136
$perl -e '
@s=("19","12","22","21","005");
@s=map {++$_} @s;
print @s; print "\n";
print sort {$b cmp $a} @s;
'
20132322006
23222013006
$perl -e '
@s=("19","12","22","21","005");
@s=map {++$_} @s;
print @s; print "\n";
print sort {$b <=> $a} @s;
'
20132322006
23222013006


bold marked are different and interesting.

2) use and require:
Here are c++ rough equivalents for this keywords:

# require:
# perl
require One::Two;
// C++
#include <One/Two>
---------%<------------
# use:
# perl:
use One::Two
from_two_func();
// C++
#include <One/Two> // all functions in Two are in the namespace Two
using namespace Two; //


3) Looking for browser passed parts in HTTP header.
Check CGI.pm description.
this parts are in $ENV{'[KEY]'} array.
for example, for key [KEY] == REQUEST_METHOD values = POST, GET, PUT...

No comments: