#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Make lines as arrays, or arrays of arrays?


    Is it possible to make each line in a file as a array, then use them to do any calculation i want?
    For instance, the is a file look like this:

    Name, Num_A, Num_B, Num_C, etc ...
    Name_A, 102, 339, 0, 12, etc...
    Name_B, 29, 220, 10, 333, etc..
    Name_C, ..., ...,
    etc...
    ...

    I want to sum up Num_A and Num_B of Name_A and Name_B, so I think it would be easier if I can make lines into arrays, or even arrays of arrays?

    Any idea will be appreciate.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Code:
    [myprompt ~]$ cat file
    Name, Num_A, Num_B, Num_C, Num_D
    Name_A, 102, 339, 0, 12
    Name_B, 29, 220, 10, 333
    [myprompt ~]$
    [myprompt ~]$ cat test.pl
    #! /usr/bin/perl
    
    use strict;
    use warnings;
    
    my @elements;
    my $sum = 0;
    
    open FILE, "< file";
    
    while (<FILE>) {
        if ($. == 1) {
            print "Name, Sum\n";
            next;
        }
        chomp;
        $sum = 0;
        @elements = split (', ', $_);
        map { $sum += $_ } @elements[1 .. $#elements];
        print "$elements[0], $sum\n";
    }
    
    close FILE;
    [myprompt ~]$
    [myprompt ~]$ ./test.pl
    Name, Sum
    Name_A, 453
    Name_B, 592
    [myprompt ~]$
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by noobie1000
    Code:
    Name, Sum
    Name_A, 453
    Name_B, 592
    [myprompt ~]$
    Thanks for the reply, but I may confuse you.

    I want to make the file becomes this:
    @arrays_of_arrays(["Name","Num_A","Num_B","Num_C", "Num_D"],
    ["Name_A", 102, 339, 0, 12],
    ["Name_B", 29, 220, 10, 333],
    [...])

    so when i want to do some calculations, i can write something like: @
    arrays_of_arryas[2][3]+@arrays_of_arrays[3][3]
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Code:
    #! /usr/bin/perl
    
    use strict;
    use warnings;
    
    my @elements;
    my @arr_of_arr;
    open FILE, "< file";
    
    while (<FILE>) {
        chomp;
        push (@arr_of_arr, [split (', ', $_)]);
    }
    
    close FILE;
    
    my ($i, $j) = (0,0);
    for $i (0..$#arr_of_arr) {
        for $j (0..3) {
            print "$i, $j => $arr_of_arr[$i]->[$j]\n";
        }    
    }
    Here, @arr_of_arr contains three elements (representing three lines of file), each of which is a reference to an array that is formed by splitting each line.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by noobie1000
    Code:
    #! /usr/bin/perl
    
    use strict;
    use warnings;
    
    my @elements;
    my @arr_of_arr;
    open FILE, "< file";
    
    while (<FILE>) {
        chomp;
        push (@arr_of_arr, [split (', ', $_)]);
    }
    
    close FILE;
    
    my ($i, $j) = (0,0);
    for $i (0..$#arr_of_arr) {
        for $j (0..3) {
            print "$i, $j => $arr_of_arr[$i]->[$j]\n";
        }    
    }
    Here, @arr_of_arr contains three elements (representing three lines of file), each of which is a reference to an array that is formed by splitting each line.
    Thanks so much!

IMN logo majestic logo threadwatch logo seochat tools logo