regex - How to assign class based on regexp match (sorting in perl) -


i reading file. based on value in 1 column, want assign own class/tag it.

these regexps:

'ltr*','mlt*','mst*' ... belong class herv.

'charlie*','looper*' ... belong class dna

right have 2 arrays, 1 regexps , 1 respective classes:

 @array = map { qr{$_} } ('alu*', 'herv*', 'charlie*' ...  @classes = ('alu', 'herv', 'dna', 'line' ... 

so know if line matches charlie*, belongs class dna.

to sum up, every line of file looping whole array , looking match:

for $i (0 .. $#array) {          if ($type =~ m/$array[$i]/) {                         $class=$classes[$i];         }  } 

of course, not clever. better say: "this group of regexps belongs class" suggests use of hash.

however, consider quite inconvenient loop lines, keys of hashmap , values of keys and, when there match, use key resulting class/tag. solution or not?

thank much.

you can this:

my %re = (        herv=>qr/ltr|mlt|mst/,        dna=> qr/charlie|looper/    ); $class; (keys %re) {     $class = $_, last if ($type =~ $re{$_}); } 

this save regex compilation , 1 loop.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -