Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

Wednesday, August 4, 2010

advanced regular expression 高级正则表达式

断言
零宽度正预测先行断言 and 零宽度正回顾后发断言

(1)零宽度: 这表示匹配是一个位置(Loaction)而不是子表达式。
(2)预测先行 vs 回顾后发
预测先行:仅当子表达式在此位置的右侧匹配时才继续匹配。返回与子表达式匹配的前面位置,从做左到右匹配。
回顾后发:仅当子表达式在此位置的左侧匹配时才继续匹配。返回与子表达式匹配的后边位置,从右到左匹配。
(3)正向 vs 负向:
正向:当子表达式满足时才匹配。
负向:当子表达式不满足时才匹配。


总共四种断言:
零宽度正预测先行断言
vim: \@=
vim例:\w\+\(ing\)\@=     匹配以ing结尾的单词的前面部分(除了ing以外的部分)
   如果用magic, 则为 \v\w+(ing)@\=
零宽度正回顾后发断言
vim: \@<=
vim例:\(re\)\@<=\w+     匹配以re开头的单词的后半部分(除了re以外的部分)
   如果用magic, 则为 \v(re)@\<\=\w+
零宽度负预测先行断言
vim: \@!
vim例:foo\(bar\)\@!     匹配后面不带 "bar" 的 "foo"
零宽度负回顾后发断言
vim: \@<!
vim例:\(foo\)\@<!bar  匹配前面不带 "foo" 的 "bar"

PCRE 对应的格式为:
(?= 子表达式), (?<= 子表达式)
(?! 子表达式), (?<! 子表达式)

for perl help:
# perldoc perlre

.NET Framework中支持有名RE, 并支持注释:
(?<myname>expression) 如果遇到匹配,则把匹配的内容命名为myname,并压入堆栈
(?<-myname>expression') 如果遇到匹配,则从堆栈中弹出 名为myname的匹配内容。
(?(myname)yes|no) 如果堆栈上存在命名为myname的匹配的内容,则去继续匹配yes部分的表达式,否去继续匹配no部分。


 注释
 注释一般通过表达式(?#注释)实现。
以下正则表达式在第一个分组中中添加了注释“不能以数字开头”。
(?<!\d+(?#不能以数字开头))[a-z_A-Z]+
如果要在正则表达式中包含注释,最好打开IgnorePatternWhitespace选项,即忽略模式里的空白字符。

Monday, June 21, 2010

mix of hash & array (perl)

  • array of hashes
@AoH = (
    {
       husband  => "barney",
       wife     => "betty",
       son      => "bamm bamm",
    },
…
)
push @AoH, { husband => "fred", wife => "wilma", daughter => "pebbles" };
$AoH[0]{husband} = "fred";
$AoH[1]{husband} =~ s/(\w)/\u$1/;

  • array of arrays
@AoA = (
         [ "fred", "barney" ],
         [ "george", "jane", "elroy" ],
         [ "homer", "marge", "bart" ],
);
$AoA[2][3]
$ref_to_AoA->[2][3]


  • hash of arrays
%HoA = (
  flintstones => [ "fred","barney" ],
…)
$HoA{teletubbies} = [ "tinky winky", "dipsy", "laa-laa", "po" ];
$HoA{flintstones}[0] = "Fred";


  • hash of hashes
%HoH = (
    flintstones => {
        husband   => "fred",
        pal       => "barney",
    },
...
)
$HoH{ mash } = {
    captain  => "pierce",
    major    => "burns",
    corporal => "radar",
};
$role=$HoH{$family}{$role};

Sunday, May 30, 2010

perl vs python (hash and list/array)











  perl python
  hash array hash array (tuple for constant, and
list for variable)
symbol
(whole, ele)
{},{} (),[] {},[] []/()
(list/tuple), []
declare my %hash =
("foo", 35)

my %last_name = (

       
"fred" => "flintstone",

…)
my @var = (11, 27.1 , "a string");

my @var = qw/ hello my god /
some_dict =
{

       
"fred": "flintstone",

…}
list = ['milk',
1, 'lettuce']
clear %hash = () @var = ()

$#array = -1;
dict={} list = []
retain
the size
my $count = keys %hash; $scalar = @array;

$scalar = scalar @array;
count = len(hash) l=len(L)
add/use
element
$last_name{'wilma'} =
"flintstone";
$array[3] = 5

@list2 = (1, @list1, 5); 
#nesting is not supported

@subarray2 = @array[0,1..3]
some_dict['wilma'] =
"flintstone"
a = (1, 2); b = (3, 4); c = (a, b) => ((1,2),(3,4))

c=a+b => (1,2,3,4)

L[i:j] = J;
use
keys/values
my @k =
keys %hash;

my @v = values %hash;

while ( ($key, $value) = each %hash )

  …
  k =
hash.keys()

v = hash.values()

for (key, value) in hash.items():

  ...
 
check
existence of an ele
if (exists $books{'dino'}) exists  $array[
$index ] -> existence

defined $array[ $index ] -> defined
if books.has_key('dino'):  
add an
ele to the end
  push(@coins,
"Penny");
  list.append(x)
add an
ele to the beginning
  shift(@coins,
"First");
  list.insert(0,
x)
remove
an ele
delete $books{$person};    del books[person]  
from the
beginning
  $var=unshift(@coins);   a=list.pop([i])
from the
end
  $var=pop(@coins);   a=list.pop()
sort sort { $a <=> $b }
keys(%hash)
@array2 =
reverse sort (@array);
  sorted([5, 2, 3, 1, 4])

list.sort()

list.reverse()
sort
(unique value)
  my @s = uniq sort @a;    
join/split   $firststring
= join(", ",@array);

@array = split('-',$astring);
   
index       list.index(var)  #find the first ele that match var
ref my
$hash_ref = {};

$href->{ $key } = $value;
    ref_list=true_list





Monday, May 17, 2010

verilog/sv indent


  1 #!/usr/bin/perl

  2 # Author; Lifeng Chen, Magnum Semiconductor

  3 # Purpose: Indent verilog/systemverilog file

  4

  5 use Term::ANSIColor;

  6

  7 # Flow Control

  8 my $debug_me = 1;

  9

 10 ##TODO

 11 # 1. better indent for block-comment

 12

 13 # Space for each indent

 14 my $Space = "  ";

 15

 16 # comment definition.

 17 my $sl_delm = qw(// );

 18 my %block_delm = (

 19   '{' => '}',

 20   '(' => ')'

 21 );

 22

 23 # Note: Each Open keyword must have corresponding close keyword

 24 my @OpenSingle = qw{repeat for while if else};

 25 my @CloseSingle = qw{; };

 26

 27 my @OpenKeywords = qw{ begin module program package class task function fork case casex casez};  #interface

 28 my @CloseKeywords = qw{ end endmodule endprogram endpackage endclass endtask endfunction join join_none join_any endcase};  #endinterface

 29 my @IgnoreKeywords = qw{ extern typedef disable};

 30

 31 my @OpenSpecial = qw{ovm_field_utils_begin ovm_object_utils_begin ovm_object_param_utils_begin ovm_component_utils_begin};

 32 my @CloseSpecial = qw{ovm_field_utils_end ovm_object_utils_end ovm_object_param_utils_end ovm_component_utils_end};

 33

 34 @OpenKeywords = (@OpenKeywords, @OpenSpecial);

 35 @CloseKeywords = (@CloseKeywords, @CloseSpecial);

 36

 37 sub indent {

 38     my $open_block_comment=0, $close_block_comment=0, $in_block_comment=0;

 39     my $total_indent = 0, $block_indent = 0, $single_indent = 0, $last_indent=0;

 40     my($infile) = @_;

 41     my $tmpfile= "$infile.tmp";

 42     my $open_num=0, $close_num=0, $bracket_delta=0;

 43     my $cleaned_line;

 44

 45     print colored ['green'],"$infile\n";

 46     system("cp $infile $tmpfile");

 47

 48     open(TFILE, ">$tmpfile");

 49     open(FILE, "<$infile");

 50

 51     LINE_LOOP: while(my $line = <FILE>) {

 52         chomp $line;

 53         if ($open_block_comment==1 and $close_block_comment==0) {$in_block_comment = 1;}

 54         else {$in_block_comment = 0;}

 55         $line =~ s{$sl_delm(\w|\*)}{$sl_delm $1};  #remove false block-comment; append space after single-line comment symbol immediately followed by letters etc

 56         $cleaned_line=$line;

 57

 58         if ($cleaned_line =~ m{/\*}) {

 59             $open_block_comment=1;  #start a block comment

 60         }

 61         if ($cleaned_line =~ m{\*/}) {

 62             $close_block_comment=1; #end a block comment

 63         }

 64         if ($close_block_comment == 1) {  #clear block comment flag

 65             $open_block_comment=0;

 66             $close_block_comment=0;

 67         }

 68         #if ($in_block_comment) {$cleaned_line =~ s{^.*\*/}{*/};}  #remove anything in block comment, till end-block symbol is detected

 69         $cleaned_line=~s{"[^"]*"}{}g;  #remove keyword in string

 70         $cleaned_line =~ s{/\*.*\*/}{}g;  #remove keyword in a single-line block comment

 71         $cleaned_line =~ s{^.*\*/}{};  #remove keyword in block comment

 72         $cleaned_line =~ s{/\*.*$}{};  #remove keyword in block comment

 73         $cleaned_line=~ s{$sl_delm.*$}{};  #remove keyword in line comment

 74         $cleaned_line =~ s{\s+$}{};  #remove tailing blanks

 75         #if ($debug_me == 1 and $. >= 577 and $. <= 585) {

 76         #print "debug_me: $., blk_ind: $block_indent, sgl_ind: $single_indent, comment=$in_block_comment: '$cleaned_line'\n";

 77         #}

 78         if ($in_block_comment==1) {

 79             print TFILE "$line\n";  #keep things as they were in a block comment

 80             next LINE_LOOP;

 81         }

 82         $line =~ s/^\s+//;  #remove leading space to prepare for indent

 83         $line =~ s/\s+$//;  #remove any tailing space

 84

 85         #output the active line

 86         if ($line =~ m{^$sl_delm}) {  #indent single-line comment

 87             for($i=0;$i<$block_indent+$single_indent;$i++) { print TFILE $Space; }

 88             print TFILE "$line\n";

 89             next LINE_LOOP;

 90         }

 91

 92         foreach $CloseKey (@CloseKeywords) {

 93             if($cleaned_line =~ /\b$CloseKey\b/) { $block_indent--; $single_indent=0;}

 94         }

 95         $bracket_delta=0;

 96         foreach $open (keys %block_delm) { #indent brackets

 97           $close=$block_delm{"$open"};

 98           $open_num = ($cleaned_line =~ s/\Q$open\E/$open/isg);

 99           $close_num = ($cleaned_line =~ s/\Q$close\E/$close/isg);

100           $bracket_delta+=$open_num-$close_num;

101         }

102         if ($cleaned_line =~ /\S/) {for($i=0;$i<$block_indent+$single_indent;$i++) { print TFILE $Space; }}  #don't indent empty line

103         print TFILE "$line\n";

104         $start_block_line=0;

105         OPEN_BLOCK: foreach $OpenKey (@OpenKeywords) {

106             foreach $IgnoreKey (@IgnoreKeywords) {

107               if($cleaned_line =~ /\b$IgnoreKey\b/) { last OPEN_BLOCK;}

108             }

109             if($cleaned_line =~ /\b$OpenKey\b/) { $block_indent++; $start_block_line=1; }

110         }

111         if ($start_block_line == 0) {

112             OPEN_SINGLE: foreach $OpenKey (@OpenSingle) {

113                 if($cleaned_line =~ /\b$OpenKey\b/ and $cleaned_line !~ /`$OpenKey\b/ ) {

114                     $single_indent++;

115                 }

116             }

117             if ($single_indent > 0) {

118                 CLOSE_SINGLE: foreach $CloseKey (@CloseSingle) {

119                     if($cleaned_line =~ /$CloseKey$/) {

120                         $single_indent=0;

121                     }

122                 }

123             }

124         }

125         $block_indent+=$bracket_delta;

126         #if ($bracket_delta<0) {$block_indent+=$bracket_delta; }

127         #if ($bracket_delta>0) {$block_indent+=$bracket_delta; }

128         if ($block_indent < 0) {print colored ['yellow'],"\tindent_num = $block_indent @ cleaned_line $.. will be reset.\n"; $block_indent=0;}

129         $last_indent=$block_indent;

130     }

131     $total_indent=$block_indent + $single_indent;

132     if ($total_indent != 0) {print colored ['yellow'],"\tThe indentation of final line is not cleared to zero: $block_indent + $single_indent\n";}

133     close FILE; close TFILE;

134     system("mv -f $tmpfile $infile");

135 }

136

137 my $argnum;

138 if($ARGV[0] eq "" or $ARGV[0] =~ /-h/) {

139     print "Usage : $0 verilogfile.v\n";

140     print "        $0 *.sv\n";

141     print "        $0 *.*v*\n";

142     print "Help  :\n";

143     print "        $0\n";

144     exit(0);

145 }

146 print "Indenting...\n";

147 foreach $argnum (0 .. $#ARGV) {

148     my $ifile = $ARGV[$argnum];

149     &indent($ifile);

150 }

>

Tuesday, May 26, 2009

perl vs xml vs excel

use Spreadsheet::ParseExcel;
use XML::DOM;
my $doc = XML::DOM::Document->new;

my $xml_pi = $doc->createXMLDecl ('1.0',"UTF-8","yes");
$doc->setXMLDecl($xml_pi);

my $root = $doc->createElement('tests');
$root->setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");

my $parser = Spreadsheet::ParseExcel->new(
  CellHandler => \&cell_handler,
  NotSetCell => 1
);

my $workbook = $parser->Parse($filename);
$root->printToFile ($outFile);

exit(0);

  sub cell_handler {

  my $workbook = $_[0];
  my $sheet_index = $_[1];
  my $row = $_[2];
  my $col = $_[3];
  my $cell = $_[4];
 my $myTag;
 my $myText;

  $testName=$cell->value();
  if ($testName !~ m{\w+\.sv}) {$testName = ""; $lastCol = -1; return;}
  $test = $doc->createElement('Test');
  $myTag = $doc->createElement('Name');
  $myText = $doc->createTextNode($cell->value());
  $myTag->appendChild($myText);    #set text content for this tag
  $test->appendChild($myTag);

  if ($implemented == 1) { $root->appendChild($test); } # add new entry only if it's new line

}

Monday, May 25, 2009

install perl module as non-root

perl Makefile.PL, make, make test, make install

The standard way to install a Perl module on Unix is to change into the directory that was created when you unpacked the .tar.gz file, and then type the following sequence of commands:

  perl Makefile.PL
  make
  make test
  make install


This will create a makefile for you, then compile the module, test it, and put it in the correct location for you. This requires that you are logged in as root, so that you can copy files to the Perl library directory, and various other places on your system where the installation will put files.

If you do not have root permissions on the machine where you want to install the module, such as if you wish to install a module in your home directory, just change one of those commands. Instead of

  perl Makefile.PL

type

  #perl Makefile.PL PREFIX=/path/to/where/you/want/it
  perl Makefile.PL INSTALL_BASE=/path/to/where/you/want/it

 make install

That will put all the files in that directory. In order to use modules that are stored in that location, you will need to add the following like to your Perl programs:

  use lib "/path/to/where/you/want/it/perl5"
and, yes, thanks for the comment, local::lib might be easier to install a new module.
  use local::lib "/path/to/where/you/want/it/perl5"