devoted_to_Perl has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'm trying to setup the reading and parsing in a data structure that is made of two parts linked together.

The first part is made of a matrix with 5 columns and N rows, and each column has headers that define the content of the column/array.

The second part employs J matrix of 2 columns and M rows each. The elements of one array in the first matrix are a pointer that links a row in the first matrix to the contents in the second matrix. For example, a header called "position" in the first matrix defines M positions in a beam section, and each position is linked to another matrix that has 2 columns/arrays.

Since I have J matrix in the second part, I need to get the right one with the corresponding header.

My problem is how to read the content of the matrix, loop over the first part and then parse the corresponding "position" in the right (second part) matrix. Also, I need to make use of the module Workflow::csv_File exclusively in order to read the contents.

Many thanks in advance for your patience and time!

Replies are listed 'Best First'.
Re: parsing with Workflow::csv_File
by Xiong (Hermit) on Nov 28, 2011 at 13:17 UTC

    I'd like to ask you to consider, please, what you're hoping to do; and what you're hoping to get from posting your problem. I'd like to help you to think about this.

    As Corion points out, the module you cite does not exist, at least not on CPAN. You might want to try searching CPAN for CSV-related modules. There are quite a few that will parse a CSV file into a reasonable Perlish structure. It's up to you to pick one you like.

    Parsing the CSV file is one, separate issue; doing stuff with that data is another. It's kinda scary to see you speaking about both issues in one breath; they're unrelated. Suggest if you need help picking out a CSV module, ask for that; if you need help munging the data once it's in a Perlish structure, ask for that; if you need help with both issues, write two distinct nodes.

    Next, let me talk about the way you talk about your problem set. It's not unusual; indeed, it's quite the common approach. It's not even un-engineer-like: We want to abstract away from details and generalize from specific cases. But in your attempt to present the general, abstract problem you make it difficult to see. We don't know exactly what you mean by some of your words, such as "beam" (at least I'm sure I don't). You might do much better by presenting actual sample data, e.g.:

    my $in_A = [ [ 0, 1, 2, 3 ], [ 9, 8, 4, 1 ], ]; my $in_B = [ ... ]; my $out = [ .... ];

    ... then, at least on the first cut, your whole question boils down to: How do I get from in to out? You can use as many input variables as you need to define the problem; and you should show the exact output you would like to get.

    It's not really useful for you to try to post your entire data set. As Corion says, a few lines is probably plenty. Looking at a large data set and cutting it down to a small example is a skill worth developing.

    I'm tempted to give more advice but I won't. I'd like to hope what I've given already will be helpful... and I'd very much like to see you work on your question with these things in mind before much more is thrown at you. Many Monks will read your post and instantly think of extremely clever solutions... to other problems; and they will present them in great detail. I'd like to know what your problem is.

    Feste: Misprison in the highest degree. Lady, cucullus non facit monachum. That's as much to say as, I wear not motley in my brain....
Re: parsing with Workflow::csv_File
by Corion (Patriarch) on Nov 28, 2011 at 12:21 UTC

    There is no module Workflow::csv_File on CPAN.

    Maybe it would help us to help you better if you would post five (relevant) lines from each table, together with an explanation of how they belong together and what they indicate. Also, please show us the relevant parts of the code you already have written and how it fails to do what you need it to do.

      Sorry, that package reads from a specified filename and the subroutine is as following:

      sub read_file { my $self = shift; my $filename = shift; my $i = 0; my $header_parse = 0; my $header_name = 0; open FILE, "<$filename" or die "cannot open $filename for input\n"; while (my $line = readline (*FILE)) { # Strip newlines and leading / trailing whitespace for ($line) { chomp; s/^\s+//; s/\s+$//; } # Skip blank lines and comments next if ($line !~ m/\w/ || $line =~ m/^#/); if ($line =~ m/^\[\s*([\w|\s]+)\s*\]/) { $header_name = $1; $header_parse = 1; } elsif ($header_parse) { $self->{HEADERS}{$header_name} = $line; $header_parse = 0; } else { my @data = split $self->{REGEX}, $line; $self->{DATA}[$i++] = \@data; } } close FILE; }

      The first matrix/table in a .csv file I need to read/loop from is as following:

      POSITION R CH TW TH

      1 2 2.0 60 100

      2 4 2.5 61 100

      3 6 2.8 62 88

      .. .. .. .. ..

      N 40 0.9 74 18

      while the structure of one of the J tables linked to the first table through the TH array is as following:

      Header 100 (or a value from 88 .. to 18, referring to TH in the first table)

      A B

      -180 0.1

      .. ..

      M 0.9

      I don't know how to parse the header (in the first table) in one of the J tables with the corresponding header.