Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am trying to get information out from a lines of a log file which look like this:
TRANSLATION VECTOR IN AS 22.44568 -51.55717-97.09864I need to get the three numbers.
Because the last two numbers are not separated by whitespace in this case,
splitting on space does not work!@values = split ('\ \', $line);
print "TRANS @values[5] @values[6] @values[7] \n";Can you help?

First problem is that your split function is not splitting on whitespace. It's splitting on a string consisting of a backslash followed by a space followed by another backslash.
Here's how to split on whitespace:
@values = split (/ /, $line);or@values = split (/\s/, $line);Second issue is how you're dereferencing the array elements. When outputting individual elements, you need to use $values[5] instead of @values[5]. @values[5] is an array slice and could be used like this:
printf "TRANS %s %s %s\n", @values[1,3,5,7];Here's one solution to your problem (which doesn't include proper error handling).
@values = $line =~ /([-\d.]+)\s*([-\d.]+)\s*(-[\d.]+)$/;
print "TRANS @values\n";

Thanks for the help!
I tried your suggestion:
@values = $line =~ /([-\d.]+)\s*([-\d.]+)\s*(-[\d.]+)$/;
print "TRANS @values\n";
It is still not working on some of the
lines, for example:TRANSLATION VECTOR IN AS 22.44568 -51.55717-97.09864
The problem is that the last two numbers are not separated by a space!

The regex I gave does work for your example provided that there isn't anything else in the line after the 3rd number. If there is anything else after it, you'll need to remove the $ anchor.
@values = $line =~ /([-\d.]+)\s*([-\d.]+)\s*(-[\d.]+)/;
The regular expression:(?-imsx:([-\d.]+)\s*([-\d.]+)\s*(-[\d.]+))
matches as follows:
NODE EXPLANATION
------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
------------
( group and capture to \1:
------------
[-\d.]+ any character of: '-', digits (0-9), '.'
(1 or more times (matching the most
amount possible))
------------
) end of \1
------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
------------
( group and capture to \2:
------------
[-\d.]+ any character of: '-', digits (0-9), '.'
(1 or more times (matching the most
amount possible))
------------
) end of \2
------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
------------
( group and capture to \3:
------------
- '-'
------------
[\d.]+ any character of: digits (0-9), '.' (1
or more times (matching the most amount
possible))
------------
) end of \3
------------
) end of grouping
------------

HI, there is still a problem.
Here is the contents of a sample file:
look.file:
TRANSLATION VECTOR IN AS 57.45078 -75.02846 -67.41889
TRANSLATION VECTOR IN AS 23.02884-118.41707 30.16230
TRANSLATION VECTOR IN AS 62.93699 -13.52023 26.16759
TRANSLATION VECTOR IN AS 34.22855 -11.29179 -51.75066
TRANSLATION VECTOR IN AS -7.30446-113.41203 -32.03547
TRANSLATION VECTOR IN AS 3.86039 -40.97996 52.01907
TRANSLATION VECTOR IN AS 91.74750 -37.90217 -24.45767
TRANSLATION VECTOR IN AS 60.97234-121.14732 -24.18033
TRANSLATION VECTOR IN AS 64.48934 -74.99355 51.04527
TRANSLATION VECTOR IN AS -9.82437 -59.35365 -63.98584
TRANSLATION VECTOR IN AS -32.63210 -78.87022 18.61702Here is my script:
#!/usr/bin/perl
$file=look;
open (LOG, "$file.log");
while ($_ = <LOG>){
$line=$_;
@values = $line =~ /([-\d.]+)\s*([-\d.]+)\s*(-[\d.]+)$/;
print "TRANS @values\n";
}
close (LOG)Here is the output:
TRANS 57.45078 -75.02846 -67.41889
TRANS
TRANS
TRANS 34.22855 -11.29179 -51.75066
TRANS -7.30446-113.4120 3 -32.03547
TRANS
TRANS 91.74750 -37.90217 -24.45767
TRANS 60.97234-121.1473 2 -24.18033
TRANS
TRANS -9.82437 -59.35365 -63.98584
TRANSFor some of the lines it just missed it!
Also for the lines that contains -113 and
-121 it did not put a space between the first two numbers in the output.

The reason some of them were failing was due to some of the numbers not having the '-' that your example had. We need to modify the regex to make the '-' optional on that part of the regex.
#!/usr/bin/perl# these 2 pragmas should be in every Perl script
use strict;
use warnings;# all vars need to be declared with the 'my' keyword (creates a lexical var)
my $file = 'look.log';# it's better to use the 3 arg form of open
# and use a lexical var for the filehandle
# additionally, you should ALWAYS check the return code of an open call
open(my $LOG, '<', $file) or die "can't open $file $!";while ( my $line = <$LOG>){
# this should be modified to include proper error handling
my @values = $line =~ /(-?[\d.]+)\s*(-?[\d.]+)\s*(-?[\d.]+)$/;
printf "TRANS %12.5f %12.5f %12.5f\n", @values;
}
close $LOG;Should output:
TRANS 57.45078 -75.02846 -67.41889
TRANS 23.02884 -118.41707 30.16230
TRANS 62.93699 -13.52023 26.16759
TRANS 34.22855 -11.29179 -51.75066
TRANS -7.30446 -113.41203 -32.03547
TRANS 3.86039 -40.97996 52.01907
TRANS 91.74750 -37.90217 -24.45767
TRANS 60.97234 -121.14732 -24.18033
TRANS 64.48934 -74.99355 51.04527
TRANS -9.82437 -59.35365 -63.98584
TRANS -32.63210 -78.87022 18.61702

![]() |
Help With Recursion in Ja...
|
Pass method as parameter,...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |