[24348] in Perl-Users-Digest
Perl-Users Digest, Issue: 6537 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri May 7 14:06:02 2004
Date: Fri, 7 May 2004 11:05:11 -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 Fri, 7 May 2004 Volume: 10 Number: 6537
Today's topics:
Function Split <georgekinley@hotmail.com>
Re: Function Split <noreply@gunnar.cc>
Re: Function Split <xxala_qumsiehxx@xxyahooxx.com>
Re: Function Split <nobull@mail.com>
Re: How to redirect STDOUT to a string? <remorse@partners.org>
Re: perl require <nobull@mail.com>
Perl script runs from command prompt but not from Task (kpowell10)
Re: Perl script runs from command prompt but not from T <remorse@partners.org>
Re: Perl script runs from command prompt but not from T <mark.clements@kcl.ac.uk>
Re: read from comma delimited file <xx087@freenet.carleton.ca>
Re: regex utility <richard@zync.co.uk>
Re: Remove elements from one array found in another <xx087@freenet.carleton.ca>
Re: Remove elements from one array found in another <nobull@mail.com>
Re: Remove elements from one array found in another <nobull@mail.com>
Re: Remove elements from one array found in another <chiefS@edu.edu>
Re: Remove elements from one array found in another <nobull@mail.com>
Re: Sort Hash o Hash accordint to two keys (Malik Yousef)
Re: Using $1, $2 ... but don't know in which order <glex_nospam@qwest.invalid>
Re: Using $1, $2 ... but don't know in which order (Walter Roberson)
Re: Using $1, $2 ... but don't know in which order (Walter Roberson)
Re: Using $1, $2 ... but don't know in which order <mark.clements@kcl.ac.uk>
Re: Using $1, $2 ... but don't know in which order <tore@aursand.no>
Re: Using $1, $2 ... but don't know in which order (Anno Siegel)
Re: Using $1, $2 ... but don't know in which order (Anno Siegel)
Re: Using $1, $2 ... but don't know in which order <tassilo.parseval@rwth-aachen.de>
Re: win32::ole and excel VBA macro conversion: SmallScr <remorse@partners.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 07 May 2004 16:25:08 GMT
From: "George Kinley" <georgekinley@hotmail.com>
Subject: Function Split
Message-Id: <ELOmc.15972$g4.308999@news2.nokia.com>
Hi,
Just wondering, when we use function "split" it returns a list ,
now is this possible to, directly access any index of a that list with out
initializing to a list variable
for example ,
$S="win.kit"
@L=split (/\./,$S)
we get @L=("wins","kit")
return @L[0]
what I want is some thing like
@{splits(/\./,$S)}[0] # is it possible :-o)
------------------------------
Date: Fri, 07 May 2004 18:34:09 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Function Split
Message-Id: <2g1senF3l95eU1@uni-berlin.de>
George Kinley wrote:
> Just wondering, when we use function "split" it returns a list ,
> now is this possible to, directly access any index of a that list
> with out initializing to a list variable
> for example ,
> $S="win.kit"
> @L=split (/\./,$S)
> we get @L=("wins","kit")
> return @L[0]
> what I want is some thing like
> @{splits(/\./,$S)}[0] # is it possible :-o)
You can do:
return ( split /\./, $S )[0];
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Fri, 07 May 2004 16:40:33 GMT
From: "Ala Qumsieh" <xxala_qumsiehxx@xxyahooxx.com>
Subject: Re: Function Split
Message-Id: <5_Omc.45875$Fk.2858@newssvr29.news.prodigy.com>
"George Kinley" <georgekinley@hotmail.com> wrote in message
news:ELOmc.15972$g4.308999@news2.nokia.com...
> Hi,
> Just wondering, when we use function "split" it returns a list ,
> now is this possible to, directly access any index of a that list with
out
> initializing to a list variable
> for example ,
> $S="win.kit"
> @L=split (/\./,$S)
> we get @L=("wins","kit")
Not really :)
> return @L[0]
You most probably should return $L[0]. Check out the faqs for the difference
between the two:
% perldoc -q '\$array'
Found in /home/gnu/perl-5.6.1/lib/5.6.1/pod/perlfaq4.pod
What is the difference between $array[1] and @array[1]?
> what I want is some thing like
> @{splits(/\./,$S)}[0] # is it possible :-o)
return (split /\./, $S)[0];
--Ala
------------------------------
Date: 07 May 2004 17:45:39 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Function Split
Message-Id: <u965b8i3t8.fsf@wcl-l.bham.ac.uk>
"George Kinley" <georgekinley@hotmail.com> writes:
> Subject: Function Split
You are not paritioning your problem properly.
> Just wondering, when we use function "split" it returns a list ,
Indeed split is just an example of a function that returns a list.
Technically all Perl functions (evaluated in list contest) return lists,
although some will always return single element list.
Your question is not about split, it's about lists.
> now is this possible to, directly access any index of a that list with out
> initializing to a list variable
> for example ,
> $S="win.kit"
> @L=split (/\./,$S)
> we get @L=("wins","kit")
> return @L[0]
> what I want is some thing like
> @{splits(/\./,$S)}[0] # is it possible :-o)
Close, but @{ whatever() }[0] is an array reference slice so
whatever() must evaluate to an array reference.
You can make a list into an array reference using [].
@{[split (/\./,$S)]}[0]
But this still initializes an array variable, albeit an anonymous
one, and you said you did't want that.
What you want is a list slice.
(split (/\./, $S, 2))[0]
Note the 2 in the call to split() to prevent it building a huge list
only to throw away all but the first element.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 07 May 2004 11:37:59 -0400
From: Richard Morse <remorse@partners.org>
Subject: Re: How to redirect STDOUT to a string?
Message-Id: <remorse-B336B8.11375907052004@plato.harvard.edu>
In article <c7g56k$37c$1@reader2.panix.com>,
J Krugman <jkrugman@yahbitoo.com> wrote:
> And one more: can someone point me to where in the Perl documentation
> lexical filehandles are discussed? I'm sure it's in there somewhere,
> but after a lot of page-flipping and poring over my Camel book's
> index, I still can't find anything. (My immediate interest is
> confirming my guess that "close" is called automatically on such
> handles when they go out of scope, but in general I want to get
> the full official lowdown on lexical handles.)
Well, in the 3rd edition of the Camel Book, on page 748, it says:
=begin quote
But C<open> is special in that if you supply it with an undefined
variable for the indirect filehandle, Perl will automatically define
that variable for you, that is, autovivifying it to contain a proper
filehandle reference. One advantage of this is that the filehandle will
be closed automatically when there are no further refences to it,
typically when the variable goes out of scope:
{
my $fh; # (uninitialized)
open($fh, ">logfile") # $fh is autovivified
or die "Can't create logfile: $!";
... # do stuff with $fh
} # $fh closed here
The C<my $fh> declaration can be readably incorporated into the C<open>:
open my $fh, ">logfile" or die ...
=end quote
=cut
Also, on the topic that you were originally asking about, while looking
through `perldoc -f open` for Perl 5.8.1 (Mac OS X 10.3.3), I noticed
the following:
=begin quote
File handles can be opened to "in memory" files held in Perl scalars
via:
open($fh, '>', \$variable) || ..
Though if you try to re-open "STDOUT" or "STDERR" as an "in memory"
file, you have to close it first:
close STDOUT;
open STDOUT, '>', \$variable or die "Can't open STDOUT: $!";
=end quote
=cut
HTH,
Ricky
--
Pukku
------------------------------
Date: 07 May 2004 18:06:54 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: perl require
Message-Id: <u9wu3ogo9d.fsf@wcl-l.bham.ac.uk>
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:
> my $VAR = 'Wetherbys';
> my $class = "Plugins::$VAR::Generate::GenerateXML";
> ( my $module = $class) =~ s{::}{/}g; # File::Spec would be better
> $module .= '.pm'; # File::Spec would be better
> require $module;
> $class->import('help');
>
> This mess may be best stuffed into a sub and taken out of sight.
There is UNIVERSAL::require but I'm not too happy with the fact that
it doesn't re-throw errors.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 7 May 2004 08:39:24 -0700
From: kpowell10@hotmail.com (kpowell10)
Subject: Perl script runs from command prompt but not from Task Scheduler in Win2000
Message-Id: <840ec4ee.0405070739.7137d768@posting.google.com>
Hi all,
I have a script that runs fine from the command prompt, but that fails
when I run it from Task Scheduler.
Apparently the line that is failing is an OLE module command:
$xlworkbook = $xl_app->Workbooks->Add;
Any ideas what might cause this behavior or how to pin down the cause?
I'm getting no errors messages of any sort in the Event Viewer.
Thanks,
Kyle
------------------------------
Date: Fri, 07 May 2004 12:10:10 -0400
From: Richard Morse <remorse@partners.org>
Subject: Re: Perl script runs from command prompt but not from Task Scheduler in Win2000
Message-Id: <remorse-D63F92.12101007052004@plato.harvard.edu>
In article <409bae44$1@news.kcl.ac.uk>,
Mark Clements <mark.clements@kcl.ac.uk> wrote:
> kpowell10 wrote:
>
> > I have a script that runs fine from the command prompt, but that fails
> > when I run it from Task Scheduler.
> >
> > Apparently the line that is failing is an OLE module command:
> > $xlworkbook = $xl_app->Workbooks->Add;
> >
> > Any ideas what might cause this behavior or how to pin down the cause?
> > I'm getting no errors messages of any sort in the Event Viewer.
>
> what happens if you wrap it in an eval {} and dump $@ somewhere?
Or put a line like the following at the start of the script:
open(STDERR, ">>", "c:/temp/odd_errors")
or die("can't change STDERR: $!"); # yes, I see the problem
open(STDOUT, ">>", "c:/temp/odd_output")
or die("can't change STDOUT: $!");
warn (('-' x 5) . ' ' . scalar(localtime) . "\n");
...
HTH,
Ricky
--
Pukku
------------------------------
Date: Fri, 07 May 2004 16:42:11 +0100
From: Mark Clements <mark.clements@kcl.ac.uk>
Subject: Re: Perl script runs from command prompt but not from Task Scheduler in Win2000
Message-Id: <409bae44$1@news.kcl.ac.uk>
kpowell10 wrote:
> I have a script that runs fine from the command prompt, but that fails
> when I run it from Task Scheduler.
>
> Apparently the line that is failing is an OLE module command:
> $xlworkbook = $xl_app->Workbooks->Add;
>
> Any ideas what might cause this behavior or how to pin down the cause?
> I'm getting no errors messages of any sort in the Event Viewer.
what happens if you wrap it in an eval {} and dump $@ somewhere?
Mark
------------------------------
Date: 7 May 2004 15:04:46 GMT
From: Glenn Jackman <xx087@freenet.carleton.ca>
Subject: Re: read from comma delimited file
Message-Id: <slrnc9n9cf.irt.xx087@smeagol.ncf.ca>
Jim Cochrane <jtc@shell.dimensional.com> wrote:
> Yes, but it appears to be harder with split, because you can't use (...)
> grouping without affecting the result of the split. I just learned about
> this while playing around with another problem last night.
Sure you can. Just use non-capturing parentheses (?:my regex)
--
Glenn Jackman
NCF Sysadmin
glennj@ncf.ca
------------------------------
Date: Fri, 07 May 2004 16:49:49 +0100
From: "Richard Gration" <richard@zync.co.uk>
Subject: Re: regex utility
Message-Id: <c7gb6m$aq1$1@news.freedom2surf.net>
In article <gv7n909u2cqc42mt8el5hg9gb7i8k0subg@4ax.com>, "Michele Dondi"
<bik.mido@tiscalinet.it> wrote:
<SNIP valid stylistic critique>
The reason that code snippet looks like it did is 'cos I wrote it in 2
minutes as a diversion from the actual coding I'm doing at the moment
which, surprise, surprise, contains several $#$array_ref sort of things
so it just carried over.
I happen to think that not using an array to hold the return from the
match expression was the most serious error. That did try to break
through my mind's limina while I was doing the copy and paste, but I was
in a hurry ...
R
------------------------------
Date: 7 May 2004 15:03:11 GMT
From: Glenn Jackman <xx087@freenet.carleton.ca>
Subject: Re: Remove elements from one array found in another
Message-Id: <slrnc9n99g.irt.xx087@smeagol.ncf.ca>
Bryan <bryan@akanta.com> wrote:
> Hi, I need a function that will remove any elements of the second array
> from the first. I found a function that does mostly what I want but its
> not behaving the way I want in one case.
[...]
sub removeArrayElements($$) {
my %tmp;
my @a = @{shift()};
my @b = @{shift()};
@tmp{@a} = (1) x @a;
delete @tmp{@b};
return keys %tmp;
}
--
Glenn Jackman
NCF Sysadmin
glennj@ncf.ca
------------------------------
Date: 07 May 2004 17:17:15 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Remove elements from one array found in another
Message-Id: <u9ekpwi54k.fsf@wcl-l.bham.ac.uk>
Glenn Jackman <xx087@freenet.carleton.ca> writes:
> Bryan <bryan@akanta.com> wrote:
> > Hi, I need a function that will remove any elements of the second array
> > from the first. I found a function that does mostly what I want but its
> > not behaving the way I want in one case.
>
> [...]
> sub removeArrayElements($$) {
What is the point of that prototype?
I can see why you could want a (\@\@) prototype to save the caller
putting \ in the call but I really can't see any use for ($$)
> my %tmp;
> my @a = @{shift()};
> my @b = @{shift()};
Why are you making copies of the arrays?
> @tmp{@a} = (1) x @a;
> delete @tmp{@b};
> return keys %tmp;
> }
This is not order preseving. The OP said they wanted to remove
elements from an array. Since arrays are ordered then the
null-hypothesis should be that the OP wanted to preserve order.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 07 May 2004 17:30:06 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Remove elements from one array found in another
Message-Id: <u9ad0ki4j5.fsf@wcl-l.bham.ac.uk>
"Chief S." <chiefS@edu.edu> writes:
> Bryan wrote:
> > Hi, I need a function that will remove any elements of the second
> > array from the first. I found a function that does mostly what I
> > want but its not behaving the way I want in one case.
> > For example, if I have two arrays;
> > @a1 = (1, 2, 3, 4, 5);
> > @a2 = (2, 4);
>
> Won't this do the trick?
>
> $seen{$_} = 1 for @a2;
> @a1 = grep ! $seen{$_}, @a1;
That does not remove elements from the array @a1.
That replaces the content of @a1 with a list which is the old content
of @a1 less those elements that also appeared in @a2.
Usually this distinction is not relevant but occasionally it may be.
To answer the OP's question more pedantically
my %seen;
undef $seen{@a2}; = 1 for @a2;
for my $i ( reverse 0 .. $#a1 ) {
if ( exists $seen{$a1[$i]} ) {
splice @a1,$i,1;
}
}
Deja vu anyone?
http://groups.google.com/groups?threadm=u9fznhjxwe.fsf%40wcl-l.bham.ac.uk
http://groups.google.com/groups?threadm=u9r8axwjvx.fsf%40wcl-l.bham.ac.uk
http://groups.google.com/groups?threadm=u9ela8gixl.fsf%40wcl-l.bham.ac.uk
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 07 May 2004 11:47:12 -0500
From: "Chief S." <chiefS@edu.edu>
Subject: Re: Remove elements from one array found in another
Message-Id: <c7geig$feq$1@lenny.tc.umn.edu>
> "Chief S." <chiefS@edu.edu> writes:
>>$seen{$_} = 1 for @a2;
>>@a1 = grep ! $seen{$_}, @a1;
Brian McCauley wrote:
> That does not remove elements from the array @a1.
>
> That replaces the content of @a1 with a list which is the old content
> of @a1 less those elements that also appeared in @a2.
>
> Usually this distinction is not relevant but occasionally it may be.
Is the relevance of the distinction related to the outcome -- that is,
sometimes the resulting @a1 will be incorrect using the grep() method?
Or is the relevance of the distinction related to process -- that is,
you need to actually remove the items from the list, rather than
building a new filtered list, because your larger programming logic is
structured a certain way?
Thanks for the clarification.
--
Chief S.
------------------------------
Date: 07 May 2004 17:58:32 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Remove elements from one array found in another
Message-Id: <u91xlwi37r.fsf@wcl-l.bham.ac.uk>
"Chief S." <chiefS@edu.edu> writes:
> > "Chief S." <chiefS@edu.edu> writes:
> >>$seen{$_} = 1 for @a2;
> >>@a1 = grep ! $seen{$_}, @a1;
>
>
> Brian McCauley wrote:
> > That does not remove elements from the array @a1.
> > That replaces the content of @a1 with a list which is the old content
> > of @a1 less those elements that also appeared in @a2.
> > Usually this distinction is not relevant but occasionally it may be.
>
> Is the relevance of the distinction related to the outcome -- that is,
> sometimes the resulting @a1 will be incorrect using the grep() method?
>
> Or is the relevance of the distinction related to process -- that is,
> you need to actually remove the items from the list, rather than
> building a new filtered list, because your larger programming logic is
> structured a certain way?
Er... I don't get the distinction you are drawing :-)
Basically it depends on if there are any references to elements of @a1
floating around.
my @a1 = qw/ cat dog rabbit /;
my @a2 = qw/ dog /;
my $r = \$a1[2]; # References 'rabbit'
my %seen;
$seen{$_} = 1 for @a2;
@a1 = grep ! $seen{$_}, @a1;
$$r = 'hare'; # Change 'rabbit' to 'hare'
print "@a1\n"; # cat rabbit
As I said above it's relatively rare that this is an issue.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 7 May 2004 10:29:10 -0700
From: yousef@pcbi.upenn.edu (Malik Yousef)
Subject: Re: Sort Hash o Hash accordint to two keys
Message-Id: <a70ad489.0405070929.22daef90@posting.google.com>
Here the code i'm using now:
@pos = sort {
$allresults{$a}{HDR} cmp $allresults{$b}{HDR}
|| $allresults{$a}{WinStart} <=>
$allresults{$b}{WinStart}
# $allresults{$a}{WinStart} <=>
$allresults{$b}{WinStart}
} keys %allresults;
But the results that i'm getting are partioally sorted according to
the used commands.
Uri Guttman <uri@stemsystems.com> wrote in message news:<x7d65h35x0.fsf@mail.sysarch.com>...
> first, don't top post. read the group guidelines which are posted
> regularly.
>
> >>>>> "MY" == Malik Yousef <yousef@pcbi.upenn.edu> writes:
>
> MY> Let me make it simple
> MY> I have the hash keys with two information, the name and the window
> MY> position separated by <->, for example:
>
> what is a window position? this is already more complex than simple.
>
> MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->1492
> MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->1531
> MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->1795
> MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->23
> MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->2918
> MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->2921
> MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->2925
> MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->3304
> MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->3305
>
> MY> I want to sort this keys according to the name and the window
> MY> position, so
> MY> *<->23 should be as the first element, so how could one sort according
> MY> to the first part ($1)<->($2) and the second part at the same time.
>
> you don't have any hash i can see there. you have a list of
> values. again, i refer you to the FAQ which answers this. read perldoc
> -q sort and write some code and come back if you need more help. post
> just the short code sample you wrote.
>
> MY> Please send your reply also to yousef@pcbi.upenn.edu
>
> you post here, you read here.
>
> uri
------------------------------
Date: Fri, 07 May 2004 10:19:30 -0500
From: "J. Gleixner" <glex_nospam@qwest.invalid>
Subject: Re: Using $1, $2 ... but don't know in which order
Message-Id: <6ONmc.1052$Tj.17053@news.uswest.net>
Tore Aursand wrote:
> Hi!
>
> I have a number of old Perl scripts doing fairly the same job; They're
> connecting to a (local) web server and retrieves something from it (plain
> text, that is).
>
> As I said, each script is doing the same job; Parsing the text, and
> writing some "meaningful" data to a MySQL database.
>
> Everything works just great, but I want to put everything these scripts do
> into one script, as most of what they do is identical. I have created a
> list of hashes which describe each resource I try to parse;
>
> my %sources = (
Ahhhhh.. might want to cut & paste next time...
> {
> 'title' => 'Server #1',
> 'href' => 'http://.../1/',
> 'regexp' => '...',
> },
> {
> 'title' => 'Server #2',
> 'href' => 'http://.../1/',
> 'regexp' => '...',
> },
> # etc...
> );
>
> Iterating through these resources;
>
> foreach ( @sources ) {
> my $text = get( $_->{'href'} ); # LWP::Simple
> if ( defined $text && length $text ) {
> while ( $text =~ m,$_->{regexp},sig ) {
> my $foo = $1;
> my $bar = $2;
> # etc...
> }
> }
> }
>
> This works as expected for the majority of the files I download, but for
> some I need to - hmm - match in a different order. Example: For most of
> the sites it is suitable to set $foo = $1, but for some $foo should be $2
> instead (or $3, whatever).
>
> How should I deal with this in a sexy way? :)
First, you blow in its ear... You haven't given us the criteria for
when $foo=$2. If it's based on the URL, then add an if:
my $foo;
if ($_->{href} =~ /criteria/) { $foo = $2 }
Whatever the reason, you have to have something make the decision.. if,
switch, m//, whatever.
------------------------------
Date: 7 May 2004 15:21:21 GMT
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Subject: Re: Using $1, $2 ... but don't know in which order
Message-Id: <c7g9hh$itc$1@canopus.cc.umanitoba.ca>
In article <pan.2004.05.07.14.35.26.101754@aursand.no>,
Tore Aursand <tore@aursand.no> wrote:
:Everything works just great, but I want to put everything these scripts do
:into one script,
: {
: 'title' => 'Server #1',
: 'href' => 'http://.../1/',
: 'regexp' => '...',
It might make sense to use 'regexp' => qr/.../
so as to avoid recompiling the regexp on each step
:Iterating through these resources;
: while ( $text =~ m,$_->{regexp},sig ) {
: my $foo = $1;
: my $bar = $2;
:This works as expected for the majority of the files I download, but for
:some I need to - hmm - match in a different order.
:How should I deal with this in a sexy way? :)
Instead of a regexp in the structure, put in an anonymous sub
that does the match and returns the elements in a consistant order.
while ( my ($foo, $bar) = $_->{extractsub} ) {
# etc
}
--
Feep if you love VT-52's.
------------------------------
Date: 7 May 2004 15:24:07 GMT
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Subject: Re: Using $1, $2 ... but don't know in which order
Message-Id: <c7g9mn$j7j$1@canopus.cc.umanitoba.ca>
In article <c7g9hh$itc$1@canopus.cc.umanitoba.ca>,
Walter Roberson <roberson@ibd.nrc-cnrc.gc.ca> wrote:
: while ( my ($foo, $bar) = $_->{extractsub} ) {
: # etc
: }
Opps, typo. I meant
my $sref = $_->{extractsub};
while ( my ($foo, $bar) = &$sref($text) ) {
# etc
}
--
Disobey all self-referential sentences!
------------------------------
Date: Fri, 07 May 2004 16:26:03 +0100
From: Mark Clements <mark.clements@kcl.ac.uk>
Subject: Re: Using $1, $2 ... but don't know in which order
Message-Id: <409baa7c$1@news.kcl.ac.uk>
Tore Aursand wrote:
> Hi!
> Iterating through these resources;
>
> foreach ( @sources ) {
> my $text = get( $_->{'href'} ); # LWP::Simple
> if ( defined $text && length $text ) {
> while ( $text =~ m,$_->{regexp},sig ) {
> my $foo = $1;
> my $bar = $2;
> # etc...
> }
> }
> }
>
> This works as expected for the majority of the files I download, but for
> some I need to - hmm - match in a different order. Example: For most of
> the sites it is suitable to set $foo = $1, but for some $foo should be $2
> instead (or $3, whatever).
>
> How should I deal with this in a sexy way? :)
from man perlop:
If the "/g" option is not used, "m//" in list
context returns a list consisting of the
subexpressions matched by the parentheses in the
pattern, i.e., ($1, $2, $3...).
so how about defining the match positions in the config data:
my @sources = (
{
'title' => 'Server #1',
'href' => 'http://.../1/',
'regexp' => '...',
matchPositions => {
foo => 1,
bar => 2
}
},
{
'title' => 'Server #2',
'href' => 'http://.../2/',
'regexp' => '...',
matchPositions => {
foo => 2,
bar => 4
}
},
);
foreach (@sources) {
my $text = get( $_->{'href'} ); # LWP::Simple
my $matchPositions = $_->{positions};
if ( defined $text && length $text ) {
while ( my @matches = $text =~ m,$_->{regexp},sig ) {
my $foo = $matches[ $matchPositions->{foo} ];
my $bar = $matches[ $matchPositions->{bar} ];
# etc...
}
}
}
regards,
Mark
------------------------------
Date: Fri, 07 May 2004 17:37:10 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: Using $1, $2 ... but don't know in which order
Message-Id: <pan.2004.05.07.15.37.02.222473@aursand.no>
On Fri, 07 May 2004 16:26:03 +0100, Mark Clements wrote:
> from man perlop:
>
> If the "/g" option is not used, "m//" in list
> context returns a list consisting of the
> subexpressions matched by the parentheses in the
> pattern, i.e., ($1, $2, $3...).
Doh! I read this one, but I never saw that "returns a list". Of course,
that's the solution; "Telling" each source where in that list the data is.
Thanks!
--
Tore Aursand <tore@aursand.no>
"First, God created idiots. That was just for practice. Then He created
school boards." (Mark Twain)
------------------------------
Date: 7 May 2004 15:59:31 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Using $1, $2 ... but don't know in which order
Message-Id: <c7gbp3$bla$1@mamenchi.zrz.TU-Berlin.DE>
Tore Aursand <tore@aursand.no> wrote in comp.lang.perl.misc:
> Hi!
>
> I have a number of old Perl scripts doing fairly the same job; They're
> connecting to a (local) web server and retrieves something from it (plain
> text, that is).
>
> As I said, each script is doing the same job; Parsing the text, and
> writing some "meaningful" data to a MySQL database.
>
> Everything works just great, but I want to put everything these scripts do
> into one script, as most of what they do is identical. I have created a
> list of hashes which describe each resource I try to parse;
>
> my %sources = (
> {
> 'title' => 'Server #1',
> 'href' => 'http://.../1/',
> 'regexp' => '...',
> },
> {
> 'title' => 'Server #2',
> 'href' => 'http://.../1/',
> 'regexp' => '...',
> },
> # etc...
> );
>
> Iterating through these resources;
>
> foreach ( @sources ) {
> my $text = get( $_->{'href'} ); # LWP::Simple
> if ( defined $text && length $text ) {
^^
Precedence problem here.
> while ( $text =~ m,$_->{regexp},sig ) {
> my $foo = $1;
> my $bar = $2;
> # etc...
> }
> }
> }
>
> This works as expected for the majority of the files I download, but for
> some I need to - hmm - match in a different order. Example: For most of
> the sites it is suitable to set $foo = $1, but for some $foo should be $2
> instead (or $3, whatever).
>
> How should I deal with this in a sexy way? :)
Include a permutation vector in the resource description that says what goes
where. Then use an array slice to assign the matches to your variable,
re-ordering as necessary. The pseudo-code below shows a modified while-loop
that deals with this situation. If no permutation is given, the matches
are assigned in natural order:
foreach ( @sources ) {
my $text = get( $_->{'href'} ); # LWP::Simple
if ( defined $text and length $text ) {
while ( my @matches = $text =~ m,$_->{regexp},sig ) {
my @perm = @{ $_->{ perm} || [ 0 .. $#matches]};
my( $foo, $bar, ...) = @matches[ @perm];
}
}
}
Anno
------------------------------
Date: 7 May 2004 16:19:32 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Using $1, $2 ... but don't know in which order
Message-Id: <c7gcuk$cf3$1@mamenchi.zrz.TU-Berlin.DE>
Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote in comp.lang.perl.misc:
[following up to myself]
> while ( my @matches = $text =~ m,$_->{regexp},sig ) {
Ugh. It's not a good idea to put the match in list context here, that
would loop forever. That detracts from the sexiness.
> my @perm = @{ $_->{ perm} || [ 0 .. $#matches]};
> my( $foo, $bar, ...) = @matches[ @perm];
> }
Since the number of variables $foo, $bar, ... is known, a fix could be
while ( $text =~ m,$_->{regexp},sig ) {
my @matches = ( $1, $2, ...);
# (otherwise unchanged)
That detracts a bit from the sexiness too, but not as badly as not working.
You may think of a better fix in the context of your actual code, or
adapt one of the other suggestions.
Anno
------------------------------
Date: Fri, 7 May 2004 19:18:19 +0200
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: Using $1, $2 ... but don't know in which order
Message-Id: <2g1umtF3m600U1@uni-berlin.de>
Also sprach Anno Siegel:
> Tore Aursand <tore@aursand.no> wrote in comp.lang.perl.misc:
>> foreach ( @sources ) {
>> my $text = get( $_->{'href'} ); # LWP::Simple
>> if ( defined $text && length $text ) {
> ^^
> Precedence problem here.
No, that's alright. Terms have the highest precedence in Perl and thus
'&&' and 'and' are interchangeable in this case.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: Fri, 07 May 2004 12:00:09 -0400
From: Richard Morse <remorse@partners.org>
Subject: Re: win32::ole and excel VBA macro conversion: SmallScroll
Message-Id: <remorse-FC6C1E.12000907052004@plato.harvard.edu>
In article <gOLmc.37443$kc2.555611@nnrp1.uunet.ca>,
"Domenico Discepola" <domenico_discepola@quadrachemicals.com> wrote:
> "How do I convert a VBA macro to Perl?
> If you record a macro in Microsoft Office, this can often be translated
> directly into Perl. In Visual Basic for Applications (VBA) the syntax is
> like this:
>
> object.method(argument).property = value
>
> In Perl this becomes
>
> object->method(argument)->{property} = value;
> "
> So, as it was suggested, I used Excel's macro recording feature to record
> the following VBA code:
> ActiveWindow.SmallScroll ToRight:=-3
>
> So, using the documentation mentioned above, I translated this into:
>
> $excel->ActiveWindow->SmallScroll->{ToRight} = -3;
>
> which didn't work. This seemingly simple example causes newbie programmers
> much grief. I saw no mention of adding parenthesis (as you provided in your
> solution): $excel->ActiveWindow->SmallScroll({ToRight=>-3});
So, look closely at the Activestate example:
object.method(argument).property = value
^
See that '.' before the property?
Now, look at the VBA created by the macro recording feature:
ActiveWindow.SmallScroll ToRight:=-3
Note the absence of the '.'? VB does not require parenthesis on all
function calls (neither does Perl, for that matter). So what you have
here is a call to ActiveWindow.SmallScroll. It is being passed what I
assume is a named parameter called 'ToRight', which has a value of -3.
Hence:
$excel->ActiveWindow->SmallScroll( { ToRight => -3 } );
which is a function call to SmallScroll, passing in a named parameter
called 'ToRight'.
HTH,
Ricky
(disclaimer: I don't know VBA -- I can read it a little, and can fix the
occasional broken code)
--
Pukku
------------------------------
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 6537
***************************************