CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Array problem

 



jonny5509
Novice

Mar 20, 2010, 1:13 PM

Post #1 of 7 (957 views)
Array problem Can't Post

Hello

I'm completely stuck.

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)



Any help much appreciated!


FishMonger
Veteran / Moderator

Mar 20, 2010, 1:41 PM

Post #2 of 7 (954 views)
Re: [jonny5509] Array problem [In reply to] Can't Post

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.


7stud
Enthusiast

Mar 21, 2010, 2:47 AM

Post #3 of 7 (948 views)
Re: [jonny5509] Array problem [In reply to] Can't Post


Quote
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:


Code
    if ( ($count % 6) == 0) { 
print "\n"}


Easy, peasy.


(This post was edited by 7stud on Mar 21, 2010, 2:50 AM)


shawnhcorey
Enthusiast


Mar 21, 2010, 5:52 AM

Post #4 of 7 (940 views)
Re: [jonny5509] Array problem [In reply to] Can't Post

Try:

Code
#!/usr/bin/perl 

use strict;
use warnings;

my $number_per_line = 6;
my @buffer = ();

while( <DATA> ){
for my $number ( split ){
push @buffer, $number;
if( @buffer >= $number_per_line ){
print "@buffer\n";
@buffer = ();
}
}
}

# now print any remaining
print "@buffer\n" if @buffer;

__DATA__
34

4 32 56 23

45 33 56 55

33 76 23


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


jonny5509
Novice

Mar 21, 2010, 6:54 AM

Post #5 of 7 (936 views)
Re: [shawnhcorey] Array problem [In reply to] Can't Post

Both methods worked. You have no idea how much pain you've just saved me. Thanks guys.


jonny5509
Novice

Mar 21, 2010, 8:27 AM

Post #6 of 7 (930 views)
Re: [jonny5509] Array problem [In reply to] Can't Post

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



Thanks again fior the previous help!



Jon


shawnhcorey
Enthusiast


Mar 21, 2010, 11:47 AM

Post #7 of 7 (920 views)
Re: [jonny5509] Array problem [In reply to] Can't Post


In Reply To
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.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives