map an array but first item different?

J

Justin C

I'm using CGI.pm to generate a table, and within that table I'm using
map to put the contents of an arrayref into cells within each table row:

print table(
{ get_formatting_string($formatting) },
Tr({-class=>'holding'},
[
th([@{$col_heads}]),
map { td($_) } @{$data},
],
),
);

Here's what Data::Dumper says about $data:
$VAR1 = [ '2010-10-01', 'First consignment of two. Order split due to size.', 'John Smith' ];
$VAR2 = [ '2010-10-21', 'Sent email requesting payment. If no joy by follow-up date then set a deadline.', 'Justin' ];

The problem I'm having is that when the second column has a lot of text
it causes the first column to shrink and wrap and this looks terrible. I
can fix this with td({-nowrap=>'nowrap}, $_) if doing things manually,
but I'd still like map to handle this for me because I'm using the same
sub to print tables with different numbers of columns.

How can I set a parameter for the first item while still letting map
handle this? ... or how else could I do this?

Thanks for any help you can give with this.

Justin.
 
R

Randal L. Schwartz

Justin> print table(
Justin> { get_formatting_string($formatting) },
Justin> Tr({-class=>'holding'},
Justin> [
Justin> th([@{$col_heads}]),
Justin> map { td($_) } @{$data},
Justin> ],
Justin> ),
Justin> );

....

Justin> How can I set a parameter for the first item while still letting map
Justin> handle this? ... or how else could I do this?

Just use an aux counter:

my $n = 0;
print table( { ... },
Tr({ ... },
[
th(..),
map {
if (++$n == 1) {
do your thing for first item
} else {
do your thing for remaining items
}
} @$data,
],
),
);

By the way, I think td($data) would have avoided the map. Been a while
since I hacked CGI.pm though.

print "Just another Perl hacker,"; # the original
 
J

John W. Krahn

Justin said:
I'm using CGI.pm to generate a table, and within that table I'm using
map to put the contents of an arrayref into cells within each table row:

print table(
{ get_formatting_string($formatting) },
Tr({-class=>'holding'},
[
th([@{$col_heads}]),
map { td($_) } @{$data},
],
),
);

Here's what Data::Dumper says about $data:
$VAR1 = [ '2010-10-01', 'First consignment of two. Order split due to size.', 'John Smith' ];
$VAR2 = [ '2010-10-21', 'Sent email requesting payment. If no joy by follow-up date then set a deadline.', 'Justin' ];

The problem I'm having is that when the second column has a lot of text
it causes the first column to shrink and wrap and this looks terrible. I
can fix this with td({-nowrap=>'nowrap}, $_) if doing things manually,
but I'd still like map to handle this for me because I'm using the same
sub to print tables with different numbers of columns.

How can I set a parameter for the first item while still letting map
handle this? ... or how else could I do this?

print table(
{ get_formatting_string($formatting) },
Tr({-class=>'holding'},
[
th([@{$col_heads}]),
td( { -nowrap => 'nowrap' }, $data->[ 0 ] ),
map { td( $_ ) } @{ $data }[ 1 .. $#$data ],
],
),
);


John
 
J

Justin C

Justin said:
I'm using CGI.pm to generate a table, and within that table I'm using
map to put the contents of an arrayref into cells within each table row:

print table(
{ get_formatting_string($formatting) },
Tr({-class=>'holding'},
[
th([@{$col_heads}]),
map { td($_) } @{$data},
],
),
);

Here's what Data::Dumper says about $data:
$VAR1 = [ '2010-10-01', 'First consignment of two. Order split due to size.', 'John Smith' ];
$VAR2 = [ '2010-10-21', 'Sent email requesting payment. If no joy by follow-up date then set a deadline.', 'Justin' ];

The problem I'm having is that when the second column has a lot of text
it causes the first column to shrink and wrap and this looks terrible. I
can fix this with td({-nowrap=>'nowrap}, $_) if doing things manually,
but I'd still like map to handle this for me because I'm using the same
sub to print tables with different numbers of columns.

How can I set a parameter for the first item while still letting map
handle this? ... or how else could I do this?

print table(
{ get_formatting_string($formatting) },
Tr({-class=>'holding'},
[
th([@{$col_heads}]),
td( { -nowrap => 'nowrap' }, $data->[ 0 ] ),
map { td( $_ ) } @{ $data }[ 1 .. $#$data ],
],
),
);

Ah! There's always MTOWTDI! Thank you for the suggestion. I'd never
considered that map could would on anything other than the whole array.

Justin.
 
S

sln

Justin said:
I'm using CGI.pm to generate a table, and within that table I'm using
map to put the contents of an arrayref into cells within each table row:

print table(
{ get_formatting_string($formatting) },
Tr({-class=>'holding'},
[
th([@{$col_heads}]),
map { td($_) } @{$data},
],
),
);

Here's what Data::Dumper says about $data:
$VAR1 = [ '2010-10-01', 'First consignment of two. Order split due to size.', 'John Smith' ];
$VAR2 = [ '2010-10-21', 'Sent email requesting payment. If no joy by follow-up date then set a deadline.', 'Justin' ];

The problem I'm having is that when the second column has a lot of text
it causes the first column to shrink and wrap and this looks terrible. I
can fix this with td({-nowrap=>'nowrap}, $_) if doing things manually,
but I'd still like map to handle this for me because I'm using the same
sub to print tables with different numbers of columns.

How can I set a parameter for the first item while still letting map
handle this? ... or how else could I do this?

print table(
{ get_formatting_string($formatting) },
Tr({-class=>'holding'},
[
th([@{$col_heads}]),
td( { -nowrap => 'nowrap' }, $data->[ 0 ] ),
map { td( $_ ) } @{ $data }[ 1 .. $#$data ],
],
),
);

Ah! There's always MTOWTDI! Thank you for the suggestion. I'd never
considered that map could would on anything other than the whole array.

map is being passed a list, not an array.

-sln
 
U

Uri Guttman

JC> On 2010-10-22 said:
print table(
{ get_formatting_string($formatting) },
Tr({-class=>'holding'},
[
th([@{$col_heads}]),
td( { -nowrap => 'nowrap' }, $data->[ 0 ] ),
map { td( $_ ) } @{ $data }[ 1 .. $#$data ],
],
),
);

JC> Ah! There's always MTOWTDI! Thank you for the suggestion. I'd never
JC> considered that map could would on anything other than the whole array.

map doesn't take an array, it takes a list. and the input there is a
just a slice of an array. think of map as a list transform function. it
takes in a list and it returns a list. you can do anything inside the
map to create the output list. it can be shorter, longer or the same
size as the input list. too many newbies think the input and output list
have to be the same size.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top