Wednesday, November 30, 2011

Sunday, November 13, 2011

Perl - [ Seconds To Hours ]

 #!/usr/bin/perl
use strict;
use warnings;

my $seconds = shift or die "Entre the seconds parameter\n";
my @parts = gmtime($seconds);
printf ("day -%4d\t hours - %4d\tminute - %4d\tseconds - %4d\n",@parts[7,2,1,0]);


$perl secondsTohours.pl 89600
day -   1        hours -    0   minute -   53   seconds -   20
$perl secondsTohours.pl 3600
day -   0        hours -    1   minute -    0   seconds -    0

Perl - [ Second TO Hours [Sub] ]


#!usr/bin/perl
use strict;
use warnings;

my $value = shift or die "Enter the second parameter\n";

&secondTOhours(\$value);

sub secondTOhours(){
 
    my ($seconds)= @_;

    my $hrs = int( $$seconds / (60*60) );

    my $min = int( ($$seconds - $hrs*60*60) / (60) );

    my $sec = int( $$seconds - ($hrs*60*60) - ($min*60) );

    print("$hrs hours, $min minutes, and $sec seconds.\n");
}


$perl secondsTohours.pl 3600

1 hours, 0 minutes, and 0 seconds.


Thursday, November 3, 2011

Perl - [ Reference | Array Sub ]


#!/usr/bin/perl
use strict;
use warnings;

my @array = [1,2,3,['a','b',['c']]];
my $rA = \@array;

print"\n[0] = [1,2,3,['a','b',['c']]]";
print"\n[3] = ['a','b',['c']]";
print"\n[2] = ['c']";
print"\n[0] = c";

print "\nResp : $rA->[0][3][2][0]\n";

my @des = [ ['andre','21','PA'],
            ['ela','-','SP']];

my $rD = \@des;
    print $rD->[0][0][0];

result


$ perl line.pl

[0] = [1,2,3,['a','b',['c']]]
[3] = ['a','b',['c']]
[2] = ['c']
[0] = c
Resp : c
andre


Perl - [ Captura Regex ]

#!/usr/bin/perl

use strict;
use warnings;

my $capitura = 'From: gnat@perl.com To: camelo@oreilly.com Date: Mon 17 Jul 200 09:00:00 -100 Subject: Nada';
$capitura =~ /(From:(.*)To:(.*)\s{1}Date:(.*)\s{1}Subject:(.*))/;
print "$1\n$2\n$3\n$4\n$5";

result


$perl  commandLine.pl
$1  # From: gnat@perl.com To: camelo@oreilly.com Date: Mon 17 Jul 200 09:00:00 -100 Subject: Nada
$2  # gnat@perl.com
$3  # camelo@oreilly.com
$4  # Mon 17 Jul 200 09:00:00 -100
$5  # Nada


Perl - [Character class | Change | Replacement | For ]


#!/usr/bin/perl
use strict;
use warnings;

my $name = ' texto gerado automaticamente pelo sistema lattes ';
my $name1;

($name1 = $name) =~ s/o/OoO/g;

print "$name\n$name1\n";

for ($name){
    s/^\s+/+/;
    s/\s+$/++/;
    s/\s+/-/g;
}
print "$name\n";

my @names = ('comand','   ','line');

print "@names\n";

tr/ /./ for @names;

print "@names\n";

my ($num,$string,$space) = (10,'line',' ');

print "numero positive - $num \n"   if ($num =~ /\d/);
print "string positive - $string \n"    if ($string =~ /\w/);
print "space  positive - $space \n" if ($space =~ /\s/);

print "numero positive - $num \n"   if ($num =~ /\p{IsDigit}/);
print "string positive - $string \n"    if ($string =~ /\p{IsWord}/);
print "space  positive - $space \n" if ($space =~ /\p{IsSpace}/);

print "numero negation - $num \n"   if ($num =~ /\D/);
print "string negation - $string \n"    if ($string =~ /\W/);
print "space  negation - $space \n" if ($space =~ /\S/);

print "numero negation - $num \n"   if ($num =~ /\P{IsDigit}/);
print "string negation - $string \n "if ($string =~ /\P{IsWord}/);
print "space  negation - $space \n" if ($space =~ /\P{IsSpace}/);

results 

$perl  blog.pl
 texto gerado automaticamente pelo sistema lattes 
 textOoO geradOoO autOoOmaticamente pelOoO sistema lattes 
+texto-gerado-automaticamente-pelo-sistema-lattes++
comand     line
comand ... line
numero positive - 10 
string positive - line 
space  positive -   
numero positive - 10 
string positive - line 
space  positive - 

Tuesday, November 1, 2011

Perl - [ Relational operators ]



numeric                     string
  >                                gt
  >=                             ge
  <                                lt
 <=                              le
  ==                             eq
  !=                              ne
<=>                             cmp