K
Kristo
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
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