PERL -- Regex incl all hash keys (sorted) + deleting empty fields from $_ in file read -
i'm working on program , have couple of questions, hope can help:
first need access file , retrieve specific information according index obtained previous step, in indexes retrieve found , store in hash.
i've been looking way include array elements in regex can use in file search, haven´t been able make work. i've found way works:
my @atoms = (); $natoms=0; foreach $atomi (keys %{$atome}){ push (@atoms,$atomi); $natoms++; } @atoms = sort {$b cmp $a} @atoms;
and use regex way:
while (<in_lig>){ if (!$natoms) {last;} ...... if ($_ =~ m/^\s*$atoms[$natoms-1]\s+/){ $natoms--; ..... }
is there way create regex expression include hash keys? numeric , must sorted. keys refer line index in in_lig, content this:
8 c5 9.9153 2.3814 -8.6988 c.ar 1 mlk -0.1500
the key found in column 0 (8). have added ^ , \s+ make sure refers first column.
my second problem input files not identical , make contain white spaces before index, when create array $_ column0 = " " instead of column0=8
i don't understand why "empty column" not eliminated on split command , i'm having trouble remove it. have done:
@info = split (/[\s]+/,$_); if ($info[0] eq " ") {splice (@info, 0,1);} # tried $info[0] =~ m/\s+/
and when print array @info this:
array: array: 8 array: c5 array: 9.9153 array: 2.3814 .....
how can rid of empty column?
many merche
there special form of split
remove both leading , trailing spaces. looks this, try it:
my $line = ' begins spaces , ends spaces '; @tokens = split ' ', $line; # prints |begins:with:spaces:and:ends:with:spaces| print "|", join(':', @tokens), "|\n";
see documentation split
@ http://p3rl.org/split (or perldoc split
)
also, first part of program might simpler as:
my @atoms = sort {$b cmp $a} keys %$atome; $natoms = @atoms;
but, ultimate goal atoms? if want verify atoms you're given indeed in file, don't need sort them, nor count them:
my @atoms = keys %$atome; while (<in_lig>){ # atom id on line ($atom_id) = split ' '; # atom id in array of atom ids looking if (grep { /$atom_id/ } @atoms) { # line of file has atom in array: $atom_id } }
Comments
Post a Comment