in reply to Re^4: Parsing .txt into arrays
in thread Parsing .txt into arrays

Hi Fshah,
I think some clarification about PerlMonks is in order.
This is a site where you can ask questions with the intent of learning about Perl. I am completely happy to help you learn at no charge. I am happy if you are learning. I am not happy if you are not learning.

Right now it appears that you are expecting me to write your code for you - without demonstrating much effort on your part.

I do have clients that pay me for solving their problems. Quite frankly these folks will get much higher priority than you. However I and others here are willing to help you learn. BUT, that means that you need to show some coding effort.

Your points 4,5,6 and 7 tell me that you didn't run much less understand the code which I modified for you.

1) Transposing a table, converting rows to columns is not that difficult if you think logically about it. I want to see a serious attempt by you. Use the 2-d table that my code generates.

2) Setting the current field to what was before in the case that it is "blank" (whether row-wise or column-wise) is also something that you should be able to make an attempt at.

The construction of a state machine to parse your various tables was beyond either of these tasks and I felt that it was necessary to get you "unstuck".

Solving this problem will help you. Write code that generates @transposed using @array as input. I know its hard, but give it a go...

#!/usr/bin/perl use strict; use warnings; my @array = ( ['a', '1', 'L'], ['b', '2', 'M'], ['c', '3', 'N'], ['d', '4', 'O'],); my @transposed = ( ['a', 'b', 'c', 'd'], ['1', '2', '3', '4'], ['L', 'M', 'N', 'O'],); foreach my $row_ref (@array) { print "@$row_ref\n"; } # Prints: #a 1 L #b 2 M #c 3 N #d 4 O foreach my $row_ref (@transposed) { print "@$row_ref\n"; } # Prints: #a b c d #1 2 3 4 #L M N O

Replies are listed 'Best First'.
Re^6: Parsing .txt into arrays
by Fshah (Initiate) on Jun 07, 2017 at 06:30 UTC
    i'm sorry I didn't realize you already sent the solution in the earlier comment, i was working on something else with reference to your code where I want to print lines only after I find the keyword until the table starts ,modifying your previous code I was able to perform my required operation on the obtained output but i'd like to optimize it by just printing the lines after I find the keyword(including the line with keyword),in your code it prints all the lines above the table but I need only those after I find the keyword!! thank you.
        hi Marshall, I was hoping not to seek your help unless it was utterly necessary, I figured out how to transpose and replicate values for empty columns, but when I put this code to test I was facing few practical issues with the code you modified for me,
        2017 Position log :Fp379 place: cal time: 23:01:45 | | |Pos |value | |bulk|lot| prev| newest| |# |Locker|(dfg) |(no) |nul|val |Id | val |val | ----------------------------------------------------------- | 0| 1| 302832| -11.88| 1| 0|Pri| 16| 0| | 1| 9| 302836| 11.88| 9| 0|Pri| 10| 0| | 2| 1| 302832| -11.88| 5| 3|Pri| 14| 4| | 3| 3| 302833| 11.88| 1| 0|sec| 12| 0| | 4| 6| 302837| -11.88| 1| 0|Pri| 16| 3| language data: time= 24hrs |no.| name | languages | proficiency | time taken| |_ _| _ _ _| _ _ _ _ _ |_ _ _ _ _ _ _| _ _ _ _ _ | |1 | eliz | English | good | 24 hrs | |2 | susan| Spanish | good | 13 hrs | |3 | danny| Italian | decent | 21 hrs | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |no.| name | age | place | year | |_ _|_ _ _ _|_ _ _ | _ _ _ | _ _ | |1 | sue |33 | NY | 2015 | |2 | mark |28 | cal | 2106 | 2017 Review log :Gt149 place: NY time: 13:31:15 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |no.| name |level | dist | year | |_ _|_ _ _ _|_ _ _ | _ _ _ | _ _ | |1 | sue |96 | Gl | 2015 | |2 | mark |67 | Yt | 2106 |
        the problem I have is that there isn't just one table under a header ,there are multiple tables(of different sizes) under each header as shown, by observation I found out that the page (header+tables) ends only when we find year(2017) in the next line ,the position log here has 3 tables and each with its own header and it ends only when we see the next "2017" (Review log) PS: max tables in a page are 4.