map an array but first item different?

Discussion in 'Perl Misc' started by Justin C, Oct 21, 2010.

  1. Justin C

    Justin C Guest

    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.

    --
    Justin C, by the sea.
    Justin C, Oct 21, 2010
    #1
    1. Advertising

  2. >>>>> "Justin" == Justin C <> 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
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.posterous.com/ for Smalltalk discussion
    Randal L. Schwartz, Oct 21, 2010
    #2
    1. Advertising

  3. 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::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
    --
    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
    John W. Krahn, Oct 22, 2010
    #3
  4. Justin C

    Justin C Guest

    On 2010-10-22, John W. Krahn <> 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::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.
    Justin C, Oct 25, 2010
    #4
  5. Justin C

    Guest

    On Mon, 25 Oct 2010 14:15:46 +0100, Justin C <> wrote:

    >On 2010-10-22, John W. Krahn <> 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::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
    , Oct 25, 2010
    #5
  6. Justin C

    Uri Guttman Guest

    >>>>> "JC" == Justin C <> writes:

    JC> On 2010-10-22, John W. Krahn <> 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.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Oct 25, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dirk Meusel
    Replies:
    1
    Views:
    2,995
    Dirk Meusel
    Aug 19, 2003
  2. Gernot Frisch
    Replies:
    1
    Views:
    293
    John Harrison
    Oct 28, 2004
  3. Marcus
    Replies:
    2
    Views:
    577
    Marcus
    Dec 9, 2005
  4. nw
    Replies:
    5
    Views:
    369
    Permostat
    Mar 12, 2010
  5. Chris
    Replies:
    7
    Views:
    96
    Gregor Kofler
    Oct 25, 2008
Loading...

Share This Page