[24483] in Perl-Users-Digest
Perl-Users Digest, Issue: 6665 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jun 8 14:05:48 2004
Date: Tue, 8 Jun 2004 11:05:09 -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 Tue, 8 Jun 2004 Volume: 10 Number: 6665
Today's topics:
Re: Converting a string to multiple search patterns <pinyaj@rpi.edu>
Re: Converting a string to multiple search patterns <tore@aursand.no>
Re: Converting a string to multiple search patterns <tore@aursand.no>
Re: Converting a string to multiple search patterns (Anno Siegel)
Re: Converting a string to multiple search patterns <bmb@ginger.libs.uga.edu>
Re: Converting a string to multiple search patterns <pinyaj@rpi.edu>
Re: Converting a string to multiple search patterns <tore@aursand.no>
Re: Converting a string to multiple search patterns <tore@aursand.no>
Re: Converting a string to multiple search patterns <peter@semantico.com>
Re: Converting a string to multiple search patterns <tore@aursand.no>
Re: Cute bit of Perl to Assign $1,$2 to named variables (Sara)
Re: Cute bit of Perl to Assign $1,$2 to named variables (Christopher Hamel)
Re: Cute bit of Perl to Assign $1,$2 to named variables <spamtrap@dot-app.org>
Re: Do you ever use awk? <Juha.Laiho@iki.fi>
Re: killing my children (Oliver)
Re: killing my children ctcgag@hotmail.com
Magic var for Current "Index" in array within loop? <lawrence.tierney@bipsolutions.com>
Re: Magic var for Current "Index" in array within loop? <tore@aursand.no>
Re: Magic var for Current "Index" in array within loop? <nobull@mail.com>
Meet the flocker <someone@somewhere.com>
Re: Meet the flocker ctcgag@hotmail.com
Modifying the input (Tan Rezaei)
Re: Modifying the input <tore@aursand.no>
Re: Modifying the input <dwall@fastmail.fm>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 8 Jun 2004 09:31:39 -0400
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
To: Tore Aursand <tore@aursand.no>
Subject: Re: Converting a string to multiple search patterns
Message-Id: <Pine.SGI.3.96.1040608092157.23702A-100000@vcmr-64.server.rpi.edu>
[posted & mailed]
On Tue, 8 Jun 2004, Tore Aursand wrote:
>I'm stumped on this one: I have an application where I need to refine the
>search mechanism. The concept is quite simple: Get a string, convert it
>to separate words, count (and "score") each word for each document, and
>then display the result based on the score;
>
> my $query = 'A B C D';
> my @words = split( /\s+/, $query );
> foreach ( @documents ) {
> # ...
> }
>
>I need to refine it, as said. I want a higher score for word sequences,
>and in a particular order. For the example above ('A B C D'), I want to
>match in this order:
>
> 1. A B C D
> 2. A B C
> 3. B C D
> 4. A B
> 5. C D
> 6. A C
> 7. B D
> 9. A D
> 9. A
> 10. B
> 11. C
> 12. D
As Anno said, you're missing three (non-empty) strings:
A B D
A C D
B C
Now, does this mean you want to attempt matching them in that order?
Like, you want your regex to do:
/ABCD|ABC|BCD|ABD|ACD|AB|BC|CD|AC|AD|BD|A|B|C|D/
Is that what you want your regex to do?
>Anyone know of a module which can accomplis this? I really haven't tried
>with anything yet, 'cause I have no clue on how to do it. The closest
>thing I've been, has been with the Algorithm::Permute module. It doesn't
>give me what I want "out of the box", though...
I think you can accomplish it by way of embedded code in your regex, but
I'd need a little more information about the aim of the regex before I
could write one.
--
Jeff Pinyan RPI Acacia Brother #734 RPI Acacia Corp Secretary
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
------------------------------
Date: Tue, 08 Jun 2004 16:05:29 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: Converting a string to multiple search patterns
Message-Id: <pan.2004.06.08.14.02.26.721759@aursand.no>
On Tue, 08 Jun 2004 11:53:53 +0000, Anno Siegel wrote:
>> 1. A B C D
>> 2. A B C
>> 3. B C D
>> 4. A B
>> 5. C D
>> 6. A C
>> 7. B D
>> 9. A D
>> 9. A
>> 10. B
>> 11. C
>> 12. D
> I'm missing "A B D", "A C D", and " B C " from the collection.
Doh! You're right;
1. A B C D
2. A B C
3. B C D
4. A B D <--
5. A C D <--
6. A B
7. B C <--
8. C D
9. A C
10. B D
11. A D
12. A
13. B
14. C
15. D
> I'm not sure what you are asking. Is it the generation of all selections
> of 1 .. 4 objects from a set of 4?
Yes. I have a string containing 'A B C D'. I split those to an array,
and I want to generate a new list (as above), ie. and AoA;
$VAR1 = [
[ A, B, C, D ],
[ A, B, C ],
[ B, C, D ],
.
.
.
]
Once I have this structure, it's quite easy to do the scoring. But I'm
really stuck on how to generate this structure... :(
> I'm sure there is a module on CPAN to generate them, but ad-hoc solutions
> aren't too hard either.
I'm almost sure, too, but I haven't found anything. I've looked at
List::*, Set::*, Array::* etc.
--
Tore Aursand <tore@aursand.no>
"The science of today is the technology of tomorrow." (Edward Teller)
------------------------------
Date: Tue, 08 Jun 2004 16:05:30 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: Converting a string to multiple search patterns
Message-Id: <pan.2004.06.08.14.03.47.554762@aursand.no>
On Tue, 08 Jun 2004 09:31:39 -0400, Jeff 'japhy' Pinyan wrote:
> [...]
> Now, does this mean you want to attempt matching them in that order?
Yes, As mentioned, the matching isn't the problem. It's generating the
structure/pattern which is the problem. See my reply to Anno for more
information.
--
Tore Aursand <tore@aursand.no>
"First get your facts; then you can distort them at your leisure."
(Mark Twain)
------------------------------
Date: 8 Jun 2004 14:52:41 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Converting a string to multiple search patterns
Message-Id: <ca4jrp$rjo$1@mamenchi.zrz.TU-Berlin.DE>
Tore Aursand <tore@aursand.no> wrote in comp.lang.perl.misc:
> On Tue, 08 Jun 2004 11:53:53 +0000, Anno Siegel wrote:
> >> 1. A B C D
> >> 2. A B C
> >> 3. B C D
> >> 4. A B
> >> 5. C D
> >> 6. A C
> >> 7. B D
> >> 9. A D
> >> 9. A
> >> 10. B
> >> 11. C
> >> 12. D
>
> > I'm missing "A B D", "A C D", and " B C " from the collection.
>
> Doh! You're right;
>
> 1. A B C D
> 2. A B C
> 3. B C D
> 4. A B D <--
> 5. A C D <--
> 6. A B
> 7. B C <--
> 8. C D
> 9. A C
> 10. B D
> 11. A D
> 12. A
> 13. B
> 14. C
> 15. D
>
> > I'm not sure what you are asking. Is it the generation of all selections
> > of 1 .. 4 objects from a set of 4?
>
> Yes. I have a string containing 'A B C D'. I split those to an array,
> and I want to generate a new list (as above), ie. and AoA;
>
> $VAR1 = [
> [ A, B, C, D ],
> [ A, B, C ],
> [ B, C, D ],
> .
> .
> .
> ]
>
> Once I have this structure, it's quite easy to do the scoring. But I'm
> really stuck on how to generate this structure... :(
I found the other part harder.
sub selections {
my @sel = [];
for my $elem ( @_ ) {
unshift @sel, map [ $elem, @$_], @sel;
}
@sel;
}
Anno
------------------------------
Date: Tue, 8 Jun 2004 11:02:29 -0400
From: Brad Baxter <bmb@ginger.libs.uga.edu>
Subject: Re: Converting a string to multiple search patterns
Message-Id: <Pine.A41.4.58.0406081052460.10988@ginger.libs.uga.edu>
On Tue, 8 Jun 2004, Tore Aursand wrote:
> Doh! You're right;
>
> 1. A B C D
> 2. A B C
> 3. B C D
> 4. A B D <--
> 5. A C D <--
> 6. A B
> 7. B C <--
> 8. C D
> 9. A C
> 10. B D
> 11. A D
> 12. A
> 13. B
> 14. C
> 15. D
>
> > I'm not sure what you are asking. Is it the generation of all selections
> > of 1 .. 4 objects from a set of 4?
>
> Yes. I have a string containing 'A B C D'. I split those to an array,
> and I want to generate a new list (as above), ie. and AoA;
The order you list isn't the order I'd expect from a simple generation of
all selections, so tell me if my interpretation is correct:
o More terms = higher score
o More adjacent terms = higher score
o Leftward terms = higher score than those to the right
Right?
Regards,
Brad
------------------------------
Date: Tue, 8 Jun 2004 11:13:39 -0400
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
To: Tore Aursand <tore@aursand.no>
Subject: Re: Converting a string to multiple search patterns
Message-Id: <Pine.SGI.3.96.1040608095640.23843A-100000@vcmr-64.server.rpi.edu>
[posted & mailed]
On Tue, 8 Jun 2004, Jeff 'japhy' Pinyan wrote:
>[posted & mailed]
>
>Now, does this mean you want to attempt matching them in that order?
>Like, you want your regex to do:
>
> /ABCD|ABC|BCD|ABD|ACD|AB|BC|CD|AC|AD|BD|A|B|C|D/
>
>Is that what you want your regex to do?
I *think* this is what you want your regex to do:
my $rx = qr{
(?{ local ($s, $f) = (0, 1) })
^ \s*
(?: A (?{ $s += $f <<= 1 }) | (?{ $f = 1 }) ) \s*
(?: B (?{ $s += $f <<= 1 }) | (?{ $f = 1 }) ) \s*
(?: C (?{ $s += $f <<= 1 }) | (?{ $f = 1 }) ) \s*
(?: D (?{ $s += $f <<= 1 }) | (?{ $f = 1 }) ) \s*
$
(?{ $s })
}x;
You don't need to worry about the set logic now -- the regex engine will
take care of that.
That regex will match "ABCD", "ABC", "BCD" (and " BCD"), etc.
Specifically, it will match the 15 non-empty strings listed, with optional
whitespace throughout. Modification of HOW it matches is simple. What's
important to see is the scoring algorithm.
Score ($s) starts out at 0, and the factor ($f) starts out at 1. Every
time we hit a consecutive keyword, we left-shift the factor by 1 (that is,
we multiply it by 2), and add it to the score. Every time the next
keyword is missing, we reset the factor to 1. This means that the string
"ABCD" has the maximum score of 30 (2 + 4 + 8 + 16), and single-character
strings like "B" have the minimum non-zero score of 2.
To *use* this, we would do something like:
# this could be made more efficient via a Guttman-Rosler
# Transform, but that's not the point here
@sorted_by_score =
map { $_->[1] }
sort { $b->[0] <=> $a->[0] }
map { /$rx/ ? [ $^R, $_ ] : () }
@data;
The $^R variable contains the return value of the most recent (?{ ... })
in a regex -- for us, this is the one that merely holds the value of $s.
We then sort the data by its score, and extract the data.
If this is totally off-base, I apologize. On the other hand, if this is
exactly what you're looking for, then the process of constructing the
regex is simple:
use re 'eval';
my @kw = qw( A B C D );
my $rx = qr{
(?{ local ($s, $f) = (0, 1) })
^ \s*
@{[ map "(?:\Q$_\E(?{ \$s += \$f <<= 1 })|(?{ \$f = 1 }) )\\s*", @kw ]}
$
(?{ $s })
}x;
That does it.
--
Jeff Pinyan RPI Acacia Brother #734 RPI Acacia Corp Secretary
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
------------------------------
Date: Tue, 08 Jun 2004 17:22:18 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: Converting a string to multiple search patterns
Message-Id: <pan.2004.06.08.15.22.17.414680@aursand.no>
On Tue, 08 Jun 2004 11:02:29 -0400, Brad Baxter wrote:
> [...]
> o More terms = higher score
> o More adjacent terms = higher score
> o Leftward terms = higher score than those to the right
That's correct. Am I missing something? Is my list wrong? Please
correct me or make any suggestions.
--
Tore Aursand <tore@aursand.no>
"What we anticipate seldom occurs. What we least expected generally
happens." (Benjamin Disraeli)
------------------------------
Date: Tue, 08 Jun 2004 17:39:37 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: Converting a string to multiple search patterns
Message-Id: <pan.2004.06.08.15.37.03.977036@aursand.no>
On Tue, 08 Jun 2004 14:52:41 +0000, Anno Siegel wrote:
> sub selections {
> my @sel = [];
> for my $elem ( @_ ) {
> unshift @sel, map [ $elem, @$_], @sel;
> }
> @sel;
> }
This one almost does the job, but it doesn't output what I want; The
elements are mostly reversed, so reverse()'ing @_ makes it a bit better.
Still, 'A B D' comes before 'B C D'. Take a look at my previous reply to
you:
1. A B C D
2. A B C
3. B C D
4. A B D
5. A C D
6. A B
7. B C
8. C D
9. A C
10. B D
11. A D
12. A
13. B
14. C
15. D
Try this simple script using your subroutine (rewritten, 'cause
"sometimes" Pan won't let me paste thing I mark in xterm):
my @array = selections( qw(A B C D) );
for ( 0..$#array ) {
print "$_. " . join(' ', @{$array[$_]}) . "\n";
}
But you're close, Anno! :)
--
Tore Aursand <tore@aursand.no>
"What we do is never understood, but only praised and blamed."
(Friedrich Nietzsche)
------------------------------
Date: Tue, 08 Jun 2004 17:30:52 +0100
From: Peter Hickman <peter@semantico.com>
Subject: Re: Converting a string to multiple search patterns
Message-Id: <40c5e9be$0$7727$afc38c87@news.easynet.co.uk>
Tore Aursand wrote:
> On Tue, 08 Jun 2004 09:31:39 -0400, Jeff 'japhy' Pinyan wrote:
>
>>[...]
>>Now, does this mean you want to attempt matching them in that order?
>
>
> Yes, As mentioned, the matching isn't the problem. It's generating the
> structure/pattern which is the problem. See my reply to Anno for more
> information.
>
>
Is this what you are after?
[peter@wasabi peter]$ cat xx
my @letters = qw/A B C D/;
foreach my $x ((0..15)) {
my %z;
@z{@letters} = split('', sprintf("%04b", $x));
printf "%2d: ", $x;
foreach my $letter (@letters) {
printf "%s ", (($z{$letter} == 1) ? $letter : ' ');
}
print "\n";
}
[peter@wasabi peter]$ perl xx
0:
1: D
2: C
3: C D
4: B
5: B D
6: B C
7: B C D
8: A
9: A D
10: A C
11: A C D
12: A B
13: A B D
14: A B C
15: A B C D
[peter@wasabi peter]$
More caffine than time ;-)
------------------------------
Date: Tue, 08 Jun 2004 19:03:00 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: Converting a string to multiple search patterns
Message-Id: <pan.2004.06.08.16.47.37.507107@aursand.no>
On Tue, 08 Jun 2004 17:30:52 +0100, Peter Hickman wrote:
> Is this what you are after?
> [...]
No. It still doesn't create the pattern excactly how I want it. Maybe
there's no logic in my pattern, but... :)
Take a look at one of my previous posts (a reply to Anno) where I outline
the pattern the way I want it. Example: 'ABC' and 'BCD' should follow
right after 'ABCD'.
--
Tore Aursand <tore@aursand.no>
"Scientists are complaining that the new "Dinosaur" movie shows
dinosaurs with lemurs, who didn't evolve for another million years.
They're afraid the movie will give kids a mistaken impression. What
about the fact that the dinosaurs are singing and dancing?" (Jay Leno)
------------------------------
Date: 8 Jun 2004 08:07:00 -0700
From: genericax@hotmail.com (Sara)
Subject: Re: Cute bit of Perl to Assign $1,$2 to named variables
Message-Id: <776e0325.0406080707.58ecc2c1@posting.google.com>
zzapper <david@tvis.co.uk> wrote in message news:<opv0c05esg0ilmpn9cnqiehi8qnltk79pi@4ax.com>...
> On Fri, 4 Jun 2004 09:53:29 -0400, wrote:
>
> >On Fri, 4 Jun 2004, zzapper wrote:
> >
> >> Hi
> >>
> >> Found this in the doc somewhere.
> >>
> >> $_="___abc___123_";
> >> my ($first,$second)=/([a-z]+).*?([0-9]+)/i;
> >> print "\n$first : $second\n";
> >>
> >> Note the absense of a ~
> >
> >There's nothing cute or unusual about this. It's standard, well-defined,
> >and documented code.
> >
> >Are you asking us to explain to you how it works?
> >
> >Paul Lalli
> Paul,
> Thanx I'm fine on how it works.
> I've been writing Perl since 1994, but the above was new to me!!
>
> zzapper (vim, cygwin, wiki & zsh)
David:
Unfortunately it's taboo here to be a long-time Perl programmer and
not know every possible language construct, or every line of every
Perldoc. Right or wrong, that's the nature of your set of critics
here.
Lesson: wear a flame-proof suit if you announce a new discovery!
BTW, I appreciated your post and learned something from it. I've
"stumbled on" this result before when I accidentally left out a ~, but
I never really found it all that useful. Your example was good,
thanks.
Cheers,
G
------------------------------
Date: 8 Jun 2004 08:22:11 -0700
From: hamelcd@hotmail.com (Christopher Hamel)
Subject: Re: Cute bit of Perl to Assign $1,$2 to named variables
Message-Id: <4f60d5b3.0406080722.67139bc3@posting.google.com>
david@tvis.co.uk (zzapper) wrote in message
> How about some of the people in this thread posting a few of their
> Perl tit-bits
>
> zzapper
I, for one, love &&=. Most others seem to feel it's as useless and
obscure as the "reset" function, but I think it's quite useful and
very Perl-esque.
------------------------------
Date: Tue, 08 Jun 2004 12:59:07 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Cute bit of Perl to Assign $1,$2 to named variables
Message-Id: <Lrmdnec56brAbVjdRVn-vA@adelphia.com>
Sara wrote:
> Lesson: wear a flame-proof suit if you announce a new discovery!
I think the proper lesson is to make sure your "discovery" is in fact new
before bragging about it.
The lesson is not unique to this group - you'd have gotten much the same
reaction if you'd announced the "discovery" of a metal with atomic weight
94 to a group of chemists.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Tue, 08 Jun 2004 16:47:02 GMT
From: Juha Laiho <Juha.Laiho@iki.fi>
Subject: Re: Do you ever use awk?
Message-Id: <ca4qhh$3dn$1@ichaos.ichaos-int>
kj <socyl@987jk.com> said:
>Do people proficient in Perl have any use for awk/gawk anymore?
>Are there things that you still prefer to do using awk/gawk over
>perl?
I recently thought about this, and ended up having the tools set
up in three categories. The first category containing the traditional
shell tools (cat, sed, grep, sort, ...). The second category was
"what do I use when I'm doing something where this combination is
no longer convenient to solve the problem at hand"; that category
was just "awk". Then, a third category was "what to use when awk
is not enough", and this was "perl" (including needed modules on
a case-by-case basis).
This third category includes things like cases where networking or
database connectivity is needed, and also things where I do something
requiring data structures more complex than provided by the awk hashes
(mostly hierarchical data, so hashes of hashes and so).
Depending on the host OS, I've lately begun skipping the awk stage
and just doing with perl whatever doesn't feel convenient to solve
with traditional tools. This mostly is when I'm working on a Linux
system, where I can assume existence of perl.
On the other hand, on systems running proprietary Unixes (Solaris,
HP-UX), I don't take existence of perl for granted (even though
perl might be installed on that single machine where I'm writing
the new tool), so in these environments I tend to avoid using perl
much longer. But I guess using awk to write output to be executed
in dtksh eval statement (to get dynamic fill-in forms) was overdoing
it... :-)
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
------------------------------
Date: 8 Jun 2004 09:58:54 -0700
From: spacehopper_man@yahoo.com (Oliver)
Subject: Re: killing my children
Message-Id: <5818fca8.0406080858.220b7610@posting.google.com>
ok - thanks very much - I still have problems getting my children to
exit - all works ok if I use POSIX::_exit :(
problem - children not going away when they've finished their work.
solution: fork as per normal - then keep track like this:
sub is_alive {
my $pid = shift;
my $status = waitpid $pid, WNOHANG;
return ($status == -1) ? 0 : 1;
}
and use SIG{CHLD} = 'IGNORE'
it works!... nearly...
it only works if the child exits like this:
POSIX::_exit(0); # force exit now
..this is the crux of my problem.
if I use normal perl 'exit' then the children hang around - and my
parent process has also stopped exiting too.
I guess what's happening is that some DESTROY or END method is hanging
- and _exit skips these.
Each child is doing this and only this:
my $smtp = Net::SMTP->new('mail', Timeout => 60) || die $!;
$smtp->mail($from) or die "net_smtp mail from failed";
$smtp->to($to) or die "net_smtp rcpt to failed";
$smtp->data($data) or die "net_smtp data failed";
$smtp->quit() or die "net_smtp quit failed";
undef($smtp);
anyone got any idea why Net::SMTP is causing this, or whether
POSIX:_exit is likely to cause me problems ??
------------------------------
Date: 08 Jun 2004 17:12:39 GMT
From: ctcgag@hotmail.com
Subject: Re: killing my children
Message-Id: <20040608131239.827$Wi@newsreader.com>
spacehopper_man@yahoo.com (Oliver) wrote:
> it only works if the child exits like this:
>
> POSIX::_exit(0); # force exit now
>
> ..this is the crux of my problem.
> if I use normal perl 'exit' then the children hang around - and my
> parent process has also stopped exiting too.
>
> I guess what's happening is that some DESTROY or END method is hanging
> - and _exit skips these.
If the child inherits large data structures from the parent, it can
take a *long* time to clean these up upon normal exit.
Are the children burning CPU, or just sitting around idle?
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Tue, 8 Jun 2004 16:47:18 +0100
From: "Lord0" <lawrence.tierney@bipsolutions.com>
Subject: Magic var for Current "Index" in array within loop?
Message-Id: <C4mxc.247$DF5.35@newsfe2-gui.server.ntli.net>
Hi there,
Is there a magic variable which will return the current index of an array if
used in a loop?
i.e.
# I don't like
my $i=0;
for(@my_array){
# do something using $i
$i++;
}
# I would like to
for(@my_array){
# do something using "magic" variable
# So in third iteration of loop the magic variable would be 2 etc
}
I know about $[ and $# but they don't do the do...........
Cheers
Lord0
http://ltierney.demon.co.uk
------------------------------
Date: Tue, 08 Jun 2004 18:00:17 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: Magic var for Current "Index" in array within loop?
Message-Id: <pan.2004.06.08.15.59.49.55437@aursand.no>
On Tue, 08 Jun 2004 16:47:18 +0100, Lord0 wrote:
> Is there a magic variable which will return the current index of an array if
> used in a loop?
No. You have to count it yourself the C-way:
for ( my $i = 0; $i <= $#array; $i++ ) {
print "Element $i = " . $array[$i] . "\n";
}
I don't like that one, either, so I constantly use this one instead:
for my $i ( 0..$#array ) {
print "Element $i = " . $array[$i] . "\n";
}
Eventually:
for ( 0..$#array ) {
print "Element $_ = " . $array[$_] . "\n";
}
--
Tore Aursand <tore@aursand.no>
"First, God created idiots. That was just for practice. Then He created
school boards." (Mark Twain)
------------------------------
Date: 08 Jun 2004 18:26:54 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Magic var for Current "Index" in array within loop?
Message-Id: <u9wu2ineox.fsf@wcl-l.bham.ac.uk>
"Lord0" <lawrence.tierney@bipsolutions.com> writes:
> Is there a magic variable which will return the current index of an array if
> used in a loop?
This question has been asked twice in .misc and once in .moderated in
the last week^H^H^H^H fourtnight.
Invisible Array Loop Counter?
Basic newbie questions
On "for (@foo)"
Usually it only pops up once every few weeks.
What the $EXPLETIVE is going on?
Is this just a random statistical variation or has someone set it as homework?
Does anyone want to submit a FAQ entry?
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Tue, 8 Jun 2004 15:14:05 +0100
From: "Bigus" <someone@somewhere.com>
Subject: Meet the flocker
Message-Id: <ca4hjd$thk@newton.cc.rl.ac.uk>
Hi
I am attempting to flock for the very first time - after some surfing
around, the following 2 methods sound like the most viable/robust I can
find:
==== METHOD 1 ====
use strict;
use warnings;
my $file = "flocktest.txt";
open FH, "+<$file" or die "Cannot open $file: $!";
flock FH, 2; # lock file
seek FH, 0, 0; # back to beginning of file
my @lines = <FH>; # get current content
### do stuff to content ###
seek FH, 0, 0; # back to beginning of file
truncate FH, 0; # empty file
print FH @lines; # print new content to file
close FH;
==== METHOD 2 ====
use strict;
use warnings;
my $file = 'flocktest.txt';
my $lockfile = $file.".lock"; # create dummy file to lock
open(LOCK, ">$lockfile") or die "Can't open $lockfile($!)";
flock(LOCK, 2); # lock file
open(FH, $file) or die "Can't open $file ($!)";
my @lines = <FH>; # get current content
close FH;
### do stuff to content ###
open(FH, ">$file") or die "Can't open $file ($!)";
print FH @lines; # print new content to file
close FH;
close LOCK;
==== END SAMPLE CODE =====
Are there any issues with these methods I should be aware of? Which one of
these, or other, methods would you be inclined to choose / think is better?
So far, I'm leaning towards the 2nd one since method 1 uses the seek &
truncate functions which I have not used before. However, only having to
open a file once in read/write mode does sound less "messy" somehow.
One other general thing - if another instance of the above scripts tried to
access the file while it was locked would it wait until it's free or would I
have to include a retry loop in the script?
Thanks
Bigus
------------------------------
Date: 08 Jun 2004 17:31:34 GMT
From: ctcgag@hotmail.com
Subject: Re: Meet the flocker
Message-Id: <20040608133134.541$5R@newsreader.com>
"Bigus" <someone@somewhere.com> wrote:
> Hi
>
> I am attempting to flock for the very first time - after some surfing
> around, the following 2 methods sound like the most viable/robust I can
> find:
>
> ==== METHOD 1 ====
> use strict;
> use warnings;
>
> my $file = "flocktest.txt";
> open FH, "+<$file" or die "Cannot open $file: $!";
> flock FH, 2; # lock file
flock can return false. You should check the results. Use an "or die $!"
if nothing more appropriate springs to mind.
Also, you should probably import and use LOCK_EX rather than hard-coding 2.
>
> Are there any issues with these methods I should be aware of? Which one
> of these, or other, methods would you be inclined to choose / think is
> better?
The first one.
> So far, I'm leaning towards the 2nd one since method 1 uses the seek &
> truncate functions which I have not used before. However, only having to
> open a file once in read/write mode does sound less "messy" somehow.
I often take something like the second route out of laziness to learn
seek. But that's not something I'm proud of. Since you've gone to the
trouble of making the first example, you may as well use it. :)
> One other general thing - if another instance of the above scripts tried
> to access the file while it was locked would it wait until it's free or
> would I have to include a retry loop in the script?
It would wait at the flock until it became free. (Unless of course one
version uses method 1, and another version uses method 2, then they would
gladly stomp on each other.)
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 8 Jun 2004 08:18:25 -0700
From: trezaei@hotmail.com (Tan Rezaei)
Subject: Modifying the input
Message-Id: <8c92d509.0406080718.4919170e@posting.google.com>
Hi All,
I know this is proabably simple but I can't get my mind around it:
I need to read from a file that has many records.
I need to read from this file and depending on what the records say I
need to perform some certain tesks. Well I got that all figured out.
My problem is that I need to change one of the fields on the records
showing that I have already read it and setting some status code so
next time I read the file I will know if I need to do anything more
with that record.
So how do I change records in the file I am reading from? In some
instances I may even need to removed the record from the middle of the
file.
I know you're thinking that I should be using a database but take my
word that is not going to happen in the environment I am working in.
It HAS TO BE A FLAT FILE. I would love to use a DB and import the
files into it and work with them but my hands are tied.
Thanks
T
------------------------------
Date: Tue, 08 Jun 2004 17:48:05 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: Modifying the input
Message-Id: <pan.2004.06.08.15.48.04.476537@aursand.no>
On Tue, 08 Jun 2004 08:18:25 -0700, Tan Rezaei wrote:
> So how do I change records in the file I am reading from? In some
> instances I may even need to removed the record from the middle of the
> file.
Write to a new, temporary file, while reading through the original file.
Change whatever suits you on the way, and finally overwrite [1] the
original file with the new (temporary) file.
[1] perldoc -f rename
perldoc -f unlink
perldoc File::Copy
> I know you're thinking that I should be using a database but take my
> word that is not going to happen in the environment I am working in.
> It HAS TO BE A FLAT FILE. I would love to use a DB and import the
> files into it and work with them but my hands are tied.
Take a look at CPAN [2]; There are lots of modules there which lets you
work with flat files as you'd do with a database.
[2] <http://www.cpan.org/>
--
Tore Aursand <tore@aursand.no>
"Leadership is doing what is right when no one is watching." (George
Van Valkenburg)
------------------------------
Date: Tue, 08 Jun 2004 16:42:27 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: Modifying the input
Message-Id: <Xns95028144C3A78dkwwashere@216.168.3.30>
Tore Aursand <tore@aursand.no> wrote:
> On Tue, 08 Jun 2004 08:18:25 -0700, Tan Rezaei wrote:
>> So how do I change records in the file I am reading from? In some
>> instances I may even need to removed the record from the middle
>> of the file.
>
> Write to a new, temporary file, while reading through the original
> file. Change whatever suits you on the way, and finally overwrite
> [1] the original file with the new (temporary) file.
Or just use Tie::File, which makes it much easier, IMHO.
------------------------------
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 6665
***************************************