Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > map an array but first item different?

Reply
Thread Tools

map an array but first item different?

 
 
Justin C
Guest
Posts: n/a
 
      10-21-2010
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:umper 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.

--
Justin C, by the sea.
 
Reply With Quote
 
 
 
 
Randal L. Schwartz
Guest
Posts: n/a
 
      10-21-2010
>>>>> "Justin" == Justin C <(E-Mail Removed)> writes:

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

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      10-22-2010
Justin C wrote:
> 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:umper 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
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      10-25-2010
On 2010-10-22, John W. Krahn <(E-Mail Removed)> wrote:
> Justin C wrote:
>> 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:umper 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.
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      10-25-2010
On Mon, 25 Oct 2010 14:15:46 +0100, Justin C <(E-Mail Removed)> wrote:

>On 2010-10-22, John W. Krahn <(E-Mail Removed)> wrote:
>> Justin C wrote:
>>> 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:umper 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
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      10-25-2010
>>>>> "JC" == Justin C <(E-Mail Removed)> writes:

JC> On 2010-10-22, John W. Krahn <(E-Mail Removed)> wrote:
>> 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

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing all but first item in drop down list Chris Javascript 7 10-25-2008 09:10 PM
STL: Map of maps possible, but no multi-map of maps? Workarounds? Marcus C++ 2 12-09-2005 06:34 AM
using map item without assigning onw first? Gernot Frisch C++ 1 10-28-2004 09:14 AM
Display Related Item or Add Item ssoss ASP .Net 2 09-18-2003 11:35 PM
Datalist selects Item after first click, but does apply the SelectedItemTemplate after the second click only Dirk Meusel ASP .Net 1 08-19-2003 09:56 AM



Advertisments