Mass value assigment
my ($a, $b, $c, $d, $e) = "test";
How I can assign "test" to all the list items? e.g. $a to $e
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Mass value assigment
On Monday 08 Mar 2010 18:00:10 Ryan Chan wrote:
> my ($a, $b, $c, $d, $e) = "test";
>
> How I can assign "test" to all the list items? e.g. $a to $e
First of all, you should not name lexical variables "$a" and "$b" because
these are built-in Perl variables: http://perldoc.perl.org/perlvar.html .
Otherwise you can assign several variables the same initial value using the x
operator:
my ($x, $y, $z) = (("test") x 3);
Note that both pairs of parentheses on the right are required.
That put aside, I should note that assigning several different variables the
same value smells of code-duplication and varvarname (see
http://perl.plover.com/varvarname.html ). Please consider using an array or a
hash instead or code a more well-rounded code.
Regards,
Shlomi Fish
--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Optimising Code for Speed - http://shlom.in/optimise
Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Mass value assigment
On Mon, Mar 8, 2010 at 10:00 PM, Ryan Chan <ryanchan404 [at] gmail.com> wrote:
> my ($a, $b, $c, $d, $e) = "test";
>
> How I can assign "test" to all the list items? e.g. $a to $e
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>
A bit ugly but works:
salmin [at] salmin:~/test$ ./massass.pl
'test' 'test' 'test' 'test'
salmin [at] salmin:~/test$ cat massass.pl
#!/usr/bin/perl
use strict;
use warnings;
my ($a, $b, $c, $d) = split /,/, "test," x 4;
print "'$a' '$b' '$c' '$d'\n";
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Mass value assigment
Shlomi Fish wrote:
> On Monday 08 Mar 2010 18:00:10 Ryan Chan wrote:
>> my ($a, $b, $c, $d, $e) = "test";
>>
>> How I can assign "test" to all the list items? e.g. $a to $e
>
> First of all, you should not name lexical variables "$a" and "$b" because
> these are built-in Perl variables: http://perldoc.perl.org/perlvar.html .
>
> Otherwise you can assign several variables the same initial value using the x
> operator:
>
> my ($x, $y, $z) = (("test") x 3);
>
> Note that both pairs of parentheses on the right are required.
No they are not. Only the parentheses around "test" are required.
Another way to do it:
$_ = "test" for my ($x, $y, $z);
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Mass value assigment
On Mon, Mar 08, 2010 at 08:00:10AM -0800, Ryan Chan wrote:
> my ($a, $b, $c, $d, $e) = "test";
>
> How I can assign "test" to all the list items? e.g. $a to $e
>
my ($a, $b, $c, $d, $e);
$a=$b=$c=$d=$e="test";
Mike
--
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Mass value assigment
>>>>> "AS" == Alexey Salmin <alexey.salmin [at] gmail.com> writes:
AS> my ($a, $b, $c, $d) = split /,/, "test," x 4;
ewww. do you know about the list form of x? no need to build the string
and then split which is a major waste of cpu.
my ($a, $b, $c, $d) = ("test") x 4;
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/