On Fri, Jun 20, 2008 at 8:18 AM, Yanto Young <[EMAIL PROTECTED]> wrote:
> My bad, it works for the example input I typed. The real input looks like 
> this:
>
> **********************
> 397.458453 -1
> 397.778590 1
> 397.999721 -1
> 398.020775 1
> 398.043870 -1
> 398.137144 -1
> 398.323242 -1
> 398.972728 1
> 399.580930 -1
> 399.593487 1
> 399.600271 1
> 399.935893 1
> ***********************

I see. I assume the *** is not part of the input. Here is the script
that should work:

$result = 0;
$filename = shift || die 'No file is specified.';
open(FILE, $filename) || die 'Could not open file.';

while ( <FILE> ) {
   chomp;
   if ($_ =~ /(-?[\.\d]+\s+)(-?\d+)/) {
       $result += $2;
       print $1, $result, "\n";
   } else {
       die 'Wrong format.';
   }
}

This one will accept a floating point on the left (negative or
positive) and an integer on the right. If you need a floating point on
the right replace the \d+ with [\.\d]+ on the right part of the
regular expression.

>
> Yanto
>
>
> On Fri, Jun 20, 2008 at 5:11 PM, Yanto Young <[EMAIL PROTECTED]> wrote:
>> Thanks for the reply Chris.
>>
>> I didn't manage to get it to work. It throws the error "Wrong format",
>> probably got something to do with the checking of white space?
>>
>> Yanto
>>
>>
>> On Fri, Jun 20, 2008 at 4:50 PM, Chris Henry <[EMAIL PROTECTED]> wrote:
>>> On Fri, Jun 20, 2008 at 7:45 AM, Chris Henry <[EMAIL PROTECTED]> wrote:
>>>> On Fri, Jun 20, 2008 at 7:43 AM, Chris Henry <[EMAIL PROTECTED]> wrote:
>>>>> #/usr/bin/perl
>>>>>
>>>>> var result = 0;
>>>>> open(FILE, '...') || die 'Can't open file';
>>>>>
>>>>> while ( <FILE> ) {
>>>>>  chomp;
>>>>>  if ($_ =~ /(t=\d+\s)(\d+)/) {
>>>>>    $result += $2;
>>>>>    print $1, $result;
>>>>
>>>> Sorry, this line should be print $1, $result, "\n";
>>>>
>>>>>  } else {
>>>>>    die 'File format is wrong.';
>>>>>  }
>>>>> }
>>>>>
>>>
>>> Ignore, too many syntax error, haven't used Perl for too long, here is
>>> a tested and working version:
>>>
>>> #!/usr/bin/perl
>>>
>>> $result = 0;
>>> $filename = shift || die 'No file is specified.';
>>> open(FILE, $filename) || die 'Could not open file.';
>>>
>>> while ( <FILE> ) {
>>>    chomp;
>>>    if ($_ =~ /(t=\d+\s+)(-?\d+)/) {
>>>        $result += $2;
>>>        print $1, $result, "\n";
>>>    } else {
>>>        die 'Wrong format.';
>>>    }
>>> }
>>>
>>>
>>>>
>>>>
>>>> --
>>>> Chris
>>>> [EMAIL PROTECTED]
>>>> [EMAIL PROTECTED]
>>>>
>>>
>>>
>>>
>>> --
>>> Chris
>>> [EMAIL PROTECTED]
>>> [EMAIL PROTECTED]
>>>
>>
>
> _______________________________________________
> Slugnet mailing list
> [email protected]
> http://wiki.lugs.org.sg/LugsMailingListFaq
> http://www.lugs.org.sg/mailman/listinfo/slugnet
>



-- 
Chris
[EMAIL PROTECTED]
[EMAIL PROTECTED]

_______________________________________________
Slugnet mailing list
[email protected]
http://wiki.lugs.org.sg/LugsMailingListFaq
http://www.lugs.org.sg/mailman/listinfo/slugnet

Reply via email to