Note that there are some explanatory texts on larger screens.

plurals
  1. POPERL -- Regex incl all hash keys (sorted) + deleting empty fields from $_ in file read
    text
    copied!<p>I'm working on a program and I have a couple of questions, hope you can help:</p> <p>First I need to access a file and retrieve specific information according to an index that is obtained from a previous step, in which the indexes to retrieve are found and store in a hash.</p> <p>I've been looking for a way to include all array elements in a regex that I can use in the file search, but I haven´t been able to make it work. Eventually i've found a way that works:</p> <pre><code>my @atoms = (); my $natoms=0; foreach my $atomi (keys %{$atome}){ push (@atoms,$atomi); $natoms++; } @atoms = sort {$b cmp $a} @atoms; </code></pre> <p>and then I use it as a regex this way:</p> <pre><code>while (&lt;IN_LIG&gt;){ if (!$natoms) {last;} ...... if ($_ =~ m/^\s*$atoms[$natoms-1]\s+/){ $natoms--; ..... } </code></pre> <p>Is there any way to create a regex expression that would include all hash keys? They are numeric and must be sorted. The keys refer to the line index in IN_LIG, whose content is something like this: </p> <pre><code>8 C5 9.9153 2.3814 -8.6988 C.ar 1 MLK -0.1500 </code></pre> <p>The key is to be found in column 0 (8). I have added ^ and \s+ to make sure it refers only to the first column.</p> <p>My second problem is that sometimes input files are not always identical and they make contain white spaces before the index, so when I create an array from $_ I get column0 = " " instead of column0=8</p> <p>I don't understand why this "empty column" is not eliminated on the split command and I'm having some trouble to remove it. This is what I have done:</p> <pre><code>@info = split (/[\s]+/,$_); if ($info[0] eq " ") {splice (@info, 0,1);} # also tried $info[0] =~ m/\s+/ </code></pre> <p>and when I print the array @info I get this:</p> <pre><code>Array: Array: 8 Array: C5 Array: 9.9153 Array: 2.3814 ..... </code></pre> <p>How can I get rid of the empty column?</p> <p>Many thanks for your help Merche</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload