[15497] in Perl-Users-Digest
Perl-Users Digest, Issue: 2907 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Apr 29 21:10:18 2000
Date: Sat, 29 Apr 2000 18:10:11 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <957057011-v9-i2907@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Sat, 29 Apr 2000 Volume: 9 Number: 2907
Today's topics:
sort 2d array numerically by row? <m002622@csb.scs.usna.navy.mil>
Re: sort 2d array numerically by row? <m002622@usna.edu>
Using variables in 'use lib' path. <you.will.always.find.him.in.the.kitchen@parties>
Re: Using variables in 'use lib' path. <bwalton@rochester.rr.com>
Visual Basic mid$ - Perl equivalent (data in mid strin <dfitzgerald@kscable.com>
Re: Visual Basic mid$ - Perl equivalent (data in mid s <bowman@montana.com>
Where is it found? <ppi@searchy.net>
Re: Where is it found? (Clinton A. Pierce)
Re: Where is it found? <godzilla@stomp.stomp.tokyo>
Re: Where is it found? <phill@modulus.com.au>
Re: Where is it found? <lr@hpl.hp.com>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 29 Apr 2000 18:46:31 -0400
From: "Eric Hardisty" <m002622@csb.scs.usna.navy.mil>
Subject: sort 2d array numerically by row?
Message-Id: <8efouf$c5h$1@news.usna.navy.mil>
Hi, I've got a simple question for you all:
I have a LOL, @distances that is two D. It's an array of distances, with a
classification--essentially ordered pairs.
How can I sort by the x value? In other words, the value held in:
$distances[don't care][0]?
I imagine that this is just a one-liner with some crazy subscripting, and
I've looked it up in the camel book but I'm missing something.
Thanks,
Eric
------------------------------
Date: Sat, 29 Apr 2000 18:25:08 -0400
From: MIDN Eric Hardisty <m002622@usna.edu>
Subject: Re: sort 2d array numerically by row?
Message-Id: <390B6144.375B621@usna.edu>
Eric Hardisty wrote:
>
> Hi, I've got a simple question for you all:
>
> I have a LOL, @distances that is two D. It's an array of distances, with a
> classification--essentially ordered pairs.
>
> How can I sort by the x value? In other words, the value held in:
> $distances[don't care][0]?
>
> I imagine that this is just a one-liner with some crazy subscripting, and
> I've looked it up in the camel book but I'm missing something.
>
> Thanks,
> Eric
Got it:
@distances = sort {$a->[0] <=> $b->[0]} @distances
sorry to waste your time. Maybe some other newbie can use this too.
--Eric
------------------------------
Date: Sun, 30 Apr 2000 08:36:58 +1200
From: "Tintin" <you.will.always.find.him.in.the.kitchen@parties>
Subject: Using variables in 'use lib' path.
Message-Id: <957040552.579902@shelley.paradise.net.nz>
Can someone explain to my why:
use FindBin qw($Bin);
use lib "$Bin/lib";
use MyModule;
works, but if I use:
$Bin='/home/peters/';
use lib "$Bin/lib";
use MyModule;
It fails to update @INC.
------------------------------
Date: Sun, 30 Apr 2000 00:52:06 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: Using variables in 'use lib' path.
Message-Id: <390B82C9.A0B46C2A@rochester.rr.com>
Tintin wrote:
>
> Can someone explain to my why:
>
> use FindBin qw($Bin);
> use lib "$Bin/lib";
>
> use MyModule;
>
> works, but if I use:
>
> $Bin='/home/peters/';
> use lib "$Bin/lib";
>
> use MyModule;
>
> It fails to update @INC.
From the 'lib' doc page:
lib - manipulate @INC at compile time
^^^^^^^^^^^^
Your statement $Bin=... is executed at run time, while the 'use'
statement is executed at compile time.
Try [not tested]:
BEGIN{
$Bin='/home/peters/';
use lib "$Bin/lib";
}
That will probably work, since the $Bin=... is executed at compile time.
--
Bob Walton
------------------------------
Date: Sun, 30 Apr 2000 00:45:22 GMT
From: Kent FitzGerald <dfitzgerald@kscable.com>
Subject: Visual Basic mid$ - Perl equivalent (data in mid string)
Message-Id: <390B81F5.6196F63B@kscable.com>
I have a large text file where the data that needs to be checked is at a
consistent location on each line. I undestand how to open the file and
read the lines. The problem that I currently have is try to just check
one location of the string. In Visual Basic:
do while eof(1)
line input #1, rec$
if mid$(rec$,1,4) = "fooo"" then
whatever
else whatever
end if
loop
I know this is not a Visual Basic group but I wanted to make sure I was
as clear as possible.
------------------------------
Date: Sat, 29 Apr 2000 14:54:50 -0600
From: "bowman" <bowman@montana.com>
Subject: Re: Visual Basic mid$ - Perl equivalent (data in mid string)
Message-Id: <ImLO4.1835$cO.10553@newsfeed.slurp.net>
Kent FitzGerald <dfitzgerald@kscable.com> wrote in message
news:390B81F5.6196F63B@kscable.com...
>
> if mid$(rec$,1,4) = "fooo"" then
perldoc -f substr
------------------------------
Date: Sat, 29 Apr 2000 22:35:29 +0200
From: Penpal International <ppi@searchy.net>
Subject: Where is it found?
Message-Id: <390B4791.65C23C4C@searchy.net>
How can I return the position where a regexp has found a result. For
example:
$string = "This is just some string";
if ($string =~ /some/i) {
print "Found some. It's was XXth word";
}
On the XX I like to have which word it was, in this case 4. If this
isn't possible, is it possible the print the number of the n-th byte it
was found on.
Thanks,
Frank de Bot
--
Penpal International
http://ppi.searchy.net/
ppi@searchy.net
------------------------------
Date: Sat, 29 Apr 2000 23:06:23 GMT
From: clintp@geeksalad.org (Clinton A. Pierce)
Subject: Re: Where is it found?
Message-Id: <PVJO4.53217$h01.376167@news1.rdc1.mi.home.com>
[Posted and mailed]
In article <390B4791.65C23C4C@searchy.net>,
Penpal International <ppi@searchy.net> writes:
> How can I return the position where a regexp has found a result. For
> example:
Hmm...that's tough. There's probably only a half a dozen ways to do
this. :)
> $string = "This is just some string";
> if ($string =~ /some/i) {
> print "Found some. It's was XXth word";
> }
If you're not married to regular expressions for some reason, you
could use index():
$start=index(lc($string), "some");
Otherwise some kludgy thing like this might work:
if ($string=~/(.*?)some/i) {
print "At offset ", length($1), "\n";
}
> On the XX I like to have which word it was, in this case 4. If this
> isn't possible, is it possible the print the number of the n-th byte it
> was found on.
Your best bet for which word might be to split the string into words,
and then search the array.
--
Clinton A. Pierce Teach Yourself Perl in 24 Hours!
clintp@geeksalad.org for details see http://www.geeksalad.org
"If you rush a Miracle Man,
you get rotten Miracles." --Miracle Max, The Princess Bride
------------------------------
Date: Sat, 29 Apr 2000 16:16:02 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Where is it found?
Message-Id: <390B6D32.402C7FA8@stomp.stomp.tokyo>
PenPal asks for help:
> How can I return the position where a regexp
> has found a result. For example:
> $string = "This is just some string";
> if ($string =~ /some/i) {
> print "Found some. It's was XXth word";
> }
> On the XX I like to have which word it was, in this case 4.
> If this isn't possible, is it possible the print the number
> of the n-th byte it was found on.
Hi PenPal!
You can find the 'index numerical value' by using,
$counter = index ($string, "some");
Which will return a value of 13 for "s" in
your word 'some' but keeping in mind the first
position is an oxymoronic value of zero. =)
Pretty sure it is number 13. I used both my
fingers and toes for counting. This is a
difficult task for me, using my toes that is.
In closing, I have attached a very simple
script which accomplishes what you want
for 'what word number' is a selected word.
Nothing eloquent about this code. It is written
simple and with a lot of spacing so you can read
it and understand what happens easier.
Read over this simple code and keep in mind
some problems you will encounter. Most obvious
problem is how to find 'some' without getting
hits on other words like, 'someday', 'something'
and others with 'some' as a root word. My quick
solution in my matching operator is to place
a space after some. However, if 'some' falls at
the end of a line or has punctuation after it,
this matching operator will fail. There are some
problems in doing this. Testing will reveal what
are those problems.
Nonetheless, this simple code will give you a
basic idea of how this is done and, you can
adapt, modify and build upon this, if you
choose to do so. This code is designed to
display concepts rather than perfection.
Basic idea is you can use transliteration
to count words, in this case, I replace my
'target word' and all following with a
space. I am counting spaces by replacing
each space with a space! Use of tr allows
you to pull a numerical position as if
a word is there, by counting spaces.
$pretend_input represents what could come
in from a form action for your target word.
I give two examples of how to use transliteration
to count words via a bit of imagination. Final
example, TEST THREE, is to show how you can do this
using an array. This would work better for a larger
data base. An array split based on a space is not
a good idea but works for this simple example.
Besides obvious inherent bugs with this, there
is one precaution. If your news reader word
wraps my longer lines, be sure $test_string,
in all cases, is ONE long line. Using this
tr will count white space as well if this
line is split up into two lines. Careful!
There are more eloquent ways to do this,
shorter combined coding could be used and,
rest assured there are completely different
ways of doing this. My intent here is to
display basic functions on how this could
be accomplished so you will have a base
of knowledge with which to work.
Test this! You will find it does what is
should and, you will find there are problems
you will have to work around.
Godzilla!
Printed results first, code last:
=====================
Printed Results:
TEST ONE
some is word number 4
TEST TWO
just is word number 3
TEST THREE
some is word number 4
some is word number 9
=====================
#!/usr/local/bin/perl
print "Content-Type: text/plain\n\n";
$test_string = "This is just some string";
print "TEST ONE \n\n";
$test_one = $test_string;
$pretend_input = "some";
$test_one =~ s/$pretend_input (.*)/ /ig;
$counter = ($test_one =~ tr/ / /);
print "$pretend_input is word number $counter \n\n";
print "TEST TWO \n\n";
$test_two = $test_string;
$pretend_input2 = "just";
$test_two =~ s/$pretend_input2 (.*)/ /ig;
$counter2 = ($test_two =~ tr/ / /);
print "$pretend_input2 is word number $counter2 \n\n";
print "TEST THREE \n\n";
$test_string3 = "This is just some string and just another some string";
@Test_Array = split (/ /, $test_string3);
$pretend_input3 = "some";
$counter3 = 1;
foreach $test_word (@Test_Array)
{
if ($test_word =~ /$pretend_input3/)
{
print "$pretend_input3 is word number $counter3 \n\n";
}
$counter3++;
}
exit;
------------------------------
Date: Sun, 30 Apr 2000 08:45:24 +1000
From: Peter Hill <phill@modulus.com.au>
Subject: Re: Where is it found?
Message-Id: <390B6604.2C96@modulus.com.au>
Penpal International wrote:
>
> How can I return the position where a regexp has found a result. For
> example:
>
> $string = "This is just some string";
> if ($string =~ /some/i) {
> print "Found some. It's was XXth word";
> }
>
> On the XX I like to have which word it was, in this case 4. If this
> isn't possible, is it possible the print the number of the n-th byte it
> was found on.
>
> Thanks,
>
> Frank de Bot
For the simple case of 'some' as the search, you don't need a regex, and
can obtain the desired result as a zero-based index into the search
string via:
$pos = index($string,'some');
which should also be marginally quicker.
HTH
--
Peter Hill,
Modulus Pty. Ltd.,
http://www.modulus.com.au/
------------------------------
Date: Sat, 29 Apr 2000 17:14:14 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Where is it found?
Message-Id: <MPG.13751aa2be16b22098a9b9@nntp.hpl.hp.com>
In article <390B6D32.402C7FA8@stomp.stomp.tokyo> on Sat, 29 Apr 2000
16:16:02 -0700, Godzilla! <godzilla@stomp.stomp.tokyo> says...
> PenPal asks for help:
> > How can I return the position where a regexp
> > has found a result. For example:
>
> > $string = "This is just some string";
> > if ($string =~ /some/i) {
> > print "Found some. It's was XXth word";
> > }
>
> > On the XX I like to have which word it was, in this case 4.
> > If this isn't possible, is it possible the print the number
> > of the n-th byte it was found on.
...
> You can find the 'index numerical value' by using,
>
> $counter = index ($string, "some");
>
> Which will return a value of 13 for "s" in
> your word 'some' but keeping in mind the first
> position is an oxymoronic value of zero. =)
>
> Pretty sure it is number 13. I used both my
> fingers and toes for counting. This is a
> difficult task for me, using my toes that is.
Some of us might use Perl for this task, on the theory that computers
are better at counting than people are (though they probably don't have
as many fingers and toes).
perl -le "print index 'This is just some string', 'some'"
> In closing, I have attached a very simple
> script which accomplishes what you want
> for 'what word number' is a selected word.
>
> Nothing eloquent about this code. It is written
> simple and with a lot of spacing so you can read
> it and understand what happens easier.
>
> Read over this simple code and keep in mind
> some problems you will encounter. Most obvious
> problem is how to find 'some' without getting
> hits on other words like, 'someday', 'something'
> and others with 'some' as a root word. My quick
> solution in my matching operator is to place
> a space after some. However, if 'some' falls at
> the end of a line or has punctuation after it,
> this matching operator will fail.
That is why using the \b word-boundary assertion is better than mucking
around with spaces.
#!/usr/bin/perl -w
use strict;
$_ = 'This is just some string';
my $find = 'some';
for (my $i = 1; /\b(\w+)\b/g; ++$i) {
$1 eq $find and print("Found '$find'. It was the ${i}th word.\n"),
last;
}
__END__
There is a CPAN module (search for Lingua, I believe) that does a better
job on that ordinal suffix, but that is beyond the scope of this
subject. :-)
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 V9 Issue 2907
**************************************