very simple question

Discussion in 'Perl Misc' started by Monkey Boy, Aug 7, 2003.

  1. Monkey Boy

    Monkey Boy Guest

    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
     
    Monkey Boy, Aug 7, 2003
    #1
    1. Advertisements

  2. Monkey Boy wrote:
    >
    > 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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Aug 7, 2003
    #2
    1. Advertisements

  3. Monkey Boy <> wrote:

    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Aug 7, 2003
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    (Monkey Boy) wrote in
    news::

    > 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-----
     
    Eric J. Roode, Aug 7, 2003
    #4
  5. Monkey Boy

    Vlad Tepes Guest

    Abigail <> wrote:
    > Eric J. Roode () wrote on MMMDCXXVIII September
    > && (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;

    --
    Vlad
     
    Vlad Tepes, Aug 7, 2003
    #5
  6. On Thu, 7 Aug 2003 19:32:03 +0000 (UTC),
    Vlad Tepes <> wrote:
    > Abigail <> wrote:


    >> && (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
    --
    |
    Martien Verbruggen | Unix is user friendly. It's just selective
    Trading Post Australia | about its friends.
    |
     
    Martien Verbruggen, Aug 8, 2003
    #6
    1. Advertisements

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. John Hoge

    Very, very, easy question

    John Hoge, May 27, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    549
  2. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    731
    Raymond Hettinger
    Jul 27, 2003
  3. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    2,064
    Merrill & Michele
    Oct 19, 2004
  4. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    730
    jacob navia
    Oct 17, 2004
  5. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    830
    Dave Thompson
    Feb 14, 2005
  6. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,798
  7. Max Norman
    Replies:
    5
    Views:
    291
  8. AMT2K5

    CGI.PM Very very simple question

    AMT2K5, Dec 1, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    199
    Brian McCauley
    Dec 2, 2005
Loading...