Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Problem reading data file

by coec (Chaplain)
on Jul 22, 2002 at 03:44 UTC ( [id://183934]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, thanks to all those that helped with my previous post.
My problem now is I always seem to only read the first line of my data file. I need to read the file 8 times (I know this is not ideal) and compare the variable $Pos against the first field in each line of data ($NewPos)
my @Column_Info = ( [ 1024, 1067 ], # Column 1 start stop [ 1068, 1093 ], # Column 2 start stop [ 1094, 1137 ], # Column 3 start stop [ 1138, 1163 ], # Column 4 start stop [ 1164, 1207 ], # Column 5 start stop [ 1208, 1233 ], # Column 6 start stop [ 1234, 1277 ], # Column 7 start stop [ 1278, 1321 ] ); # Column 8 start stop <snip> open(IN,"cat /tmp/show_library.$$|"); while (<IN>) { while ($Pos <= $EndPos) { $_ =~ s/^ //; chomp; printf "DEBUG: String: '%s', Position: %d, New Position: %d, Label: %s +\n", $_, $Pos, $NewPos, $NewLabel; if ($Pos == 1024) { printf "%s Diag tape\n", $Pos; $Library[$Count][$Column_Count] = $_; } else { ($NewPos, $NewLabel) = split(' ', $_, 9999); printf "DEBUG: String: '%s', Position: %d, New Position: %d, Label: %s +\n", $_, $Pos, $NewPos, $NewLabel; <snip> } $Column_Count++; $Pos++; } }
Any ideas where I am going wrong here?
TIA
CC

Replies are listed 'Best First'.
Re: Problem reading data file
by jarich (Curate) on Jul 22, 2002 at 07:26 UTC
    G'day Coec

    As Zaxo has said you need to step back from your problem and rethink it through. Once you know what it is (as a whole) that you're trying to do, tell us and let us suggest solutions. Offering part solutions to us is great, but it's very hard for us to give you reasonable advice if you won't tell us what the overall picture should look like.

    Having said that I'm going to take a wild guess at what your problem is and what you're trying to do to solve it and offer a solution to match that.

    (Some of) The Problem
    You want to read in a file and create a two dimensional array of the form:

    Library ( 0 -> [all the data from the file which starts with a + value between 1 and n] 1 -> [all the data from the file which starts with a + value between n and 2n] .... 7 -> [all the data from the file which starts with a + value between 7n and 8n] );
    Ideally you'd like to be able to change the size of each partition or add more partitions if necessary without having to change all the code.

    Is that correct? *jarich hopes so.* Try this.

    The structure I have here is much more simple than what you've suggested. I realise that I might be completely wrong about what you're trying to achieve but I hope that this might give you some assistance.

    Of course, if all you're trying to do is to read in the file in such a way that the first 8th of it is in Library[0], and the second eigth is in Library[1] etc, that's a whole other story. :)

    Hope it helps

    jarich

    Update:Whoops! I forgot to label the next. Have done so now.

Re: Problem reading data file
by Zaxo (Archbishop) on Jul 22, 2002 at 04:37 UTC

    Your code is not very clear about what you want to do. How about stepping back and decoding the whole record with something like:

    my @records; { # local $/ = \1322; # may be desirable open IN, '<', '/path/to/file.data' or die $!; @records = map { [ unpack "C1024 C44 C26 C44 C26 C44 C26 C44 C26", $_ ] } <IN>; close IN or die $!; }
    modulo some adjustments for line endings and the like. It is usually less troublesome to code in chunks like that than to fuss with index twiddling. I've set this up to read the whole file, but the unpack idea will work as well for handling one line at a time.

    Perhaps you do, you seem to have snipped parts of the code setting up your variables, but use strict; and use warnings; will help you find some kinds of weakness in your code.

    After Compline,
    Zaxo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://183934]
Approved by Zaxo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found