[19525] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1720 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Sep 9 18:10:26 2001

Date: Sun, 9 Sep 2001 15:10:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1000073410-v10-i1720@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Sun, 9 Sep 2001     Volume: 10 Number: 1720

Today's topics:
        printing all even || odd numbers from array <ds@ss.com>
    Re: printing all even || odd numbers from array <tony_curtis32@yahoo.com>
    Re: printing all even || odd numbers from array <ds@ss.com>
    Re: printing all even || odd numbers from array (Sam Holden)
    Re: printing all even || odd numbers from array <godzilla@stomp.stomp.tokyo>
    Re: printing all even || odd numbers from array <krahnj@acm.org>
    Re: printing all even || odd numbers from array <philip@zaynar.demon.co.uk>
    Re: printing all even || odd numbers from array <uwe@richard-schneider.de>
    Re: printing all even || odd numbers from array (Randal L. Schwartz)
    Re: printing all even || odd numbers from array <bart.lateur@skynet.be>
        Redirect eval output to socket problem <javaneese@hotmail.com>
    Re: Redirect eval output to socket problem <bart.lateur@skynet.be>
    Re: Reverse DNS lookup (David Efflandt)
    Re: Reverse DNS lookup <godzilla@stomp.stomp.tokyo>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sun, 09 Sep 2001 17:35:48 GMT
From: GunneR <ds@ss.com>
Subject: printing all even || odd numbers from array
Message-Id: <jcupptcchdq1s9fchguv2bstsiq792bhb1@4ax.com>

Just trying to print out all even numbers (not even elements) from an
array. Someone told me to use the % operator but I cant seem to get it
working:

#perl

@array1 = qw(1 2 3 4 5 6);

$b = 0;

foreach (@array1) {
    if (! $array1[$b] % 2) {
        print "$array1[$b]\n";
        $b++;
    }
}

Want it to print "2 4 6"

Any advice?

Thanks!


------------------------------

Date: Sun, 09 Sep 2001 12:57:38 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: printing all even || odd numbers from array
Message-Id: <87y9nojlgt.fsf@limey.hpcc.uh.edu>

>> On Sun, 09 Sep 2001 17:35:48 GMT,
>> GunneR <ds@ss.com> said:

> Just trying to print out all even numbers (not even
> elements) from an array. Someone told me to use the %
> operator but I cant seem to get it working:

> @array1 = qw(1 2 3 4 5 6);
> $b = 0;
> foreach (@array1) {
>   if (! $array1[$b] % 2) {
>     print "$array1[$b]\n";
>     $b++;
>   }
> }

> Want it to print "2 4 6"

This looks awfully C-like.  In general you don't need to
use subscripts in perl, you simply iterate over the
elements rather than each index:

    @a = qw(1 2 3 4 5 6);
    foreach my $e (@a) {
        print "$e " if $e % 2 == 0;
    }
    print "\n";

hth
t
-- 
Yes way!  Mmmmkay?


------------------------------

Date: Sun, 09 Sep 2001 17:50:46 GMT
From: GunneR <ds@ss.com>
Subject: Re: printing all even || odd numbers from array
Message-Id: <ebvppt4mom86gapbdjvak64h7t637te1p1@4ax.com>

Never mind! :)

#perl

@array1 = qw(1 2 3 4 5 6);

$b = 0;

foreach (@array1) {
    if (($array1[$b] % 2) == 0) {
        print "$array1[$b]\n";
        $b++;
        next;
    }
    else { $b++; next;
    }
}

Never Perl on an empty stomach....

On Sun, 09 Sep 2001 17:35:48 GMT, GunneR <ds@ss.com> wrote:

>Just trying to print out all even numbers (not even elements) from an
>array. Someone told me to use the % operator but I cant seem to get it
>working:
>
>#perl
>
>@array1 = qw(1 2 3 4 5 6);
>
>$b = 0;
>
>foreach (@array1) {
>    if (! $array1[$b] % 2) {
>        print "$array1[$b]\n";
>        $b++;
>    }
>}
>
>Want it to print "2 4 6"
>
>Any advice?
>
>Thanks!



------------------------------

Date: 9 Sep 2001 18:10:45 GMT
From: sholden@pgrad.cs.usyd.edu.au (Sam Holden)
Subject: Re: printing all even || odd numbers from array
Message-Id: <slrn9pnc55.8ih.sholden@pgrad.cs.usyd.edu.au>

On Sun, 09 Sep 2001 17:50:46 GMT, GunneR <ds@ss.com> wrote:
>Never mind! :)
>
>#perl
>
>@array1 = qw(1 2 3 4 5 6);
>
>$b = 0;
>
>foreach (@array1) {
>    if (($array1[$b] % 2) == 0) {
>        print "$array1[$b]\n";
>        $b++;
>        next;
>    }
>    else { $b++; next;
>    }
>}

Maybe you should read the docs on what foreach does... That is one
strange way to code a loop....

There's some unnecesary cut-n-paste code as well...



------------------------------

Date: Sun, 09 Sep 2001 12:15:24 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: printing all even || odd numbers from array
Message-Id: <3B9BBFCC.3CF57F57@stomp.stomp.tokyo>

GunneR wrote:
 
(snipped)

> Just trying to print out all even numbers (not even elements) from an
> array. Someone told me to use the % operator but I cant seem to get it
> working:
 

#!perl

@array1 = qw(1 2 3 4 5 6);

for (@array1)
 {
  if (substr ($_, -1, 1) =~ /[02468]/)
   { print $_; }
 }

exit;


Godzilla!
--
Sun Sep  9 10:11:26 2001 - RESTRICTED FILE REDIRECT:
   - DNS:  - IPA: 207.225.221.67
   - System: 
   - Redirect URL: /default.ida


------------------------------

Date: Sun, 09 Sep 2001 19:43:27 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: printing all even || odd numbers from array
Message-Id: <3B9BC6D8.6D60EE56@acm.org>

GunneR wrote:
> 
> Just trying to print out all even numbers (not even elements) from an
> array. Someone told me to use the % operator but I cant seem to get it
> working:
> 
> #perl
> 
> @array1 = qw(1 2 3 4 5 6);
> 
> $b = 0;
> 
> foreach (@array1) {
>     if (! $array1[$b] % 2) {
>         print "$array1[$b]\n";
>         $b++;
>     }
> }
> 
> Want it to print "2 4 6"

The Perl way:

#perl -w
use strict;

my @array1 = qw(1 2 3 4 5 6);

foreach my $b ( @array1 ) {
    unless ( $b % 2 ) {
        print "$b\n";
    }
}
__END__

The C way:

#perl -w
use strict;

my @array1 = qw(1 2 3 4 5 6);

for ( my $b = 0; $b < @array1; ++$b ) {
    if ( ! $array1[$b] % 2 ) {
        print "$array1[$b]\n";
    }
}
__END__



John
-- 
use Perl;
program
fulfillment


------------------------------

Date: Sun, 9 Sep 2001 21:00:18 +0100
From: Philip Taylor <philip@zaynar.demon.co.uk>
Subject: Re: printing all even || odd numbers from array
Message-Id: <aeG4tFASp8m7EwGy@zaynar.demon.co.uk>

In article <3B9BC6D8.6D60EE56@acm.org>, "John W. Krahn" <krahnj@acm.org>
writes
>GunneR wrote:
>> 
>> Just trying to print out all even numbers (not even elements) from an
>> array. Someone told me to use the % operator but I cant seem to get it
>> working:
>
>The Perl way:
>
>#perl -w
>use strict;
>
>my @array1 = qw(1 2 3 4 5 6);
>
>foreach my $b ( @array1 ) {
>    unless ( $b % 2 ) {
>        print "$b\n";
>    }
>}


Or with less lines (and less understandability):

my @array1 = qw(1 2 3 4 5 6);
print grep !($_&1), @array1;

__END__
-- 
Philip Taylor
philip @ zaynar . demon . co . uk

http://robowarriors.ultrastore.com/legoworld.shtml
  -- If the Earth was made of Lego...


------------------------------

Date: Sun, 09 Sep 2001 22:11:51 +0200
From: Uwe Schneider <uwe@richard-schneider.de>
To: GunneR <ds@ss.com>
Subject: Re: printing all even || odd numbers from array
Message-Id: <3B9BCD07.63469932@richard-schneider.de>

GunneR wrote:
> 
> Just trying to print out all even numbers (not even elements) from an
> array. Someone told me to use the % operator but I cant seem to get it
> working:
> 
> #perl
> 
> @array1 = qw(1 2 3 4 5 6);
> 
> $b = 0;
> 
> foreach (@array1) {
>     if (! $array1[$b] % 2) {
>         print "$array1[$b]\n";
>         $b++;
>     }
> }
> 
> Want it to print "2 4 6"

What's wrong with grep()?

#!/usr/bin/perl
 
use strict;
my @ary = qw(1 2 3 4 5 6);
my @bry = grep( $_ % 2 == 0,@ary);
print join(" ",@bry),"\n";


U. 

-- 
Uwe Schneider       | Telefon +49 7251 / 82587
Karlsdorfer Str. 31 | Mail    pi@richard-schneider.de
DE-76646 Bruchsal   | http://www.richard-schneider.de/uwe
Linux - OS al dente!


------------------------------

Date: 09 Sep 2001 13:21:54 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: printing all even || odd numbers from array
Message-Id: <m1g09wnmhp.fsf@halfdome.holdit.com>

>>>>> "GunneR" == GunneR  <ds@ss.com> writes:

GunneR> @array1 = qw(1 2 3 4 5 6);
[...]
GunneR> Want it to print "2 4 6"

    for (0..10) {
      my @array1 = (1..$_);
      print "$_: >@array1[map 1+2*$_, 0..($#array1-1)/2]<\n";
    }

results in...

    0: ><
    1: ><
    2: >2<
    3: >2<
    4: >2 4<
    5: >2 4<
    6: >2 4 6<
    7: >2 4 6<
    8: >2 4 6 8<
    9: >2 4 6 8<
    10: >2 4 6 8 10<

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


------------------------------

Date: Sun, 09 Sep 2001 21:25:23 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: printing all even || odd numbers from array
Message-Id: <a8nnptsuaifesk1p3sk1v1ke3bjnkas0pg@4ax.com>

GunneR wrote:

>$b = 0;
>
>foreach (@array1) {
>    if (($array1[$b] % 2) == 0) {
>        print "$array1[$b]\n";
>        $b++;
>        next;
>    }
>    else { $b++; next;
>    }
>}

Even with such an awkward loop, you can still improve on the neatness of
the code:

	$b = 0;
	foreach (@array1) {
	    if (($array1[$b] % 2) == 0) {
	        print "$array1[$b]\n";
	    }
	} continue {
	    $b++;
	}

but making $b the loop variable sounds like a better idea:

	foreach my $b (0 .. $#array1) {
	    if (($array1[$b] % 2) == 0) {
	        print "$array1[$b]\n";
	    }
	}

Well alright, the perlish way would be to recognise the fact that in
such a foreach loop, the loop variable gets set to an alias of each item
in turn. The default loop variable is $_. So:

	$b = 0;
	foreach (@array1) {
	    if ($_ % 2) == 0) {
	        print "$_\n";
	    }
	} continue {
	    $b++;
	}

(Perhaps you still have a use for that $b?)

-- 
	Bart.


------------------------------

Date: Sun, 09 Sep 2001 20:04:26 GMT
From: Daniel <javaneese@hotmail.com>
Subject: Redirect eval output to socket problem
Message-Id: <3B9BCA66.2E302E55@hotmail.com>

Hi all pro's!

I have a problem with redirecting the output from eval to a socket (NS)
A code example that shows what I mean:

######################
$command = "print \"this text wants to be printed on the socket NS\"";
print NS "line one\n";
eval($command);   # I want to redirect the output from eval to NS...
print NS "line two\n";
######################

the example prints "line one" and "line two" to the socket NS but the
output from eval comes to STDOUT.
I have tried several ways of redirection but obviosly I make something
wrong...  arghhh?%!#
How to do to redirect the output from eval to the socket NS?
All help would be higly appreciated!

Thanks in advance,
Daniel



------------------------------

Date: Sun, 09 Sep 2001 21:37:23 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Redirect eval output to socket problem
Message-Id: <k7onptggbs4sc9uii3clkgskq15ee6pfg6@4ax.com>

Daniel wrote:

>$command = "print \"this text wants to be printed on the socket NS\"";
>print NS "line one\n";
>eval($command);   # I want to redirect the output from eval to NS...
>print NS "line two\n";
>
>the example prints "line one" and "line two" to the socket NS but the
>output from eval comes to STDOUT.

Does

	select NS;

do any good?

-- 
	Bart.


------------------------------

Date: Sun, 9 Sep 2001 16:19:08 +0000 (UTC)
From: efflandt@xnet.com (David Efflandt)
Subject: Re: Reverse DNS lookup
Message-Id: <slrn9pn5jr.p2b.efflandt@typhoon.xnet.com>

On Sun, 9 Sep 2001 11:46:22 +0100, Gareth Horth <Gareth@Horth.F9.co.uk> wrote:
> Hi,
> 
> How can I do a reverse DNS lookup from a perl script?
> If I do 'whois 1.2.3.4' on the CGI server it doesn't find a match - but when
> I do the same on my computer it works.
> Is there another way to do the lookup?

This is not a Perl question if it is a shell command and is not a DNS
question because whois will not tell you the hostname for an IP.  whois
_might_ tell you who owns the IP block and maybe what domain name and 
nameservers are used for it (if any).

It is possible that whois on the webserver is a different path/version or
not configured with a default host for looking up IP's.  See 'man whois'
or what whois returns with no parameters.  Examples for IP range lookup:

whois (Unix):  whois -h whois.arin.net 1.2.3.4
fwhois (Linux):  whois 1.2.3.4@whois.arin.net

But you also want to end any shell command with 2>&1 so you can see any 
error message (whois is slow lately and may tend to timeout in CGI).

-- 
David Efflandt - All spam is ignored - http://www.de-srv.com/
http://www.autox.chicago.il.us/  http://www.berniesfloral.net/
http://cgi-help.virtualave.net/  http://hammer.prohosting.com/~cgi-wiz/


------------------------------

Date: Sun, 09 Sep 2001 12:35:50 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Reverse DNS lookup
Message-Id: <3B9BC496.BF298AE7@stomp.stomp.tokyo>

Gareth Horth wrote:

> How can I do a reverse DNS lookup from a perl script?
> If I do 'whois 1.2.3.4' on the CGI server it doesn't find a match - but when
> I do the same on my computer it works.
> Is there another way to do the lookup?


  use Socket;
  $ENV{REMOTE_HOST}  = gethostbyaddr (inet_aton ($ENV{REMOTE_ADDR}), AF_INET);


Godzilla!


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 1720
***************************************


home help back first fref pref prev next nref lref last post