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:
Tables, Display 2 cols only...

 



crashinto
Novice

Sep 8, 2001, 1:56 PM

Post #1 of 16 (7223 views)
Tables, Display 2 cols only... Can't Post

I am wanting to make a list with a flat file database I already have, set up as:

test | Test | Test |
test2 | test2 | test2|
test3 | test3| test3|

But I want it to print on the page as:

Test Test2
test3 Test4

How do I get it to do that?

Thanks in advance.




PerlKid
stranger

Sep 8, 2001, 11:49 PM

Post #2 of 16 (7218 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post

um... what code are you using in the first place to read the file? Are you using a DBI driver like CSV or a simple while loop and a split? Specifics always help solve problems...

Anyway, for example If the data is @data, then to print the second and third values you'd use $data[1] and $data[2].

--Drew
http://www.camelsoup.com

Code
s;[\d\$&(\^)];;g+s;\.; ;g+s;(.)(..);$2$1;g+print,if$_='&61k4I.)l6il.edn7(K2e^ny$';



crashinto
Novice

Sep 9, 2001, 2:49 AM

Post #3 of 16 (7215 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post

open(FILE, "shopdb.txt");
while(<FILE>) {
my ($item, $num, $price) = split /\|/;
}
close(FILE);



Jean
User


Sep 9, 2001, 6:02 AM

Post #4 of 16 (7213 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post

Your example was:
Test Test2
test3 Test4

Please clarify what function is to be performed,
i.e. how Test becomes Test2 and Test3 becomes Test4.

Jean Spector
QA Engineer @ [url=www.msafe.com]mSAFE Ltd.
jean.spector@softhome.net


crashinto
Novice

Sep 9, 2001, 7:01 AM

Post #5 of 16 (7211 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post

What i mean, Each of Test Test2 Test3 and Test4 is seprate, then the list goes on until I stop.. Each of them is a record in the flat file database, with what i am doing that is the item name, Instead of placing the item names like

test
test2
test3
test4
I want to display in a table like

test test2
test3 test4

I know their is a way to do it, because its on mostly all the link scripts.



Jean
User


Sep 9, 2001, 10:36 PM

Post #6 of 16 (7203 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post

You'll have to use html tables that I'm not familiar with (as with any other web based concepts). The only thing that I can think of is make a loop

Code
for ($i = 0; $i <= $#data; $i+=2) { 
somefunction($data[$i], $data[$i+1]);
}

instead of

Code
foreach $data (@data) {}

That is supposed to give you two items per table row.

Hope this helps, though I'm not sure whether such an answer would help myself Smile


Jean Spector
QA Engineer @ mSAFE Ltd.
jean.spector@softhome.net


mhx
Enthusiast / Moderator

Sep 10, 2001, 12:53 AM

Post #7 of 16 (7201 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post

You can use something like this to distribute your data to a table with n columns:

Code
#!/bin/perl -w 
use CGI qw(:standard *table);
use strict;

my $cols = 2; # number of columns goes here
my @data = qw(this data is displayed in multiple columns);

# the following line is just to fill empty fields
push @data, ('&nbsp;')x($cols-@data%$cols);

# print the table
print start_table { border => 1 };
while( my @row = splice @data, 0, $cols ) {
print Tr td [@row];
}
print end_table;

Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



crashinto
Novice

Sep 10, 2001, 3:44 AM

Post #8 of 16 (7196 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post

Thanks, mhx, but how would I get that to print the data from my database?

open(FILE, "shopdb.txt");
while(<FILE>) {
my ($item, $num, $price) = split /\|/;
my $cols = 2;
# number of columns goes here
my @data = qw($item, $num, $price);
# the following line is just to fill empty fields
push @data, (' ')x($cols-@data%$cols);
# print the table
print start_table { border => 1 };
while( my @row = splice @data, 0, $cols ) {
print Tr td [@row];
}
print end_table;


}


close(FILE);

Just prints 2 columns with $item in it, not the what the name of the item thats in shopdb.txt.
Shopdb.txt looks like:

test | Test | Test |
test2 | test2 | test2|
test3 | test3| test3|


(This post was edited by crashinto on Sep 10, 2001, 2:47 AM)


Jean
User


Sep 10, 2001, 4:06 AM

Post #9 of 16 (7193 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post

First fill the @data with ALL the lines from the file and then print to table, i.e. put the following piece of code outside your main loop.
Second: you are using the @data array to hold one string - make it hold all the strings.

Code
print start_table { border => 1 }; 
while( my @row = splice @data, 0, $cols ) {
print Tr td [@row];
}
print end_table;

Hopefully this is intelligible enough ;-)

Jean Spector
QA Engineer @ mSAFE Ltd.
jean.spector@softhome.net


crashinto
Novice

Sep 10, 2001, 1:56 PM

Post #10 of 16 (7185 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post

Anyway you could explain more??

To see what I am getting go here,

http://www.crashinto.com/cgi-bin/easyshop.cgi

As you can see it has the correct amount of colums and rows, but it displays as $item and not the item name.



mhx
Enthusiast / Moderator

Sep 10, 2001, 2:04 PM

Post #11 of 16 (7184 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post


Code
my @data = qw($item, $num, $price);

That's not what you want. qw quotes words. It's equivalent to

Code
my @data = ('$item', '$num', '$price');

You want (perhaps):

Code
my @data = ($item, $num, $price);

Hope this gets you a little further...
If you're still in trouble, just ask Smile

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



crashinto
Novice

Sep 10, 2001, 2:47 PM

Post #12 of 16 (7182 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post

Thanks. Knew it was something easy I was missing.



crashinto
Novice

Sep 10, 2001, 5:17 PM

Post #13 of 16 (7178 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post

Ok I know Im asking a bit too much here, but I now cannot think of how to make it, print just the $item in 2 straight columns, right now it;s printing:

Item Name Item Number
Price

I need
Item Name Item Name 2
Item Name3 Item Name 4

Thanks for all the help, I'll keep trying all I know to see if I can get it.

I have tried just using @data = ($item); but that prints

Item Name
Item Name2
Item Name3

with the code you gave me.





mhx
Enthusiast / Moderator

Sep 10, 2001, 10:48 PM

Post #14 of 16 (7174 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post

I'm not quite getting your problem, so here's a complete script that should work for you.

Code
#!/bin/perl -w 
use CGI qw(:standard);
use strict;

# Read the data into array of arrays
my @data = map [split /\|/], <DATA>;

# Print a multicolumn table of the first data column
print table( {border => 1},
multicol_table(2, map $_->[0], @data) );

# Multicolumn table subroutine
sub multicol_table # ( no_columns, data_array )
{
my($cols, @data) = @_;
my(@rows, @row);
push @rows, [@row] while @row = splice @data, 0, $cols;
push @{$rows[-1]}, ('&nbsp;')x($cols-@{$rows[-1]});
Tr [map td($_), @rows];
}

__DATA__
Item1|1|$8
Item2|1|$13
Item3|2|$21
Item4|3|$34
Item5|5|$55

For the sake of readability, I've used the DATA filehandle to read from. You can open any file you like and use another file handle instead of DATA. (In case you're not familiar with DATA, it treats the section followed by __DATA__ as a file so you can read from it. You just don't have to open or close it.)
So, what's that doing?

Code
my @data = map [split /\|/], <DATA>;

This line reads the complete file into an array of arrays. That way, assumed you've got the three columns item, count, price in your data file, you could access the price of the fourth item using $data[3][2]. The item name of the first item would be $data[0][0].

Code
print table( {border => 1}, 
multicol_table(2, map $_->[0], @data) );

The interesting part about this is the map, which extracts only the first element (the item name) of each of the arrays referenced by the elements of @data (read: extracts only the first column of your data file). If you're not familiar with data structures in Perl, you may find perldoc perldata, perldoc perldsc and especially perldoc perllol quite useful.

The multicol subroutine is just a wrapper for the code I've posted earlier. You pass it a number of columns and an array of data, and it will return the corresponding body of a table.

Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



crashinto
Novice

Sep 12, 2001, 1:42 PM

Post #15 of 16 (7164 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post

Ok I hate to do this to you all again, but I can't get it to be a link now. I can specify one entry to be a link by doing $data[0][0] = qq|<a..........|;

How Would I get it to make all entries a link?



mhx
Enthusiast / Moderator

Sep 12, 2001, 9:35 PM

Post #16 of 16 (7161 views)
Re: Tables, Display 2 cols only... [In reply to] Can't Post

I've added a fourth column to the data that contains the name of an HTML document to link to. If you can get the name of your link otherwise, that's ok, but I'm not too creative when I just got out of bed. Wink

Code
#!/bin/perl -w 
use CGI qw(:standard);
use strict;

# Read the data into array of arrays
my @data = map { chomp; [split /\|/] } <DATA>;

# Print a multicolumn table of the first data column
my $base = 'http://some.server.uh';
my @td = map a({href=>"$base/$_->[3]"}, $_->[0]), @data;
print table( {border => 1}, multicol_table(2, @td) );

# Multicolumn table subroutine
sub multicol_table # ( no_columns, data_array )
{
my($cols, @data) = @_;
my(@rows, @row);
push @rows, [@row] while @row = splice @data, 0, $cols;
push @{$rows[-1]}, ('&nbsp;')x($cols-@{$rows[-1]});
Tr [map td($_), @rows];
}

__DATA__
Item1|1|$8|item1a.html
Item2|1|$13|item2b.html
Item3|2|$21|item3c.html
Item4|3|$34|item4d.html
Item5|5|$55|item5e.html

Now, what's different? First, I added a chomp to the first map. Its absence was my fault in the last post. The only thing I addionally changed was the remapping of the data that is passed to multicol_table(). The subroutine itself didn't change.
Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"


 
 


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

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