Tuesday, October 18, 2011

Perl - [ References /pointers variables ]


Anyone familiar with the C language (and other), you know whatpointers are and how programming can become proficient with its use. References are pointers to previously defined data types(whether it's scalars, arrays or hashes). Through references it is possible to obtain the contents of a scalar variable, an array or ahash the same as using the original name.
------------------------------------------------------------------------------------------------------------------------------
#!/usr/bin/perl

use strict;
use warnings;

my (%HoA,$valor,@nome);
#References scalar ($)
$valor = 10;
my $rvalor = \$valor;
print "$$rvalor\n";

#References array (@)
@nome = ('andre','ayres');
my $rnome = \@nome;
print "$$rnome[0]\n";



#References hash (%)

@{$HoA{'1'}} = (1..10);
print join("\t",@{$HoA{1}}) , "\n";

my $rHoA = \%HoA;
print join ("\t",@{$rHoA->{1}});

result 

$perl tmp.pl 

10
andre
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10

Friday, October 14, 2011

Perl - ( if ) [ testing if uninitialized variable! ]


#!/usr/bin/perl

use strict;
use warnings;
my $key ; not initialized

if($key != 0){ # line 7
    print "1ok\n";
}
if($key != ''){  # line 10
    print "2ok\n";
}
if($key ne 0){ # line 13
    print "3ok\n";
}
if($key ne ''){ #line  16
    print "4ok\n";
}
if($key){ # line 19
    print "5ok\n";
}

resul


$ perl tmp.pl
Use of uninitialized value $key in numeric ne (!=) at tmp.pl line 7.
Argument "" isn't numeric in numeric ne (!=) at tmp.pl line 10.
Use of uninitialized value $key in numeric ne (!=) at tmp.pl line 10.
Use of uninitialized value $key in string ne at tmp.pl line 13.
3ok
Use of uninitialized value $key in string ne at tmp.pl line 16.


--------------------------------------------------------------------------------------------------------------------------------

#!/usr/bin/perl

use strict;
use warnings;
my $key = 1 ;  initialized

if($key != 0){
    print "1ok\n";
}
if($key != ''){
    print "2ok\n";
}
if($key ne 0){
    print "3ok\n";
}
if($key ne ''){
    print "4ok\n";
}
if($key){
    print "5ok\n";
}

result 

$ perl tmp.pl

1ok
Argument "" isn't numeric in numeric ne (!=) at tmp.pl line 10.
2ok
3ok
4ok
5ok
--------------------------------------------------------------------------------------------------------------------------------

Thursday, October 13, 2011

Perl Hash [SINTAXE]

 high voltage =)
----------------------------------------------------------------------------------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;\

my $key = 1;
my $hash;

@{$hash->{$key}} = ("10","11");
$hash->{1}[2] = "sintaxe\n";
print join ("|",@{$hash->{1}});

Result

$ perl tmp.pl 
10|11|sintaxe

Friday, October 7, 2011

Perl - [ Expression ]


#!/usr/bin/perl

use strict;
use warnings;

my $var = 'LGMB-RP_06242011_HGWSI_1.fastq.sai.sam.bam.sort.bam';
if( $var =~ /^(.*)\.fastq/ ){
    $var = $1;
}
print"$var\n";

$var = 'LGMB-RP_06242011_HGWSI_1.fastq.sai.sam.bam.sort.bam';
$var = "$1.sam" if( $var =~ /^(.*)\.sam/ );
print"$var\n";


$ perl t.pl
LGMB-RP_06242011_HGWSI_1
LGMB-RP_06242011_HGWSI_1.fastq.sai.sam


Thursday, October 6, 2011

Perl -[ if , unless ]


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

my $val = 1;

#first  
print "first (if)\n";
if($val > 1){
    print "if > 1\n";
}else{
    print "else <= 1\n";
}   

not functional
#second 
print "second (if)\n";
$val = 2;
if($val < 1){
    print "if < 1\n";
}unless($val > 1){
    print "unless < 1\n";
}   

#third
print "third (if)\n";
$val = 'a';
if($val =~ 'a'){
        print "if = a\n";
}unless($val =~ 'b'){
        print "unless != a\n";
} 

result 

first (if)
else <= 1
second (if)
third (if)
if = a
unless != a 


Perl - A [ Switch ] statement


Necessary import library.
Initialize the variable and play ($val).



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

use Switch;
my $val = 'a';
switch ($val) {
      case 1      { print "number 1" }
      case "a"    { print "string a" }
      case [1..10,42] { print "number in list" }
      case /\w+/  { print "pattern" }
      case qr/\w+/    { print "pattern" }
      else { print "previous case not true" }
}

Wednesday, October 5, 2011

Perl - [Function] (Sort)


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

my @blog = (1,7,6,34,2,46,7);

print "disorderly\n";
print join (" ",@blog);

@blog = sort @blog;
print "\norderly - the first number\n";
print join (" ",@blog);

print "\norderly - the complete number( descending order )\n";
@blog = sort {$b <=> $a} @blog;
print join (" ",@blog);

print "\norderly - the complete number( ascending order )\n";
@blog = sort {$a <=> $b} @blog;
print join (" ",@blog);


Result 

$ perl blog.pl 



disorderly
1 7 6 34 2 46 7
orderly - the first number
1 2 34 46 6 7 7
orderly - the complete number( descending order )
46 34 7 7 6 2 1
orderly - the complete number( ascending order )
1 2 6 7 7 34 46

-------------------------------------------------------------------------------------------------------------------------------
  • String 

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

my @blog = ('oi','tim','claro','vivo','nextel');
print "Disorderly : \n";
print join ("\t",@blog);

@blog = sort @blog;
print "\nOrderly : \n";
print join("\t",@blog);

@blog = ('cab','abc','abb','aaa','baa');
print "\nDisorderly : \n";
print join ("\t",@blog);

@blog = sort @blog;
print "\nOrderly : \n";
print join("\t",@blog);

Result 

$ perl blog.pl 



Disorderly :
oi tim claro vivo nextel
Orderly :
claro nextel oi tim vivo

Disorderly :
cab abc abb aaa baa
Orderly :
aaa abb abc baa cab
----------------------------------------------------------------------------------------------------------------------------------


continues soon ...