Writing a simple loop in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Writing a simple loop in perl
# 1  
Old 04-08-2009
Writing a simple loop in perl

Dear all,

I am a complete newbie to (perl) programming and I am completely stuck in a project. I have the following arrays

@array_1
@array_2

@array_A
@array_B
@array_C

Each element pair of array_1 and array_2 is linked to all elements of a third array (e.g. array_A, array_B or array_C). I would like to put the arrays into a data structure of the following form:

$array_1[1];$array_2[1];$array_A[1]
$array_1[1];$array_2[1];$array_A[2]
$array_1[1];$array_2[1];$array_A[3]
$array_1[1];$array_2[1];$array_A[4]
$array_1[1];$array_2[1];$array_A[5]

$array_1[1];$array_2[2];$array_B[1]
$array_1[1];$array_2[2];$array_B[2]
$array_1[1];$array_2[2];$array_B[3]
$array_1[1];$array_2[2];$array_B[4]

$array_1[1];$array_2[3];$array_C[1]
$array_1[1];$array_2[3];$array_C[2]
$array_1[1];$array_2[3];$array_C[3]
$array_1[1];$array_2[3];$array_C[4]
...

I know how to loop over the elements of the arrays [i]. However, my problem is that I do not know how i can loop over _A, _B and _C. Any comments would be appreciated. Thank you in advance.

Best regards

Philipp
# 2  
Old 04-08-2009
some nice looping trouble...
no free lunch. just brute force.

Code:
#!perl

@array_1 = qw( ant bat cat dog emu );
@array_2 = qw( fox gnu hen ibis jay );
@array_A = qw( kiwi lion mink nene owl );
@array_B = qw( piq quail rat tern uakari );
@array_C = qw( vole wren yak zebu );

foreach $anum ( 0 .. $#array_1 ){
  foreach $bnum ( 0 .. $#array_A ){
    print "$array_1[$anum]:$array_2[$anum]:$array_A[$bnum]\n";
    }
  }

foreach $anum ( 0 .. $#array_1 ){
  foreach $bnum ( 0 .. $#array_B ){
    print "$array_1[$anum]:$array_2[$anum]:$array_B[$bnum]\n";
    }
  }

foreach $anum ( 0 .. $#array_1 ){
  foreach $bnum ( 0 .. $#array_C ){
    print "$array_1[$anum]:$array_2[$anum]:$array_C[$bnum]\n";
    }
  }

# 3  
Old 04-08-2009
He posted this on another forum with a different description of the arrays. I suggested he learn hashes and complex data structures (and provided a link to some tutorials) as quickly as possible because this is torture and is an attempt to do something more than likely done better another way once he knows a bit more perl.

Last edited by KevinADC; 04-08-2009 at 01:22 PM.. Reason: corrected some spelling mistakes
# 4  
Old 04-08-2009
Cheers

Thank you very much . I'll look into what you suggested and also learn more Perl (including hashes) as Kevin suggested here and elsewhere.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Writing a for loop to manipulate multiple files

Hi, I have 1000 text files in a folder that are labeled data1.txt all the way to data1000.txt. I want to write a small script that manipulates the text files in this way: (1) cut the 2nd and 9th columns of the text files (2) sort by the numerical value in the 9th column (3) then save the rows... (3 Replies)
Discussion started by: evelibertine
3 Replies

2. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

3. Shell Programming and Scripting

writing a simple script to get total number of cpus/cores in server

Hi, I am very new to scripting and I wanted to write a unix shell script which can give me, 1)number of cpu's in a box 2)number of cores per cpu 3)total number of cores in abox (ie multiplying 1&2) I am also trying to figure out how to check if hyper-threading is enabled in the... (8 Replies)
Discussion started by: steven12
8 Replies

4. Shell Programming and Scripting

problem writing a simple c shell script

#!/bin/csh echo hello world this is what i got in a text file called ss1. i type "chmod 755 ss1.txt" to make it executable. then when i type ss1 or ss1.txt it says "ss1 command not found" what am i doing wrong? (19 Replies)
Discussion started by: pantelis
19 Replies

5. Shell Programming and Scripting

Help with writing simple bash script

I want to write a bash script to: 1. Send an email from localhost to an external gmail account. (gmail then automatically forwards the message back to a pop account on the same server. 2. Script waits 3 minutes then checks to see if the email arrived, and if not, it sends an email to... (9 Replies)
Discussion started by: sallyanne
9 Replies

6. Red Hat

Writing simple python setup commands

Building software in most languages is a pain. Remember ant build.xml, maven2 pom files, and multi-level makefiles? Python has a simple solution for building modules, applications, and extensions called distutils. Disutils comes as part of the Python distribution so there are no other packages... (0 Replies)
Discussion started by: Linux Bot
0 Replies

7. Programming

Tools for writing a simple syntax checker?

I'm trying to write a small utility for syntax checking. I've tried using Flex/Bison, but these seem too advanced for my task. A simpler tool would be appreciated. (1 Reply)
Discussion started by: Ilja
1 Replies

8. UNIX for Dummies Questions & Answers

HELP! writing simple shell script

how would i write a shell script to show the number of lines in which int variable appears in a c++ program. (3 Replies)
Discussion started by: deadleg
3 Replies

9. UNIX for Dummies Questions & Answers

Writing to variables within a loop

I have a loop which will go round "i" number of times and return a value each time. Is there a way to set a variable(i) such that I will end up with variable(1), variable(2), variable(3) etc. for however many times the loop went round? eg: for (i=1;i<=3;i++) --do some sums--... (0 Replies)
Discussion started by: Sniper Pixie
0 Replies

10. UNIX for Dummies Questions & Answers

Need help writing simple script

I'm trying to write a simple unix script that will delete files after 30 days of being created. I've never done this before but conceptually it sounds easy. Here is what I'm trying to do: Get System Date Get File Date If (sysdate-filedate>30days) rm file All of these files are contained... (1 Reply)
Discussion started by: tamdoankc
1 Replies
Login or Register to Ask a Question