Is this considered ugly?

Discussion in 'Perl Misc' started by Kristo, Mar 17, 2005.

  1. Kristo

    Kristo Guest

    I'm in the process of learning Perl and I'm wondering if there's a
    preferred way to do the following. I have a list of strings that
    potentially will contain ampersands. I'd like to replace all
    occurrances of & with & for use on a webpage. Here's what I have
    so far:

    use warnings;
    use strict;

    my ($foo, $bar, $baz) = ("A&B", "C&D", "E&F");
    foreach (($foo, $bar, $baz))
    {
    s/&/&/g;
    }
    print "Foo = $foo, Bar = $bar, Baz = $baz\n";
    __END__

    This outputs "Foo = A&B, Bar = C&D, Baz = E&F" as expected.
    The reason I chose to name each element of the list was so I could use
    the strings later and retain an idea of what each element represented.
    Now, in an attempt to be more concise, I came up with this:

    use warnings;
    use strict;

    my @foo = ("A&B", "C&D", "E&F");
    foreach (@foo)
    {
    s/&/&/g;
    }
    print "Foo = $foo[0], Bar = $foo[1], Baz = $foo[2]\n";
    __END__

    I've succeeded in making the code shorter but at the cost of having to
    remember which list element goes with each subscript. My question to
    everyone is this: which approach is more "Perlish"?

    Thanks in advance.

    Kristo
     
    Kristo, Mar 17, 2005
    #1
    1. Advertising

  2. Kristo

    Paul Lalli Guest

    "Kristo" <> wrote in message
    news:...
    > I'm in the process of learning Perl and I'm wondering if there's a
    > preferred way to do the following. I have a list of strings that
    > potentially will contain ampersands. I'd like to replace all
    > occurrances of & with &amp; for use on a webpage. Here's what I have
    > so far:
    >
    > use warnings;
    > use strict;
    >
    > my ($foo, $bar, $baz) = ("A&B", "C&D", "E&F");
    > foreach (($foo, $bar, $baz))
    > {
    > s/&/&amp;/g;
    > }
    > print "Foo = $foo, Bar = $bar, Baz = $baz\n";
    > __END__
    >
    > This outputs "Foo = A&amp;B, Bar = C&amp;D, Baz = E&amp;F" as

    expected.
    > The reason I chose to name each element of the list was so I could use
    > the strings later and retain an idea of what each element represented.
    > Now, in an attempt to be more concise, I came up with this:
    >
    > use warnings;
    > use strict;
    >
    > my @foo = ("A&B", "C&D", "E&F");
    > foreach (@foo)
    > {
    > s/&/&amp;/g;
    > }
    > print "Foo = $foo[0], Bar = $foo[1], Baz = $foo[2]\n";
    > __END__
    >
    > I've succeeded in making the code shorter but at the cost of having to
    > remember which list element goes with each subscript. My question to
    > everyone is this: which approach is more "Perlish"?


    Neither. :)

    Hashes are used exactly for this reason - to associate a string with the
    value in that position of the structure.

    my %strs = (Foo=>'A&B', Bar=>'C&D', Baz=>'E&F');
    foreach (keys %strs){
    $strs{$_} =~ s/&/&amp;/g;
    }
    print join (', ', map "$_ = $strs{$_}", keys %strs), "\n";

    Also, there already exists a function do do the substitution you're
    looking for. It is CGI::escapeHTML. This will convert & to &amp;, < to
    &lt;, etc:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use CGI qw/escapeHTML/;
    my %strs = (Foo=>'A&B', Bar=>'C&D', Baz=>'E&F', Omega=>'5<8');
    foreach (keys %strs){
    $strs{$_} = escapeHTML($strs{$_});
    }
    print join (', ', map "$_ = $strs{$_}", keys %strs), "\n";
    __END__

    For more information on what I've used here:
    hashes: perldoc perldata
    map: perldoc -f map
    join: perldoc -f map
    escapeHTML: perldoc CGI

    Hope this helps,
    Paul Lalli
     
    Paul Lalli, Mar 17, 2005
    #2
    1. Advertising

  3. Paul Lalli wrote:
    > "Kristo" <> wrote in message
    > news:...
    >
    >>I'm in the process of learning Perl and I'm wondering if there's a
    >>preferred way to do the following. I have a list of strings that
    >>potentially will contain ampersands. I'd like to replace all
    >>occurrances of & with &amp; for use on a webpage. Here's what I have
    >>so far:
    >>
    >>use warnings;
    >>use strict;
    >>
    >>my ($foo, $bar, $baz) = ("A&B", "C&D", "E&F");
    >>foreach (($foo, $bar, $baz))
    >>{
    >> s/&/&amp;/g;
    >>}
    >>print "Foo = $foo, Bar = $bar, Baz = $baz\n";
    >>__END__
    >>
    >>This outputs "Foo = A&amp;B, Bar = C&amp;D, Baz = E&amp;F" as

    >
    > expected.
    >
    >>The reason I chose to name each element of the list was so I could use
    >>the strings later and retain an idea of what each element represented.
    >>Now, in an attempt to be more concise, I came up with this:
    >>
    >>use warnings;
    >>use strict;
    >>
    >>my @foo = ("A&B", "C&D", "E&F");
    >>foreach (@foo)
    >>{
    >> s/&/&amp;/g;
    >>}
    >>print "Foo = $foo[0], Bar = $foo[1], Baz = $foo[2]\n";
    >>__END__
    >>
    >>I've succeeded in making the code shorter but at the cost of having to
    >>remember which list element goes with each subscript. My question to
    >>everyone is this: which approach is more "Perlish"?

    >
    >
    > Neither. :)
    >
    > Hashes are used exactly for this reason - to associate a string with the
    > value in that position of the structure.
    >
    > my %strs = (Foo=>'A&B', Bar=>'C&D', Baz=>'E&F');
    > foreach (keys %strs){
    > $strs{$_} =~ s/&/&amp;/g;
    > }


    And remember that hash keys can't be modified so this will work:

    foreach (%strs)
    {
    s/&/&amp;/g;
    }


    $ perl -le'
    my %strs = qw( Foo A&B Bar C&D Baz E&F );
    print for %strs;
    s/&/&amp;/g for %strs;
    print for %strs;
    '
    Bar
    C&D
    Baz
    E&F
    Foo
    A&B
    Bar
    C&amp;D
    Baz
    E&amp;F
    Foo
    A&amp;B




    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Mar 17, 2005
    #3
  4. Kristo

    Big and Blue Guest

    Kristo wrote:
    >
    > My question to
    > everyone is this: which approach is more "Perlish"?


    Probably neither. On the assumption that other chars may end up there
    which neeed to be handled the Perlish way to do things may be to use the
    module which knows about them.

    use HTML::Entities


    --
    Just because I've written it doesn't mean that
    either you or I have to believe it.
     
    Big and Blue, Mar 18, 2005
    #4
    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. =?Utf-8?B?Q2hhcmxpZSBEaXNvbg==?=

    Datagrid with fewer records than page size is ugly

    =?Utf-8?B?Q2hhcmxpZSBEaXNvbg==?=, Feb 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    457
    Alvin Bruney [MVP]
    Feb 29, 2004
  2. Woody Splawn

    What is considered orthodox?

    Woody Splawn, May 7, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    334
    David Jessee
    May 8, 2004
  3. Replies:
    4
    Views:
    556
    Ajay Patil
    Nov 26, 2003
  4. David Combs
    Replies:
    0
    Views:
    339
    David Combs
    Jul 8, 2004
  5. Robert Mischke
    Replies:
    3
    Views:
    1,559
    Tony Morris
    May 19, 2005
Loading...

Share This Page