Dynamic Array
This will dynamically create a table with unlimited number of rows and definable columns across#!/bin/perl
my $limit = 0;
my $max = scalar(@array)+1;
my $i = 0;
my $j = 0;
my $k = $limit;
do {
@{"$i"} = @array[$j..$k];
foreach $line (@{"$i"}) {
print "$line\n" unless $seen{$line}++;
}
$j = (($i * $limit) +$i);
$k = ($j + $limit);
$i++;
} until ( "$i" eq "$max" );
Difference | intersection | union of two Arrays
Reference: Perl Cookbook 2nd Edition, Page 128 -130, Section 4.9 - Indirect solution#!/bin/perl
# Read in the entire directory into the first array
opendir(DIR, "$path") || "Can't open: $!\n";
@fArray=grep(!/^\.\.?$/, sort {-M $b <=> -M $a} readdir(DIR));
closedir(DIR );
# the first array
# create an array with specific files of interest
foreach $pattern(@fArray){
if($pattern =~ /^n[0-9]*\.tmp/) {
$pattern =~ s/n//;
$pattern =~ s/\.tmp//g;
push(@nArray,$pattern);
}
}
# the second array
my @sArray = ($start .. $stop);
# clear the arrays and hash
@union = @intersection = @difference = ();
%count = ();
# create an hash of all the elements in both arrays
foreach $element (@sArray, @nArray) { $count{$element}++ }
# cycle through each element in the hash
foreach $element (keys %count) {
# create an array with elements of both arrays
push @union, $element;
# create an array with unique element that are in both arrays (intersection)
# create an array with elements that in the FIRST array (difference)
push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element;
}
# sort the array numerically
@difference = sort {$a <=> $b} (@difference);
Difference between two arrays
Reference: Perl Cookbook 2nd Edition, Page 126 -128, Section 4.8 - Loopless version#!/bin/perl
opendir(DIR, "$path") || "Can't open: $!\n";
@fArray=grep(!/^\.\.?$/, sort {-M $b <=> -M $a} readdir(DIR));
closedir(DIR );
foreach $pattern(@fArray){
if($pattern =~ /^n[0-9]*\.tmp/) {
$pattern =~ s/n//;
$pattern =~ s/\.tmp//g;
push(@nArray,$pattern);
}
}
my @sArray = ($start .. $stop);
my %seen;
my @notseen;
# create a hash with the keys set to the elements of @sArray
@seen{@sArray} = ();
# remove any element from the hash %seen with any element that matches in @nArray
delete @seen{@nArray};
# create an array with elements not found in @nArray
my @notseen = keys %seen;
Regular Expression
Email Address
#!/bin/perlif($line !~ /[@][a-z0-9A-Z]*\.[a-z0-9A-Z]/) {
Copyrights
#!/bin/perlif($line !~ m/(copyright +(©|\(c\)|©) +\d{4})( *[-,] *\d{4})*/) {
Perl function equivalent of PHP's AddSlashes()
Here is a perl equivalent of PHP's AddSlashes() function. It's a quick an dirty way to clean up text to insert into a database. There are better ways to do this. It should exactly mimic PHP's function. It adds slashes before single quotes('), double-quotes(”), backslashes(\), and NULL bytes (\0).#!/bin/perl
sub AddSlashes {
$text = shift;
## Make sure to do the backslash first!
$text =~ s/\\/\\\\/g;
$text =~ s/'/\\'/g;
$text =~ s/"/\\"/g;
$text =~ s/\0/\\\0/g;
return $text;
}
Installed modules
Find all installed modulesperl -MCPAN -e 'print CPAN::Shell->r '
No comments:
Post a Comment