[27900] in Perl-Users-Digest
Perl-Users Digest, Issue: 9264 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jun 8 11:25:34 2006
Date: Thu, 8 Jun 2006 08:25:27 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 8 Jun 2006 Volume: 10 Number: 9264
Today's topics:
Parsing Perl.. Please explain.. <jeffledger@gmail.com>
Re: Parsing Perl.. Please explain.. <tadmc@augustmail.com>
Re: Parsing Perl.. Please explain.. <joe@inwap.com>
Re: Parsing Perl.. Please explain.. <jeffledger@gmail.com>
Premature end of script headers <madan.narra@gmail.com>
Re: Premature end of script headers <john@castleamber.com>
Problem with SMPT mail <lstern@capgemini.fr>
Re: Problem with SMPT mail <nobull67@gmail.com>
Re: Problem with SMPT mail <nobull67@gmail.com>
Re: Problem with SMPT mail <tintin@invalid.invalid>
Re: Problem with SMPT mail <lstern@capgemini.fr>
Re: read from ttySx buffer 4096 problem <Ra.spam-niet@t.k.pl>
Re: read from ttySx buffer 4096 problem <benmorrow@tiscali.co.uk>
Returning the first n elements of a list <ro.naldfi.scher@gmail.com>
Re: Returning the first n elements of a list <rvtol+news@isolution.nl>
Re: Returning the first n elements of a list <peace.is.our.profession@gmx.de>
Re: Returning the first n elements of a list <peace.is.our.profession@gmx.de>
Re: Returning the first n elements of a list <ro.naldfi.scher@gmail.com>
Re: Returning the first n elements of a list <peace.is.our.profession@gmx.de>
Re: Returning the first n elements of a list <nobull67@gmail.com>
Re: Returning the first n elements of a list <tzz@lifelogs.com>
Re: Returning the first n elements of a list <tadmc@augustmail.com>
Re: Returning the first n elements of a list <wahab@chemie.uni-halle.de>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 6 Jun 2006 18:07:59 -0700
From: "Oldbitcollector" <jeffledger@gmail.com>
Subject: Parsing Perl.. Please explain..
Message-Id: <1149642479.069355.76510@u72g2000cwu.googlegroups.com>
I've got a routine that looks something like this...
for( $i = 1; $i <= $pop->Count(); $i++ ) {
foreach( $pop->Head( $i ) ) {
/^(From|Subject):\s+/i && print $_, "\n";
}
}
This parses two lines from an email header. (From & Subject)
I'm having some trouble understanding how this works.
I'm shooting for a parse that would put this lines into variables as
well as print them.
something like
$from = /^(From):\s+/i
print $from;
$subject = /^(Subject):\s+/i
print $subject;
Could someone take a little time to explain how this works so I can
apply it correctly to my own code requirements. Thanks
Jeff
------------------------------
Date: Tue, 6 Jun 2006 22:54:09 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Parsing Perl.. Please explain..
Message-Id: <slrne8cjf0.nto.tadmc@magna.augustmail.com>
Oldbitcollector <jeffledger@gmail.com> wrote:
> I've got a routine that looks something like this...
>
> for( $i = 1; $i <= $pop->Count(); $i++ ) {
foreach my $i ( 1 .. $pop->Count() ) {
> foreach( $pop->Head( $i ) ) {
> /^(From|Subject):\s+/i && print $_, "\n";
> }
> }
>
> This parses two lines from an email header. (From & Subject)
>
> I'm having some trouble understanding how this works.
When you have things "and"ed together, as soon as you encounter
a false value, you can stop evaluating clauses because you already
know that the whole expression must be false.
Perl takes that same short cut.
When the m// is false, the print() is never evaluated.
The print() is evaluated only when the m// is true.
The body of the for loop is equivalent to:
if ( /^(From|Subject):\s+/i ) {
print $_, "\n";
}
> I'm shooting for a parse that would put this lines into variables as
> well as print them.
if ( /^(From|Subject):\s+/i ) {
print $_, "\n";
$hash{$1} = $_;
}
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 07 Jun 2006 01:36:13 -0700
From: Joe Smith <joe@inwap.com>
Subject: Re: Parsing Perl.. Please explain..
Message-Id: <SZedneOpFvWRDRvZnZ2dnUVZ_oGdnZ2d@comcast.com>
Oldbitcollector wrote:
> something like
>
> $from = /^(From):\s+/i
> print $from;
>
> $subject = /^(Subject):\s+/i
> print $subject;
my($from,$subject);
while(...) {
/^From:\s+/i and $from = $_;
/^Subject:\s+/i and $subject = $_;
}
print $from if defined $from;
print $subject if defined $subject;
A better way would be to use a hash instead of separate values.
You can use
/^(From|Subject):\s/i and $requested_headers{lc $1} = $_;
or
$requested_headers{lc $1} = $_ if /^(From|Subject):\s/i;
to do that.
Both statements do the same thing; it's a matter of style whether you prefer
(condition) and $variable = $value;
versus
$variable = $value if (condition);
-Joe
------------------------------
Date: 7 Jun 2006 15:45:23 -0700
From: "Oldbitcollector" <jeffledger@gmail.com>
Subject: Re: Parsing Perl.. Please explain..
Message-Id: <1149720322.942994.30750@i39g2000cwa.googlegroups.com>
Thanks guys,
With that explaination and a couple more hours of study, I'm starting
to understand how this works.
Jeff
------------------------------
Date: 7 Jun 2006 22:39:42 -0700
From: "madan" <madan.narra@gmail.com>
Subject: Premature end of script headers
Message-Id: <1149745182.190659.35920@i39g2000cwa.googlegroups.com>
hi all,
i am getting the error "Premature end of script headers" when ever i
include use Win32::ODBC.
i need to connect to database...
my code is as below..
#! /usr/bin/perl
push(@INC,"/cgi-bin");
require("cgi-lib.pl");
require("CGI.pm");
$query=new CGI;
print &PrintHeader;
print $query->start_html;
print $query->startform("get","/cgi-bin/data.cgi");
use Win32::ODBC;
$dbh= new Win32::ODBC("dsn=madan; uid=madan; pwd=madan");
print "<input type=text name=dsn value=$dsn><br>";
print "<center>The List of Tables That are specified in the DataBase
you Entered are:<br>";
if(!$dbh)
{
print "failure in connecting ".Win32::ODBC::Error();
exit();
}
else
{
print "Connection Sucessful<br>";
}
$dbh->Connection();
foreach $Temp ($dbh->TableList())
{
$Table = $Temp;
print "<option value=$Table>$Table";
}
print "</select><br><br>";
print "<br><br><input type=submit value=Submit>";
print $query->endform;
print $query->end_html
thanks in advance
madan chowdary
Edit/Delete Message
------------------------------
Date: 8 Jun 2006 06:15:40 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: Premature end of script headers
Message-Id: <Xns97DCCD561B3Ecastleamber@130.133.1.4>
"madan" <madan.narra@gmail.com> wrote:
> hi all,
> i am getting the error "Premature end of script headers" when ever i
> include use Win32::ODBC.
> i need to connect to database...
> my code is as below..
Do yourself a favour, and throw away the book you have been using. Or
better, burn it, it should not fall into the hands of someone else.
> #! /usr/bin/perl
>
> push(@INC,"/cgi-bin");
You might want to use
use lib '/cgi-bin';
moreover, are you sure that your cgi-bin is under the root (not as in
Document Root, where it *shouldn't* be IMO).
> require("cgi-lib.pl");
Sounds like a very, very outdated CGI library to me, which you probably
don't want to use.
> require("CGI.pm");
use strict;
use warnings;
use CGI::Carp 'fatalsToBrowser';
use CGI;
> $query=new CGI;
better:
my $cgi = CGI->new;
> print &PrintHeader;
You really need the & there? (I don't think so). Use:
print $cgi->header( 'text/html' );
> print $query->start_html;
> print $query->startform("get","/cgi-bin/data.cgi");
See, and that's why I recommend $cgi, since it's silly to let a query
start html.
> use Win32::ODBC;
Better move this up, probably best place is just after use CGI;
> $dbh= new Win32::ODBC("dsn=madan; uid=madan; pwd=madan");
>
> print "<input type=text name=dsn value=$dsn><br>";
> print "<center>The List of Tables That are specified in the DataBase
> you Entered are:<br>";
>
> if(!$dbh)
> {
> print "failure in connecting ".Win32::ODBC::Error();
> exit();
you might want to do this earlier on, before you start an HTML page for
example.
> }
> else
> {
> print "Connection Sucessful<br>";
> }
> $dbh->Connection();
>
> foreach $Temp ($dbh->TableList())
> {
> $Table = $Temp;
> print "<option value=$Table>$Table";
> }
Uhm, why a "$Temp"?
Disclaimer: typos and other errors because of 1:15 AM :-D.
--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/
------------------------------
Date: Wed, 7 Jun 2006 17:00:53 +0200
From: "L. Stern" <lstern@capgemini.fr>
Subject: Problem with SMPT mail
Message-Id: <4486ea25$0$29812$626a54ce@news.free.fr>
Hello
I am installing Bugzilla 2.22 on a Windows 2000 and I have some problem to
configure the mail notification.
In fact no email are sent. My problem is on the use of perl script to send
email.
I have made some simple Perl script (either via Mailer or directly via SMTP)
and it seems OK for me (no error), but no email are sent.
The scripts and logs are below
Exemple 1
Script1
use Mail::Mailer;
$mailer = new Mail::Mailer('smtp', Server => "10.135.1.3",Debug => 2);
#$mailer = new Mail::Mailer 'testfile', Server => "10.135.1.3";
$mailer->open({From => 'Laurent.Stern@darty.fr',
To => 'lstern@capgemini.fr',
Subject => "test 1234",})
or die "Can't open: $!\n";
print $mailer "Ceci est le texte";
$mailer->close();
Trace1
Net::SMTP>>> Net::SMTP(2.29)
Net::SMTP>>> Net::Cmd(2.26)
Net::SMTP>>> Exporter(5.58)
Net::SMTP>>> IO::Socket::INET(1.29)
Net::SMTP>>> IO::Socket(1.29)
Net::SMTP>>> IO::Handle(1.25)
Net::SMTP=GLOB(0x1a4ed48)<<< 220 KIX001 ESMTP Service (Lotus Domino Release
5.0.11) ready at Tue, 30 May 2006 14:25:55 +0200
Net::SMTP=GLOB(0x1a4ed48)>>> EHLO localhost.localdomain
Net::SMTP=GLOB(0x1a4ed48)<<< 250-KIX001 Hello localhost.localdomain
([136.0.1.0]), pleased to meet you
Net::SMTP=GLOB(0x1a4ed48)<<< 250-HELP
Net::SMTP=GLOB(0x1a4ed48)<<< 250-SIZE 51200000
Net::SMTP=GLOB(0x1a4ed48)<<< 250 PIPELINING
Net::SMTP=GLOB(0x1a4ed48)>>> MAIL FROM:<postmaster@s0i600.intranet.darty.fr>
Net::SMTP=GLOB(0x1a4ed48)<<< 250 postmaster@s0i600.intranet.darty.fr...
Sender OK
Net::SMTP=GLOB(0x1a4ed48)>>> RCPT TO:<lstern@capgemini.fr>
Net::SMTP=GLOB(0x1a4ed48)<<< 250 lstern@capgemini.fr... Recipient OK
Net::SMTP=GLOB(0x1a4ed48)>>> DATA
Net::SMTP=GLOB(0x1a4ed48)<<< 354 Enter message, end with "." on a line by
itself
Net::SMTP=GLOB(0x1a4ed48)>>> Subject: test 1234
Net::SMTP=GLOB(0x1a4ed48)>>> X-Mailer: Mail::Mailer[v1.67] Net::SMTP[v2.29]
Net::SMTP=GLOB(0x1a4ed48)>>> To: lstern@capgemini.fr
Net::SMTP=GLOB(0x1a4ed48)>>> From: Laurent.Stern@darty.fr
Net::SMTP=GLOB(0x1a4ed48)>>> Ceci est le texte
Net::SMTP=GLOB(0x1a4ed48)>>> .
Net::SMTP=GLOB(0x1a4ed48)<<< 250 Message accepted for delivery
Net::SMTP=GLOB(0x1a4ed48)>>> QUIT
Net::SMTP=GLOB(0x1a4ed48)<<< 221 KIX001 SMTP Service closing transmission
channel
Exemple 2
Script 2
use Net::SMTP;
my $smtp = Net::SMTP->new('10.135.1.3', Timeout => 30, Debug => 2, ); #
connect to SMTP server
$smtp->auth;
$smtp->mail('Laurent.Stern@darty.fr');# use the sender's adress here
$smtp->to('lstern@capgemini.fr'); #recipient's address
$smtp->data(); # Start the mail
$smtp->datasend('test');
$smtp->dataend(); # Finish sending the mail
$smtp->quit; # Close the SMTP connection
exit;
Trace2
Net::SMTP>>> Net::SMTP(2.29)
Net::SMTP>>> Net::Cmd(2.26)
Net::SMTP>>> Exporter(5.58)
Net::SMTP>>> IO::Socket::INET(1.29)
Net::SMTP>>> IO::Socket(1.29)
Net::SMTP>>> IO::Handle(1.25)
Net::SMTP=GLOB(0x1c29bd8)<<< 220 KIX001 ESMTP Service (Lotus Domino Release
5.0.11) ready at Tue, 6Jun 2006 15:04:11 +0200
Net::SMTP=GLOB(0x1c29bd8)>>> EHLO localhost.localdomain
Net::SMTP=GLOB(0x1c29bd8)<<< 250-KIX001 Hello localhost.localdomain
([136.0.1.0]), pleased to meet you
Net::SMTP=GLOB(0x1c29bd8)<<< 250-HELP
Net::SMTP=GLOB(0x1c29bd8)<<< 250-SIZE 51200000
Net::SMTP=GLOB(0x1c29bd8)<<< 250 PIPELINING
Net::SMTP=GLOB(0x1c29bd8)>>> MAIL FROM:<Laurent.Stern@darty.fr>
Net::SMTP=GLOB(0x1c29bd8)<<< 250 Laurent.Stern@darty.fr... Sender OK
Net::SMTP=GLOB(0x1c29bd8)>>> RCPT TO:<lstern@capgemini.fr>
Net::SMTP=GLOB(0x1c29bd8)<<< 250 lstern@capgemini.fr... Recipient OK
Net::SMTP=GLOB(0x1c29bd8)>>> DATA
Net::SMTP=GLOB(0x1c29bd8)<<< 354 Enter message, end with "." on a line by
itself
Net::SMTP=GLOB(0x1c29bd8)>>> test
Net::SMTP=GLOB(0x1c29bd8)>>> .
Net::SMTP=GLOB(0x1c29bd8)<<< 250 Message accepted for delivery
Net::SMTP=GLOB(0x1c29bd8)>>> QUIT
Net::SMTP=GLOB(0x1c29bd8)<<< 221 KIX001 SMTP Service closing transmission
channel
Thanks in advance for any help on my problem.
Regards
Laurent STERN
------------------------------
Date: 7 Jun 2006 10:01:24 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: Problem with SMPT mail
Message-Id: <1149699683.990967.145720@u72g2000cwu.googlegroups.com>
L. Stern wrote:
> Hello
>
> I am installing Bugzilla 2.22 on a Windows 2000 and I have some problem to
> configure the mail notification.
> In fact no email are sent. My problem is on the use of perl script to send
> email.
How have you concluded that?
> I have made some simple Perl script (either via Mailer or directly via SMTP)
> and it seems OK for me (no error), but no email are sent.
If you connect to the SMTP by hand using a telnet client (e.g. PuTTY or
HyperTerminal) and send mail that way does it work?
Are there any error in the mailer's log?
Are errors being sent to the return path address
<postmaster@s0i600.intranet.darty.fr>?
Have you tried sending a test mail (by another mechanism) to
<postmaster@s0i600.intranet.darty.fr> to make sure that it is working?
------------------------------
Date: 7 Jun 2006 10:11:57 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: Problem with SMPT mail
Message-Id: <1149700317.346308.197780@u72g2000cwu.googlegroups.com>
Brian McCauley wrote:
> Are errors being sent to the return path address
> <postmaster@s0i600.intranet.darty.fr>?
Note: if you want to control the return-path (aka envelope-from)
address used by Mail::Mailer::smtp you should set $ENV{MAILADDRESS} .
This is less than clear in the documentation.
------------------------------
Date: Thu, 8 Jun 2006 08:04:36 +1200
From: "Tintin" <tintin@invalid.invalid>
Subject: Re: Problem with SMPT mail
Message-Id: <448723dd$0$26847$88260bb3@free.teranews.com>
"L. Stern" <lstern@capgemini.fr> wrote in message
news:4486ea25$0$29812$626a54ce@news.free.fr...
> Hello
>
> I am installing Bugzilla 2.22 on a Windows 2000 and I have some problem to
> configure the mail notification.
> In fact no email are sent. My problem is on the use of perl script to send
> email.
>
> I have made some simple Perl script (either via Mailer or directly via
SMTP)
> and it seems OK for me (no error), but no email are sent.
>
> The scripts and logs are below
>
> Exemple 1
>
> Script1
>
> use Mail::Mailer;
>
> $mailer = new Mail::Mailer('smtp', Server => "10.135.1.3",Debug => 2);
> #$mailer = new Mail::Mailer 'testfile', Server => "10.135.1.3";
>
> $mailer->open({From => 'Laurent.Stern@darty.fr',
> To => 'lstern@capgemini.fr',
> Subject => "test 1234",})
> or die "Can't open: $!\n";
> print $mailer "Ceci est le texte";
> $mailer->close();
>
> Trace1
>
> Net::SMTP>>> Net::SMTP(2.29)
> Net::SMTP>>> Net::Cmd(2.26)
> Net::SMTP>>> Exporter(5.58)
> Net::SMTP>>> IO::Socket::INET(1.29)
> Net::SMTP>>> IO::Socket(1.29)
> Net::SMTP>>> IO::Handle(1.25)
> Net::SMTP=GLOB(0x1a4ed48)<<< 220 KIX001 ESMTP Service (Lotus Domino
Release
> 5.0.11) ready at Tue, 30 May 2006 14:25:55 +0200
> Net::SMTP=GLOB(0x1a4ed48)>>> EHLO localhost.localdomain
> Net::SMTP=GLOB(0x1a4ed48)<<< 250-KIX001 Hello localhost.localdomain
> ([136.0.1.0]), pleased to meet you
> Net::SMTP=GLOB(0x1a4ed48)<<< 250-HELP
> Net::SMTP=GLOB(0x1a4ed48)<<< 250-SIZE 51200000
> Net::SMTP=GLOB(0x1a4ed48)<<< 250 PIPELINING
> Net::SMTP=GLOB(0x1a4ed48)>>> MAIL
FROM:<postmaster@s0i600.intranet.darty.fr>
> Net::SMTP=GLOB(0x1a4ed48)<<< 250 postmaster@s0i600.intranet.darty.fr...
> Sender OK
> Net::SMTP=GLOB(0x1a4ed48)>>> RCPT TO:<lstern@capgemini.fr>
> Net::SMTP=GLOB(0x1a4ed48)<<< 250 lstern@capgemini.fr... Recipient OK
> Net::SMTP=GLOB(0x1a4ed48)>>> DATA
> Net::SMTP=GLOB(0x1a4ed48)<<< 354 Enter message, end with "." on a line by
> itself
> Net::SMTP=GLOB(0x1a4ed48)>>> Subject: test 1234
> Net::SMTP=GLOB(0x1a4ed48)>>> X-Mailer: Mail::Mailer[v1.67]
Net::SMTP[v2.29]
> Net::SMTP=GLOB(0x1a4ed48)>>> To: lstern@capgemini.fr
> Net::SMTP=GLOB(0x1a4ed48)>>> From: Laurent.Stern@darty.fr
> Net::SMTP=GLOB(0x1a4ed48)>>> Ceci est le texte
> Net::SMTP=GLOB(0x1a4ed48)>>> .
> Net::SMTP=GLOB(0x1a4ed48)<<< 250 Message accepted for delivery
> Net::SMTP=GLOB(0x1a4ed48)>>> QUIT
> Net::SMTP=GLOB(0x1a4ed48)<<< 221 KIX001 SMTP Service closing transmission
> channel
There is no problem with your Perl script as can be seen from the above
trace. The SMTP server accepts the email and what happens to it after that
is entirely up to the SMTP server and the whole delivery chain.
--
Posted via a free Usenet account from http://www.teranews.com
------------------------------
Date: Thu, 8 Jun 2006 08:01:46 +0200
From: "L. Stern" <lstern@capgemini.fr>
Subject: Re: Problem with SMPT mail
Message-Id: <4487bd4f$0$21208$626a54ce@news.free.fr>
"Brian McCauley" <nobull67@gmail.com> a écrit dans le message de news:
1149700317.346308.197780@u72g2000cwu.googlegroups.com...
>
> Brian McCauley wrote:
>
>> Are errors being sent to the return path address
>> <postmaster@s0i600.intranet.darty.fr>?
>
> Note: if you want to control the return-path (aka envelope-from)
> address used by Mail::Mailer::smtp you should set $ENV{MAILADDRESS} .
>
> This is less than clear in the documentation.
>
Thank you,
Setting the $ENV{MAILADDRESS} I received an email with my problem:
In fact the "@" was not properly analyzed (the From and To was modified) and
I just have to change my "name@foo.com" to "name\@foo.com" to make it works
Laurent
------------------------------
Date: Wed, 07 Jun 2006 10:41:57 +0200
From: Tomasz Kobus <Ra.spam-niet@t.k.pl>
Subject: Re: read from ttySx buffer 4096 problem
Message-Id: <n34d82d87up608m1dshl695av1e5vpvp9j@4ax.com>
On Tue, 6 Jun 2006 20:02:00 +0100, Ben Morrow
<benmorrow@tiscali.co.uk> wrote:
thank you very much, it's work, i do like these:
--------------------------------------
#!/usr/bin/perl
use IO::Handle;
open (PORT,"/dev/ttyS1");
open my $LOGI, '>>', 'file_data' or die "problem";
$LOGI->autoflush;
while (<PORT>)
{
print $LOGI "$_";
}
----------------------------------
------------------------------
Date: Wed, 7 Jun 2006 12:21:15 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: read from ttySx buffer 4096 problem
Message-Id: <bepil3-aqb.ln1@osiris.mauzo.dyndns.org>
Quoth Tomasz Kobus <Ra.spam-niet@t.k.pl>:
> On Tue, 6 Jun 2006 20:02:00 +0100, Ben Morrow
> <benmorrow@tiscali.co.uk> wrote:
What did I write? Please quote some context when you reply.
> thank you very much, it's work, i do like these:
You are not listening to everyting I told you. It was all important.
> --------------------------------------
> #!/usr/bin/perl
>
use strict;
use warnings;
> use IO::Handle;
> open (PORT,"/dev/ttyS1");
open my $PORT, '>', '/dev/ttyS1'
or die "can't open /dev/ttyS1: $!";
> open my $LOGI, '>>', 'file_data' or die "problem";
You need to include the filename in that message, so you know what you
couldn't open.
You need to include $! in that message, so you know why you couldn't
open it.
> $LOGI->autoflush;
> while (<PORT>)
> {
> print $LOGI "$_";
You don't need to quote that variable.
You have not set $\ so this program will do something different from
your old one (it's probably correct, though).
Ben
--
You poor take courage, you rich take care:
The Earth was made a common treasury for everyone to share
All things in common, all people one.
'We come in peace'---the order came to cut them down. [ben@morrow.me.uk]
------------------------------
Date: 7 Jun 2006 04:34:40 -0700
From: "Ronny" <ro.naldfi.scher@gmail.com>
Subject: Returning the first n elements of a list
Message-Id: <1149680080.674923.174170@i40g2000cwc.googlegroups.com>
Assume that I have a function f expecting a list of arguments, and an
expression
E returning a list. Of course one way to call this function is just:
f(E)
Example: f(`ls -l foo`);
Now suppose that I want to pass to the function only the first $n
elements of E.
A naive approach (assuming $n > 0) would be
f((E)[0..($n-1)])
but this raises error messages if E contains less than $n elements. One
way out
from this dilemma would be the introduction of an auxiliary array:
my @temp=(E);
f(@temp[0..($n > $#temp ? $#temp : $n-1)]);
Needless to say that this is a very ugly solution. Can someone suggest
a more
elegant way?
Ronald
------------------------------
Date: Wed, 7 Jun 2006 14:07:06 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Returning the first n elements of a list
Message-Id: <e66mlr.n8.1@news.isolution.nl>
Ronny schreef:
> Now suppose that I want to pass to the function only the first $n
> elements of E.
> [...]
> One way out
> from this dilemma would be the introduction of an auxiliary array:
>
> my @temp=(E);
> f(@temp[0..($n > $#temp ? $#temp : $n-1)]);
sub min { $_[0] < $_[1] ? $_[0] : $_[1] }
and use
f( @temp[ 0 .. min( $n-1, $#temp ) ] )
or
f( @temp[ 0 .. min( $n, 0+@temp ) -1 ] )
or pass the array by reference, and take cary of it it inside f().
f(\@temp, $n)
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: Wed, 07 Jun 2006 14:29:33 +0200
From: Mirco Wahab <peace.is.our.profession@gmx.de>
Subject: Re: Returning the first n elements of a list
Message-Id: <e66h20$ld5$1@mlucom4.urz.uni-halle.de>
Thus spoke Ronny (on 2006-06-07 13:34):
> Now suppose that I want to pass to the function
> only the first $n elements of E [but no undefs]
> f(@temp[0..($n > $#temp ? $#temp : $n-1)]);
f( grep{$_}(1..3)[0..6] );
Regards
Mirco
------------------------------
Date: Wed, 07 Jun 2006 14:50:51 +0200
From: Mirco Wahab <peace.is.our.profession@gmx.de>
Subject: Re: Returning the first n elements of a list
Message-Id: <e66i9u$lp4$1@mlucom4.urz.uni-halle.de>
Thus spoke Mirco Wahab (on 2006-06-07 14:29):
> Thus spoke Ronny (on 2006-06-07 13:34):
>> f(@temp[0..($n > $#temp ? $#temp : $n-1)]);
>
> f( grep{$_}(1..3)[0..6] );
must be:
f( grep{defined $_}(0,0,0)[0..5] );
because grep of course bails on 0 as on undef ...
Sorry
Mirco
------------------------------
Date: 7 Jun 2006 06:05:14 -0700
From: "Ronny" <ro.naldfi.scher@gmail.com>
Subject: Re: Returning the first n elements of a list
Message-Id: <1149685514.757306.220050@y43g2000cwc.googlegroups.com>
Mirco Wahab schrieb:
> Thus spoke Ronny (on 2006-06-07 13:34):
>
> > Now suppose that I want to pass to the function
> > only the first $n elements of E [but no undefs]
> > f(@temp[0..($n > $#temp ? $#temp : $n-1)]);
>
> f( grep{$_}(1..3)[0..6] );
This works, but could you enlighten me, how?
I observed that this works:
f( grep {$_} ( (1..3)[0..6]) )
but this does not:
f( ((1..3)[0..3]) )
So what peculiar blessing does grep do onto its right argument?
Ronald
------------------------------
Date: Wed, 07 Jun 2006 15:34:28 +0200
From: Mirco Wahab <peace.is.our.profession@gmx.de>
Subject: Re: Returning the first n elements of a list
Message-Id: <e66krp$mel$1@mlucom4.urz.uni-halle.de>
Thus spoke Ronny (on 2006-06-07 15:05):
> Mirco Wahab schrieb:
>> Thus spoke Ronny (on 2006-06-07 13:34):
>> > f(@temp[0..($n > $#temp ? $#temp : $n-1)]);
>> f( grep{$_}(1..3)[0..6] );
>
> This works, but could you enlighten me, how?
> I observed that this works:
> f( grep {$_} ( (1..3)[0..6]) )
(OTHER_LIST) <== grep <== ( L_I_S_T )
So grep constructs onother list from a
"source" list. It "filters" the L_I_S_T
elements (each gets probed as $_) and
puts only those to the OTHER_LIST for
which the {BLOCK} return true.
And 'undef' returns false when tested in
boolean context, like
if($_ == undef) return false;
0 (zero) also return false, this is why you
have to check explicitly for 'definedness'
f( grep {defined $_} @list[0..5] );
which means: only elements which are
defined (including 0) are passed to
the other list.
Regards
Mirco
------------------------------
Date: 7 Jun 2006 10:21:29 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: Returning the first n elements of a list
Message-Id: <1149700889.562597.205000@g10g2000cwb.googlegroups.com>
Ronny wrote:
> Assume that I have a function f expecting a list of arguments, and an
> expression
> E returning a list. Of course one way to call this function is just:
> f(E)
>
> Example: f(`ls -l foo`);
>
> Now suppose that I want to pass to the function only the first $n
> elements of E.
> A naive approach (assuming $n > 0) would be
>
> f((E)[0..($n-1)])
>
> but this raises error messages if E contains less than $n elements. One
> way out
> from this dilemma would be the introduction of an auxiliary array:
>
> my @temp=(E);
> f(@temp[0..($n > $#temp ? $#temp : $n-1)]);
>
> Needless to say that this is a very ugly solution. Can someone suggest
> a more elegant way?
Since the auxiliary array is expendable you can splice() it.
my @temp = E;
f( splice @temp, 0, 3 );
You can even avoid naming the auxiliary array
f( splice @{[ E ]}, 0, 3 );
------------------------------
Date: Wed, 07 Jun 2006 15:26:16 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Returning the first n elements of a list
Message-Id: <g69slmg938n.fsf@CN1374059D0130.kendall.corp.akamai.com>
On 7 Jun 2006, ro.naldfi.scher@gmail.com wrote:
> Assume that I have a function f expecting a list of arguments, and
> an expression E returning a list. Of course one way to call this
> function is just: f(E)
>
> Example: f(`ls -l foo`);
>
> Now suppose that I want to pass to the function only the first $n
> elements of E.
You should consider why you only need to pass the first few elements.
It complicates things, as you see. Why not just
my @results = `command`;
f(\@results); # passes an array reference
then in f(), use the contents of the array. Splice them if you have
to. The essential thing is that your function's call is simpler, and
the *function* will decide what it wants from the data. That way,
when you decide f() needs N+1 elements of the array, you don't have to
change something in the main program flow, only in the function
itself. IMO this helps avoid spaghetti code.
Of course, you may really want to pass only this much. Perhaps data
security is important and you don't want to pass too much. Splice,
map, and grep are helpful (as others showed). You could also do
something like this:
f(shift @results, shift @results, shift @results);
which clearly indicates, to a reader (which could be you in 5 years),
what you are doing.
Ted
------------------------------
Date: Wed, 7 Jun 2006 14:03:34 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Returning the first n elements of a list
Message-Id: <slrne8e8o6.st4.tadmc@magna.augustmail.com>
Mirco Wahab <peace.is.our.profession@gmx.de> wrote:
> Thus spoke Mirco Wahab (on 2006-06-07 14:29):
>
>> Thus spoke Ronny (on 2006-06-07 13:34):
>>> f(@temp[0..($n > $#temp ? $#temp : $n-1)]);
>>
>> f( grep{$_}(1..3)[0..6] );
>
> must be:
>
> f( grep{defined $_}(0,0,0)[0..5] );
>
> because grep of course bails on 0 as on undef ...
But the OP didn't say that undefs are forbidden in the middle
of the list so you are (potentially) throwing away _valid_
list elements...
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 07 Jun 2006 22:25:37 +0200
From: Mirco Wahab <wahab@chemie.uni-halle.de>
Subject: Re: Returning the first n elements of a list
Message-Id: <e67d02$t68$1@mlucom4.urz.uni-halle.de>
Thus spoke Tad McClellan (on 2006-06-07 21:03):
> Mirco Wahab <peace.is.our.profession@gmx.de> wrote:
>> Thus spoke Mirco Wahab (on 2006-06-07 14:29):
>>> Thus spoke Ronny (on 2006-06-07 13:34):
>>>> f(@temp[0..($n > $#temp ? $#temp : $n-1)]);
>>> f( grep{$_}(1..3)[0..6] );
>> f( grep{defined $_}(0,0,0)[0..5] );
>>
>> because grep of course bails on 0 as on undef ...
>
> But the OP didn't say that undefs are forbidden in the middle
> of the list so you are (potentially) throwing away _valid_
> list elements...
As I have come to understand this,
the OP got bored by errors/warnings
/due to/ undefs:
>>>> A naive approach (assuming $n > 0) would be
>>>>
>>>> f( (E)[0..($n-1)] )
>>>>
>>>> but this raises error messages if E
>>>> contains less than $n elements.
For this reason I suggested
simply to filter out undef.
It would be, of course, perfectly possible to
have 'wanted undefs' as regular array elements -
but from what the OP signalled, I ruled that out ... ;-)
Regards
Mirco
------------------------------
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 9264
***************************************