Paul Lalli wrote:

On Nov 8, 3:32 pm, [EMAIL PROTECTED] (Demian) wrote:
Hello,

I ran into a problematic file that combined two numeric columns into
one:

ATOM    325  CA  GLU B  40     -30.254  72.432-297.620  1.00
10.00           C
ATOM    326  CA  ASP B  41     -28.149  73.031-294.529  1.00
10.00           C
ATOM    327  CA  GLU B  42     -27.716  76.690-295.429  1.00
10.00           C
ATOM    328  CA  LEU B  43     -31.425  77.076-296.027  1.00
10.00           C
ATOM    329  CA  VAL B  44     -32.237  75.542-292.673  1.00
10.00           C
ATOM    330  CA  SER B  45     -29.850  77.900-290.914  1.00
10.00           C
ATOM    331  CA  LEU B  46     -31.335  80.873-292.720  1.00
10.00           C
ATOM    332  CA  GLN B  47     -34.837  79.809-291.801  1.00
10.00           C

I came up with a solution, but I'm sure there's an easier way.  Is
there a more elegant way of doing it?

Split on either whitespace or on a minus sign that is both followed by
and preceded by a digit:

my @fields = split /\s+|(?<=\d)-(?=\d)/, $line;

Why have you decided that the OP's solution doesn't provide what he wants,
snipped it, and offered some code that does something very different?

Demian posted this program:


use strict;
use warnings;

my $numstring= "-100.00-400.34";

my @pieces= split /([-])/, $numstring;

my @nums;

for (my $k=0; $k<=$#pieces; $k++){
 if ($pieces[$k] eq "-") {
   push @nums, "$pieces[$k]$pieces[$k+1]";
   $k++;
   next;
 }
 elsif($pieces[$k]=~ /\d+/) {
   push @nums, $pieces[$k];
 }
}

map {print $_ . "\n"} @nums;

__END__

which outputs

-100.00
-400.34

and he asked for a more elegant way of doing the same thing. I would have
thought it was a good idea to assume he knew what he wanted.

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to