Tuesday, July 1, 2008

LEARN PERL(CLASS 2)

Arrays
Looping thru an Array, and examining each value
foreach (@some_array){
print $_; # the value is in $_ by default
}


Associative Arrays (Hashes)
Adding a key/value pair to an hash
$hashname{key}{value} = "newvalue";
Keeping a running count of the times a string is equal to a certain value
$different_strings{$a_string} = $different_strings{$a_string}++;
Looping thru an Associative Array, and examining each value
foreach (@some_assoc_array){
print $_; # the value is in $_ by default
}
Looping thru an Associative Array, and printing each key and value
foreach $key keys(%hash_1) {
print "$key => $hash_1{$key}\n";
}
Print the keys of a Associative Array in sorted order (alphabetical/ascending)
foreach $key (sort (keys(%hash_1))) {
print "$key => $hash_1{$key}\n";
}
Testing if a Associative Array has a key/value
if ($some_assoc_arry{$the_key} eq "" ) {
print "key not in array";
}
Testing if a Associative Array has any key/value. Is it empty?
if (%some_assoc_array) {
print "Hash is not empty. It has data!";
}

Hashes of Arrays
%families = (
smith => [ "jim", "bob" ],
jones => [ "roger", "jan", "roy" ],
kent => [ "mark", "mag", "bert" ],
);
Add another Array to the hash of arrays
$families{bluejean} = [ "norma jean", "jack", "pam", "port" ];
Append new members to an existing existing array in the hash

push @{ $families{smith} }, "jen", "jack";
Accessing (changing) the first element of an array in the hash
# Change "jim" to "Jimmy"
$families{smith}[0] = "Jimmy";
You can print all of the families in the arrays by looping through the keys of the hash
for $family ( keys %families ) {
print "$family => @{ $families{$family} }\n";
}
Sort the arrays in the hash by how many elements they have
for $family ( sort { @{$families{$b}} <=> @{$families{$a}} } keys %families ) {
print "$family => @{ $families{$family} }\n"
}
Loop thru the hash of arrays. Sort on the second value (name) in the arrays. Print the key and only values 0 and 1.
for $family ( sort { $families {$b}[1] cmp $families{$a}[1] } keys %families ) {
print "Key => $family\n
Value0 => $families{$family}[0]\n
Value1 => $families{$family}[1]\n";
}


IF Statement
IF statement
if ( $string eq "Hi There" )
if ( "A" gr "B" )
if ( $num == 10)
if ( $num != 10)
if ( $num > 10)
if ($x > 208 && $x <> 64 && $y < 78 ) # && is AND, is OR


Subroutines
Subroutine example
# create subroutine
sub write_current_date
{
}

# Run subroutine
write_current_date ();


Reports
Print a formatted print line-
#Define the Header Line
format STDOUT_TOP=
Name Address Num
----------- ---------------------- -----
. #Dot marks end of format

#Define the detail Line
format STDOUT =
@<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<<@######



Network
Network example

use Socket;

# use port 9999 as default
$port = shift 9999;

# create a socket, make it reusable
socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";

# grab a port on this machine
$paddr = sockaddr_in($port, inet_aton("127.0.0.1"));

# bind to a port, then listen
bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
accept(CLIENT, SERVER);

select CLIENT; #select filehandle
$ = 1; #make filehandle hot so data shows up unbuffered

print CLIENT "\ntest\n";

# telnet to port 9999 on localhost to see your printed word above.



Misc
Checking if system call failed or not.
@args = ("wget","-qN","-T15");
$rc = 0xffff & system @args;
if ($rc != 0) {
printf "system(%s) returned %#04x: ", "@args", $rc;
die ("\nProgram did not exit correctly. Try a test manualy.\n");
}
Tag this post with del.icio.us! Digg Me! Reddit this!
Related stories
· Perl script to output block stats from PF log file (1 shared tags)
· Perl script to check for OpenBSD package updates (1 shared tags)
· Doing perl search and replace from the commandline (1 shared tags)
· Shell oneliners (1 shared tags)
· Fixing Sqlite error "unable to open database file" (1 shared tags)

No comments: