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?
>
> Thank you.
>
> _____________________________________________________
>
> #!/usr/bin/perl
> #
> 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;
>
> ______________________________________________
Based on your example, I'm not sure what you final goal is, as the
string you used does not look like a line from your sample data. I
came up with an example using your data which may or may not be what
you want.
use warnings;
use strict;
while (<DATA>)
{
# This should fix the line if the minus sign is always
# preceded and followed by a digit
s{(\d)(-\d)} {$1 $2}g;
# if for some reason you only want numbers
my @pieces= split (" ");
my @nums;
foreach my $piece ( @pieces )
{
# There is probably a better way to do this.
# This is a fairly simple regular expression
# that does not handle the case where a number
# might end with a period.
if ( $piece =~ /^-?\d+(?:.\d+)?$/ )
{
push @nums, $piece;
}
}
map {print $_ . "\n"} @nums;
}
__DATA__
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
__END__
Of course there are many ways to do this.
HTH, Ken
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/