🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #7996

Re: Leer un fichero

#7996
From: Fernando Arbeiza
Date: Fri, 03 Jan 2003 12:46
40 lines
1857 bytes
Feliz a�o a todos.

On Thu, 2 Jan 2003 23:11:15 +0000 (UTC), Angel Luis Jim�nez Mart�nez
<al026635@alumail.uji.es> wrote:
>> Quiero sacar la media de una serie de datos que tengo almacenados en
>> un fichero. El fichero podria ser de la siguiente manera:(datos.txt)
>> 15
>> 47
>> 30
>> 80
>> 11
>> 42
>> 65
>> 2
>> 25
>> 23
>> 16

Como me aburro, ah� va otra posibilidad. En mi favorito, ruby:

########################
suma = 0
num_lineas = 0
$stdin.each_line do |linea|
    suma = suma + linea.to_i
    num_lineas = num_lineas + 1
end

media = suma.to_f / num_lineas
puts(media.to_s)
########################
    
    ./media.rb < datos.txt
    32.36363636

Un saludo.

-- 
Fernando Arbeiza        <URL: mailto:arbeizaf@ono.com>
Crea tu propio Linux:   <URL: http://www.escomposlinux.org/lfs-es>
Technical Details
Message-ID: <slrnb1b1hu.e0.arbeizaf@jupiter.olimpo>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.freenet.de!fu-berlin.de!uni-berlin.de!62-43-51-180.user.ono.COM!not-for-mail
References: <7d4ff0e4.0301020707.6388a1a6@posting.google.com> <av2guj$cng$1@sofora01.uji.es>

Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII
X-Google-Thread: f86c8,5cf283a7acb0d167
X-Google-Attributes: gidf86c8,public
X-Google-ArrivalTime: 2003-01-03 05:09:34 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.freenet.de!fu-berlin.de!uni-berlin.de!62-43-51-180.user.ono.COM!not-for-mail
From: Fernando Arbeiza <arbeizaf@ono.com>
Newsgroups: es.comp.os.linux.programacion
Subject: Re: Leer un fichero
Date: Fri, 3 Jan 2003 13:46:54 +0100
Lines: 40
Message-ID: <slrnb1b1hu.e0.arbeizaf@jupiter.olimpo>
References: <7d4ff0e4.0301020707.6388a1a6@posting.google.com> <av2guj$cng$1@sofora01.uji.es>
NNTP-Posting-Host: 62-43-51-180.user.ono.com (62.43.51.180)
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 8bit
X-Trace: fu-berlin.de 1041598586 12433089 62.43.51.180 (16 [148301])
X-Orig-Path: jupiter.olimpo!news
User-Agent: slrn/0.9.7.4 (Linux)