Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Rekenbewerking op text file

Path: g2news1.google.com!news1.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: John Bokma <postmas...@castleamber.com>
Newsgroups: nl.comp.os.linux.programmeren
Subject: Re: Rekenbewerking op text file
Date: 22 Feb 2005 17:58:52 GMT
Organization: Castle Amber - software development
Lines: 83
Message-ID: <Xns960579E08973Bcastleamber@130.133.1.4>
References: <slrnd1loso.emr.root@dragonhill.xs4all.nl> <newscache$hpbbci$x9d$1@news.emea.compuware.com>
X-Trace: individual.net 3ss3ZoycKriaZqm8Jzo4Mgzcv7SBvGFv00aUF3fLq/JnFQIPSA
User-Agent: Xnews/5.04.25
X-Face: $7h&5K*ZL9*ZQ\6Hco2"V?1dD*C<ab^>zlws[S%i0b,@gKk|[,)Y-<hk143{.Ov"x{({zo( l,N\m`-_^nJfVQdEyt+-uAP^7WG]5mHj9&::7X-qK;Yv;xmRvNWQR"_igw+cr5WMj;6:zlj<R80Ei@ $gGWq&*{FL*kC*a

Koos Pol wrote:

> my $line;
> my ($be, $ae); # before equal sign, after equal sign

declareer vars waar je ze gebruikt. Daarnaast, $before_equal, 
$after_equal had je dat commentaar kunnen besparen :-D.

> my @numbers;
> while ($line = <DATA>) {

while ( my $line = <DATA> )

>     ($be, $ae) = split /=/, $line;

      dus hier: my ( $be, $ae )

Andere oplossing:

my ( @numbers ) = $line =~ /(\d+\.\d+)/g;

>     @numbers = map {$_ * 4} @numbers;
>     print join " ", map {"[$_]"} @numbers;
>     print "\n";

print "[",

    join ( "] [", map { $_ * 4 } @numbers ),

    "]\n";

Als de getallen in een bestand staan, kan:

#!perl -n

use strict;
use warnings;

my ( @numbers ) = $_ =~ /(\d+\.\d+)/g;

print "[",

    join ( "] [", map { $_ * 4 } @numbers ),

    "]\n";


handig zijn (zie perl -h voor de -n optie)

Of wellicht:

#!perl -n

use strict;
use warnings;

my ( $stamp, @numbers ) = split /[=,]/;
my $count = pop @numbers;

print "$stamp=",

    join ( ",", map { $_ * 4 } @numbers ),

    ",$count";

( merk op dat $count de \n nog bevat )

En een meer awky oplossing:

#!perl -naF[=,]
printf "%14s=%0.02f,%0.02f,%0.02f,%0.02f,%d\n",
    $F[ 0 ], ( map { $_ * 4 } @F[ 1..4 ] ), $F[ 5 ];

De "one liner" is left as an excercise ;-)


( merk op: %14s ipv %14d, getal was hier te groot (WinXP) )

-- 
John Bokma
                 Voorbeeldscripts in Perl: http://johnbokma.com/perl/
Ervaren Perl / Java programmeur beschikbaar:  http://castleamber.com/
Tevreden opdrachtgevers:     http://castleamber.com/testimonials.html