very simple question

M

Monkey Boy

How do I initialise (assign value of 0) to multiple variables without
having to manually specify each one.

ie.
$a = 0;
$b = 0;
$c = 0;
$d = 0;
...etc

How to simplify this -
FILE is a mail log file, where I have to repeat the following regex on
multiple email address. I want to have all the email addresses in an
array where I can just call it up on just a single step in the while
loop instead of copying the below line multiple times per email
address.

while (<FILE>){
chomp;
if(/amavis/ && /Passed/ && ! /\<\>/ &&
/\-\>\s\<me\@emailaddress.com\>/i){
$a++;
}

Thanks :)

MB
 
J

John W. Krahn

Monkey said:
How do I initialise (assign value of 0) to multiple variables without
having to manually specify each one.

ie.
$a = 0;
$b = 0;
$c = 0;
$d = 0;
..etc


$a = $b = $c = $d = 0;

Or:

( $a, $b, $c, $d ) = (0) x 4;



John
 
T

Tad McClellan

Monkey Boy said:
Subject: very simple question


Please put the subject of your article in the Subject of your article!

How do I initialise (assign value of 0) to multiple variables without
having to manually specify each one.


I do not know how to initialize a variable without specifying
what variable it is that is to be initialized...

ie.
$a = 0;
$b = 0;
$c = 0;
$d = 0;


.... but I do know how to do it better than that at least:

$a = $b = $c = $d = 0;

if(/amavis/ && /Passed/ && ! /\<\>/ &&
^ ^
^ ^
/\-\>\s\<me\@emailaddress.com\>/i){
^ ^ ^ ^
^ ^ ^ ^

None of those backslashes are needed.

Angle brackets and hyphens are not special in regular expressions.
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) (Monkey Boy) wrote in
How do I initialise (assign value of 0) to multiple variables without
having to manually specify each one.

ie.
$a = 0;
$b = 0;
$c = 0;
$d = 0;
..etc

As with many things in Perl, there are several ways:

$a = $b = $c = $d = 0;

($a, $b, $c, $d) = (0, 0, 0, 0);

$_ = 0 for $a, $b, $c, $d;

And probably others.

- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPzHKhWPeouIeTNHoEQIRjgCdHYcmeez8bWNaVl37krGgbIuh2i8AoO0C
KZIR7iZjy4cWv5vzbqaHhTvv
=TA4P
-----END PGP SIGNATURE-----
 
V

Vlad Tepes

Abigail said:
Eric J. Roode ([email protected]) wrote on MMMDCXXVIII September
&& (e-mail address removed) (Monkey Boy) wrote in
&& > How do I initialise (assign value of 0) to multiple variables without
&& > having to manually specify each one.
&& >
&& > ie.
&& > $a = 0;
&& > $b = 0;
&& > $c = 0;
&& > $d = 0;
&& > ..etc
&&
&& As with many things in Perl, there are several ways:
&&
&& $a = $b = $c = $d = 0;
&&
&& ($a, $b, $c, $d) = (0, 0, 0, 0);
&&
&& $_ = 0 for $a, $b, $c, $d;
&&
&& And probably others.

$$_ = 0 for qw /a b c d/;

eval "\$$_ = 0" for qw /a b c d/;

What about dropping the qw?

$$_ = 0 for a..d;

map $$_ = 0, a..d;
 
M

Martien Verbruggen

&& (e-mail address removed) (Monkey Boy) wrote in
&& > How do I initialise (assign value of 0) to multiple variables without
&& > having to manually specify each one.
&& >
&& > ie.
&& > $a = 0;
&& > $b = 0;
&& > $c = 0;
&& > $d = 0;
&& > ..etc
[snip]
$$_ = 0 for qw /a b c d/;

eval "\$$_ = 0" for qw /a b c d/;

[Added comments for people who are thinking of copying this without
knowing what it does.]

This won't work under strict, of course, and should only be used if
you know why you should use it. It uses symbolic references, which are
not a good idea in general, even though they are useful in some
specific cases (this one not being one of them, IMO).
What about dropping the qw?

$$_ = 0 for a..d;

map $$_ = 0, a..d;

And, apart from using symbolic references, this will also generate
warnings if they are enabled, because you're using bare strings.

$$_ = 0 for 'a' .. 'd';

Martien
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top