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

Re^4: A complicated file parsing and 2D array/matrix problem.

by zing (Beadle)
on Aug 27, 2012 at 08:12 UTC ( #989938=note: print w/ replies, xml ) Need Help??

in reply to Re^3: A complicated file parsing and 2D array/matrix problem.
in thread A file parsing and 2D array/matrix problem.

Hello Kenosis. Now there's the last piece of puzzle. I have created 5 such matrices (with obviously same number of rows and column). Now the problem is that I have to concatenate (OR logic operation) two such matrices,

INPUT = Two matrices A,B of same row and column saved in text files A.txt and B.txt

OUTPUT = A single matrix C ( Cij = Aij OR Bij )

==============INPUT======= MAT - A 1875 2809 3182 3419 2809 - 1 1 - 3182 1 - - - 3186 1 1 - - 3485 - - - - 3486 - - - - MAT - B 1875 2809 3182 3419 2809 1 - - 1 3182 - - - - 3186 - 1 1 - 3485 - - - - 3486 - 1 - 1 ========== OUTPUT=========== MAT - C 1875 2809 3182 3419 2809 1 1 1 1 3182 1 - - - 3186 1 1 1 - 3485 - - - - 3486 - 1 - 1
I.e. an element of matrix will be one if either of the corresponding element of A or B is one.

Comment on Re^4: A complicated file parsing and 2D array/matrix problem.
Download Code
Replies are listed 'Best First'.
Re^5: A complicated file parsing and 2D array/matrix problem.
by Kenosis (Priest) on Aug 27, 2012 at 18:18 UTC

    Hi, zing!

    What have you tried so far to solve this puzzle?

      Kenosis, This is what Im trying, but is throwing a lot of errors.
      open FILE, "Matrix.txt" or die $!; my @lines = <FILE>; my $size = scalar @lines; my @matrix = (1 .. 3); my $i = 0; my $j = 0; my @micro; foreach ($matrix) { foreach ($lines) { push @{ $micro[$matrix]}, $lines; } }

      CONTENTS OF matrix.txt

      1 2 3 4

      1 5 6 8

      1 7 8 0

      I need to be able to access each element of the matrix thus created on a "one by one basis".

        It may be best to work on the A.txt and B.txt data sets you showed. If you can create a script to generate the ORed results of those two matrices, then it's most likely that script will work with larger matrices.

        First, always begin your scripts with either:

        use strict; use warnings;


        use Modern::Perl;

        I prefer the latter, since it includes the former two pragmas--among other things.

        You can use File::Slurp qw/read_file/; to read a file's contents into an array. The foreach() takes a list or array--not a scalar, like you've done. That would account for some of the errors.

        Here's one way to approach the problem:

        1. Read both matrix files into arrays.
        2. The first element (row 0) of each array is identical, as it contains the column titles. Grab those and create the title string (like in the first script, but w/o sorting), and move to the next rows (1 .. n) of the two arrays.
        3. Split each row (1 .. n) of the two arrays into their elements, placing those elements into two element arrays. The zeroth element of these arrays contains the row label.
        4. Do an OR comparison between the two element arrays (1 .. n), and build an OR-result array for the table.
        5. Finally, build the table and print it.

        The scripting that produced the first table should help with reading files and building the table.

        Step through this scripting process, and then share what you've completed. A script can be created to produce the ORed output you need; it'll just take a little more work...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://989938]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2015-11-27 10:24 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (724 votes), past polls