I have a file with a random number of words (well, numbers) on each line, and I need to reformat them so that there are a regular number of numbers per line.

For example:

The original file might looks like:

34

4 32 56 23

45 33 56 55

33 76 23

I just need to put that into two rows of six:

34 4 32 56 23 45

33 56 55 33 76 23

In the real file, there are thousands of rows of course.

At the moment, Im splitting each line on space and storing the values in an array, doing a scalar on the line array to get the number of values per line, which is fine, but how do I then add each value to a new array until it reaches the desired number, and then start on the next array?

(Just so you know Im not some kid trying to get you to do his homework, Im a geologist, Im trying to 'unwrap' ascii well log data into a standard format)

There are several ways to accomplish this task. My first thought would be to put the entire file into an array (stripping the line terminator as you build the array), then use the modulus operator as you output the data to determine when to add the line terminator.

At the moment, Im splitting each line on space and storing the values in an array, doing a scalar on the line array to get the number of values per line, which is fine, but how do I then add each value to a new array until it reaches the desired number, and then start on the next array?

split(), print(), count: split() a line into an array, then print() the numbers in the array one by one, and count after you print. After counting, use the modulus operator to tell you when to print a newline:

Hmm, actually could I ask another silly question or should I start a new thread?

When Im reading the files and storing them into an array line by line, the script acts differently if the files were created on Linux or Windows. I'm not sure which way round it is, but I think that Linux files are fine (each line going into the array as a single element) but the script puts the whole Windows file into the first element of the array.

(Might be the other way round because Im not always sure of the origin of the files)

I'm using Tie::File. Is this something to do with linefeed/carriage returns?

I've tried searching for \r in both filetypes to see if this might be the problem, and cant find a carriage return in any of them

Hmm, actually could I ask another silly question or should I start a new thread?

You should start a new thread with each question. The main reason being that only those subscribed to this thread will read your new question. Starting a new thread will give you a wider audience. It also means that people with a similar problem to yours can find the answer easier.