ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have an array with 15 elements, and I want to break it down into three columns.
When the array is split into a the three elements - however on the iteration, it does not conform to that structure.
i figured that it would just print out into one column - all the fish under the column FIDH. However, it just prints out the whole array.
Code:
FISH ---- ANIMAL ---- INSECT
striped bass
german shepard
housefly
bluefish
monkey
praying mantis
grouper
mountain cat
red ant
mahi mahi
wolf
gypsy moth
clown fish
grizzly bear
japanese beetle
what I am looking for is a array strategy that will print out
in this format:
Code:
FISH ---- ANIMAL ---- INSECT
striped bass german shepard housefly
bluefish monkey praying mantis
grouper mountain cat red ant
mahi mahi wolf gypsy moth
clown fish grizzly bear Japanese beetle
The data in the array is consistent - I need it to newline every third element.
Last edited by casperdaghost; 07-20-2011 at 09:52 PM.
Click here to see the post LQ members have rated as the most helpful post in this thread.
The third column is nested under the first, and it prints out 5 times for every one.
i run into ths issue alot, i dont really understand how to scope this.
Code:
#!/usr/bin/perl -w
use strict ;
my $i;
my $j = 3;
my @array = ("striped bass", "german shepard", "housefly", "bluefish", "monkey", "praying mantis", "grouper", "mountain cat", "red
ant","mahi mahi", "wolf", "gypsy moth", "clown fish", " grizzly bear", "japanese beetle" );
print "FISH--------- ANIMAL--------- INSECT\n";
for ($a =0; $a<=$#array; $a = $a + $j){
print "$array[$a]\n";
for ($i = 2 ; $i <=$#array; $i = $i + $j ){
print " $array[$i]\n";
}
}
casper-compaq@compaq:~/work_perl$ /usr/bin/perl shray5
FISH--------- ANIMAL--------- INSECT
striped bass
housefly
praying mantis
red ant
gypsy moth
japanese beetle
bluefish
housefly
praying mantis
red ant
gypsy moth
japanese beetle
if i scope each 'for' loop independently, as opposed to nesting each one under the other, and have one each 'for' loop execute one after the other i get this (i omit the middle loop for clarity) :
Code:
FISH--------- ANIMAL--------- INSECT
striped bass
bluefish
grouper
mahi mahi
clown fish
housefly
praying mantis
red ant
gypsy moth
japanese beetle
I dont care when three for loops execute, concurrently or one after the other, I just want all the data lined up in one table
Last edited by casperdaghost; 07-21-2011 at 01:03 AM.
Use a loop over the data rows, and either print all columns on that row at once, or use an inner loop to print just the columns on that row. If you have a fixed number of columns, the one print command is usually a better choice. If you have a loop, you need to remember to add an end-of-this-row (newline!) after the last column on each row.
Now I am sure there are perlites out there to show you much more efficient ways (maybe even what I was trying to do),
but this seems to work, although the formatting needs some work.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.