Output Arrays as columns perl


 
Thread Tools Search this Thread
Top Forums Programming Output Arrays as columns perl
# 1  
Old 09-26-2011
Output Arrays as columns perl

Ok so I am new to perl and I am trying to write a random .cpt file for a GMT script for a class. Anyways I successfully read in the columns out of the file I wanted and I was able to generate 3 arrays full of random numbers that were the same length as the columns and now I just need to output them in the proper format.

I want the data to be formatted like so:
Code:
@Elevation1  @r1   @g1   @b1  @elevation2   @r1   @g1  @b2
       .      .     .     .         .        .     .     .
       .      .     .     .         .        .     .     .
       .      .     .     .         .        .     .     .
       .      .     .     .         .        .     .     .

I really don't know how to go about this. I tried doing a nested loop and filling a 2d array and that didn't work. Is there an easy way to do this in perl?

This is my code that I have written:
Code:
#!/usr/bin/perl

use strict;



my $file = '/Users/Brady/Documents/MUOHIO/Alaska_Station_Map/Alaska_Tremor/custom.cpt';
open(INFO, $file);

my @elevation1;
my @elevation2;
my $line;

while ($line=<INFO>)
{
	push(@elevation1, (split(/\D+/,$line)) [0]);
	push(@elevation2, (split(/\D+/,$line)) [4]);
}

#get the length of the array
my $arrayLength = @elevation1;

#declare variables
my $i;
my @r1;
my @g1;
my @b1;


#Generate random numbers from 0-255 the length of the elevations
for( $i = 0; $i <= $arrayLength; $i++)
	{
		my $range = 255;
		
		my $randomNumber = int(rand($range));
		
		@r1[$i] = $randomNumber;
	}
for( $i = 0; $i <= $arrayLength; $i++)
	{
		my $range = 255;
		
		my $randomNumber = int(rand($range));
		
		@g1[$i] = $randomNumber;
	}	
for( $i = 0; $i <= $arrayLength; $i++)
	{
		my $range = 255;
		
		my $randomNumber = int(rand($range));
		
		@b1[$i] = $randomNumber;
	}

#I have tested the arrays with print join("\n", @arrayName) and it prints the array on the screen

close(INFO);


exit;

# 2  
Old 09-26-2011
Why store it at all? Just print it the way you want:

Code:
for($i=0; $i<10; $i++)
{
        $pfix="";
        for($j=0; $j<10; $j++)
        {
                print $pfix, int(rand(255));
                $pfix="\t";
        }
        print "\n";
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading columns using arrays

Hello, Please help in how to read rows and columns using array and print them. I have below output and i want to store this in array and print the required rows or columns. aaaaaaa 123 bbbbbb 456 ccccccc 888 Use code tags, thanks. (1 Reply)
Discussion started by: Cva2568
1 Replies

2. Shell Programming and Scripting

awk arrays comparing multiple columns across two files.

Hi, I'm trying to use awk arrays to compare values across two files based on multiple columns. I've attempted to load file 2 into an array and compare with values in file 1, but success has been absent. If anyone has any suggestions (and I'm not even sure if my script so far is on the right lines)... (4 Replies)
Discussion started by: hubleo
4 Replies

3. Shell Programming and Scripting

perl: compare two arrays

Hi friends, I want to compare two arrays and find matched one using perl? Also, I want to delete unmatched one. Plz suggest me solution (1 Reply)
Discussion started by: Renesh
1 Replies

4. Shell Programming and Scripting

Arrays in perl

Hi all, I have a log file which has logs. I am reading logs line by line into perl arrays. I want to print all the arrays elements starting from 8(word) to end of the line. print array......array to a new file. and I have to do it in perl as res of the program in perl. Please help me on... (9 Replies)
Discussion started by: firestar
9 Replies

5. Shell Programming and Scripting

Perl Compare 2 Arrays

Hello, Consider the following 2 arrays: Array1 = qw(Fa0/0 Fa0/1 Fa0/2 Fa0/3); Array1 = qw(Fa0/1 Fa0/2 Fa0/3 Fa0/4); I want to compare the following 2 arrays as follows: Take specific action when elements of Array1 that doesn't exist in Array2 (in my example: Fa0/0). Take another... (4 Replies)
Discussion started by: ahmed_zaher
4 Replies

6. Shell Programming and Scripting

Compare arrays in perl

Hello, Let's say that we have the two following arrays @array1= @array2= Is there any easy way to compare these two arrays and print the values that exist in array1 and not in array2 and the values that exist in array2 and not in array1? Regards, Chriss_58 (3 Replies)
Discussion started by: chriss_58
3 Replies

7. Shell Programming and Scripting

perl arrays

Hi I need some help using arrays in perl. I have an array say var and a variable var1. I want to check if the var1 is present in the array. How do I check that ? my @var = 1...10; my $var1 =5; if ( $var1 in @var ) { ....... } else { ....... } Something like above. Can some... (2 Replies)
Discussion started by: ammu
2 Replies

8. Shell Programming and Scripting

Perl array of arrays

Hi, I am trying to assign an array as a value to one of the array element, I mean I have an array @KS and array @kr. I want array @KS to hold @kr as an element. So I am doin this $KS=@kr; But the value stored is number of elements in the @kr array. Can... (2 Replies)
Discussion started by: eamani_sun
2 Replies

9. Shell Programming and Scripting

Perl - Compare 2 Arrays

Hi all, I have the following script where the contents of file1 and file2 would be something like this: file1: 56790,0,0,100998765 89756,0,0,100567876 867645,1,3,678777654 file2: 56790,0,0,100998765 65776,0,0,4766457890 +5896,0,0,675489876 What I then want to do is check if... (4 Replies)
Discussion started by: Donkey25
4 Replies

10. Programming

perl arrays

hello ppl, i'm coding a perl script and i have the following situation: @array1 = ("test1", "test2", "test3"); @array2 = ("something1", "something2", "something1"); $var1 = "with_one_of_the_array1_values"; $var2 = "with_one_of_the_array2_values"; what i want to do is to compare $var1... (2 Replies)
Discussion started by: crashnburn
2 Replies
Login or Register to Ask a Question