Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

adding array elements

by Sidac (Initiate)
on Mar 09, 2011 at 14:15 UTC ( #892197=perlquestion: print w/replies, xml ) Need Help??
Sidac has asked for the wisdom of the Perl Monks concerning the following question:

Hello, i have a text file with numbers arranged as groups seperated by blank(empty) lines such as -
_DATA_ 20.187 15.415 8.663 6.001 6.565 6.459 6.564 13.454 13.475 13.220 +12.982 13.502 13.774 13.490 19.715 14.726 8.307 5.833 6.367 6.089 6.444 6.403 8.672 8.236 +12.982 13.502 13.774 13.490 20.099 15.205 8.763 6.317 6.861 6.973 6.701 6.334 8.406 8.155 + 7.395 8.156 9.551 8.530
How can i add all the first elements (20.187+19.715+20.099 etc ) , second elements (15.415+14.726+15.205) and so on in the list.(Groups are always of equal length. thanks

Replies are listed 'Best First'.
Re: adding array elements
by johngg (Abbot) on Mar 09, 2011 at 14:56 UTC

    Have a look at $INPUT_RECORD_SEPARATOR or $/ in perlvar for how to read your data file record by record. Have a look at split to break each record into fields and perldsc and push for building an array of arrays. Then think how you would iterate over all of the elements; perhaps a nested loop?

    Have a go at putting this all together then come back to the Monastery with any problems you are having with your code.



Re: adding array elements
by JavaFan (Canon) on Mar 09, 2011 at 14:44 UTC
    use 5.010; my @s; while (<DATA>) { my @e = split; $s[$_] += $e[$_] for 0 .. $#e; } say "@s";

      You could also add up the elements on each line with sum from List::Util, so the code above would become:

      use 5.010; use List::Util qw(sum); my @s; push @s, sum( split ) while <DATA> say "@s";
        Yeah, and I guess one could dance around in the streets as well.

        Considering that you are presenting a solution for a problem that isn't the OPs, what exactly is your point?

Re: adding array elements
by Anonymous Monk on Mar 09, 2011 at 14:44 UTC
    Read perlintro, read from data, split, iterate, add ... actually, you know what, considering your first question, subtraction in array, how about you ask that guy?
Re: adding array elements
by Fletch (Chancellor) on Mar 09, 2011 at 17:03 UTC
    ;; Presented with just about as much Perl as the OP . . . (ns sumcols (:use [clojure.contrib.str-utils :only (re-split)]) (:use [ :only (read-lines)])) (defn sumline [acc line] (loop [a acc l (map #(Float. %) (re-split #"\s+" line)) idx 0] (if (empty? l) a (recur (assoc a idx (+ (get a idx 0) (first l))) (rest l) (inc i +dx))))) (defn sumcols [filename] (loop [acc {} lines (filter #(> (count %) 0) (read-lines filename))] (if (empty? lines) (vals acc) (recur (sumline acc (first lines)) (rest lines))))) (println (sumcols (first *command-line-args*)))

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: adding array elements
by Neighbour (Friar) on Mar 09, 2011 at 14:45 UTC
    You could do this by maintaining an array in which you keep incrementing the various fields as you read lines. Like so:
    use strict; my @data; open(INPUT,'<',"monks.dat") or die("Can't open file"); while (<INPUT>) { chomp; my @newdata = split(/ +/); for my $index (0 .. $#newdata) { $data[$index] += $newdata[$index]; } } close(INPUT);
    Though this does not check whether the amount of fields is actually the same or not :)
Re: adding array elements
by Gulliver (Monk) on Mar 09, 2011 at 17:19 UTC

    Why not use a spreadsheet? Import the data as fixed width or specify a space as the delimiter. Are you doing this to learn Perl?

    By the way: __DATA__ needs to have 2 underscores in front and back to work in a perl script, it looks like you only have one.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://892197]
Approved by johngg
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2017-07-28 05:08 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (424 votes). Check out past polls.