LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-20-2011, 08:54 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
Array to columns basics


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.

Code:
#!/usr/bin/perl -w 
use strict ; 
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";
foreach my $line(@array) {
my ($fish , $animal, $insect ) = split ( ',', $line); 
print "$fish\n"; 
 }
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.
Old 07-20-2011, 09:13 PM   #2
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
When i get rid of the foreach loop it prints out in format, but only
prints out the first line.

Code:
#!/usr/bin/perl -w 
use strict ; 
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";
my ($fish , $animal, $insect ) = @array ;  
print "$fish  $animal    $insect \n";
I would like to get the same structure, however for each element in the array.
all 15 elements
This is results of above:
Code:
casper-compaq@compaq:~/work_perl$ /usr/bin/perl  shray
FISH--------- ANIMAL---------  INSECT
striped bass  german shepard    housefly
 
Old 07-20-2011, 09:41 PM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
this does not work either
Code:
#!/usr/bin/perl -w 
use strict ; 
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";
while (<@array>) { 
my ($fish , $animal, $insect ) =  split(',', $_) ;  
print "$fish\n"; 
sleep 1; 
}
 
Old 07-20-2011, 10:20 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Your logic is flawed. You are to busy thinking of the outcome but not thinking about the input.

What if I asked you to print the 3 x N elements of the array (ie 3rd, 6th, 9th, etc). How would accomplish this?
 
Old 07-21-2011, 12:08 AM   #5
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
Every third element -
this is the only way that I know.
Code:
#!/usr/bin/perl -w 
use strict ; 
my $i ; 
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' ); 
@array = grep {not ++$i % 3} @array; 
 print "@array\n";
which is going to give us just the insects.
Code:
housefly praying mantis red ant gypsy moth japanese beetle
 
Old 07-21-2011, 12:12 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
hmmm ... what if i said you need to combine a for loop with your previous construct:
Code:
my ($fish , $animal, $insect ) = @array ;
I am not trying to be difficult, but more the old adage of teaching you to fish instead of giving them to you
 
Old 07-21-2011, 12:34 AM   #7
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
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 ($i = 2 ; $i <=$#array; $i = $i + $j  ){ 
print "                               $array[$i]\n"; 
sleep 1; 
}
produces
Code:
FISH--------- ANIMAL---------  INSECT
                               housefly
                               praying mantis
                               red ant
                               gypsy moth
                               japanese beetle

Last edited by casperdaghost; 07-21-2011 at 11:17 PM.
 
Old 07-21-2011, 12:56 AM   #8
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
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
 
Old 07-21-2011, 12:58 AM   #9
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
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.
 
Old 07-21-2011, 01:47 AM   #10
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
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.
 
1 members found this post helpful.
Old 07-21-2011, 02:12 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Ok ... so it turns out we have both learnt something. My thinking was that every time you did the following within a loop:
Code:
my ($fish , $animal, $insect ) = @array ;
That you would get the next 3 items of the array. Instead you just get the same 3 items each time

So this is the way I went:
Code:
#!/usr/bin/perl -v

use strict;
use warnings;

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";

my $count = 1;

while ( scalar ( @array ) > 0 )
{
    print shift ( @array );

    if ( $count++ % 3 > 0 )
    {
        print " --- ";
    }
    else
    {
        print "\n";
    }
}
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.
 
1 members found this post helpful.
Old 07-21-2011, 04:00 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Ok ... found kind of what I was thinking of earlier:
Code:
#!/usr/bin/perl

use warnings;
use strict;

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";

my @temp = @array;

for ( my $i = 1; $i <= (@array / 3); $i++ )
{
        my @temp2 = splice( @temp, 0, 3);

        print "@temp2\n";
}
 
1 members found this post helpful.
Old 07-21-2011, 05:27 AM   #13
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
I'm definitely not a Perl person, but I was thinking of
Code:
#!/usr/bin/perl -w 

use warnings;
use strict;

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 (my $i = 0; $i < $#array; $i += 3) {
	printf "%-15s %-15s %s\n", @array[ ($i) .. ($i+2) ];
}
or, with less formatting (just horizontal tabs between columns) but adjustable number of columns,
Code:
#!/usr/bin/perl -w 

use warnings;
use strict;

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" ); 

my @header = ("FISH", "ANIMAL", "INSECT");

$, = "\t";
$\ = "\n";

print @header;
for (my $i = 0; $i < $#array; $i += 1 + $#header) {
	print @array[ ($i) .. ($i + $#header) ];
}

Last edited by Nominal Animal; 07-21-2011 at 05:30 AM.
 
2 members found this post helpful.
Old 07-21-2011, 09:42 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Much cleaner I knew there had to be better ways.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash: Calculating in array and storing result in array ghantauke Linux - Newbie 6 12-02-2010 12:28 PM
Server array (mysql, php) -> gboolean array in c client kalleanka Programming 1 07-27-2010 06:50 AM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration