Help - Search - Member List - Calendar
Full Version: combinations
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44
Support our Sponsors!
Andrew Gaffney
I completely understand the way the Perl's namespaces work. Here is what confused me. In
my module, I created a function that connects to a MySQL database and then returns a DB
handle. In the script that receives the DB handle, I removed the 'use DBI;' line. I can
make queries against the database without problems. Why does this work?

[Email Removed] wrote:
QUOTE
That would be true in some languages. Not in Perl. Modules only "exist" in
the lexical scope (usually a block, in this case a file/package). So, there's no
danger in polluting namespace from that module for sure. But, as with
everything else in Perl, you can access it if you go out of your way.

So, if you use Data::Dumper in the Foo::Bar class, and you call the Foo::Bar
class (via use Foo::Bar), you'd have to do &Foo::Bar::Dumper to use it. If you
understand. Which you probably don't. So wait until someone else answers. :-)

Will

In a message dated 2/17/2004 7:21:35 PM Eastern Standard Time,
[Email Removed] writes:
I've been using Perl for about a year. I'm writing my own module that
contains some custom
functions that I use a lot. In doing this, I started wondering how modules
work. For
example, if my module has 'use SomeModule;' and a script I write has 'use
MyModule;', does
that mean that I am effectively saying 'use SomeModule;' in my script?



--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.


Wiggins D'Anconia
[Email Removed] wrote:
QUOTE
You are in fact using the DBI module. The DB handle is probably (I think it
is) a bless reference that will send you straight to the DBI module that's
called in the function you've created. Although you're not using the DBI module in
your code, you're indirectly using it without ever knowing it if you made a
good enough class.

I think. I hope. Maybe I'm completely wrong. I don't know. Someone will
correct me though. Right or Wrong.

Will


Generally you are correct, though there is a little "and then in step 3
black magic happens and..." to your answer.

Presumably the 'use DBI' is still in the top of the personal module. It
must be somewhere (obviously), and that is generally where it would go.
In this case the module is accessing the DBI functions, so 'use DBI'
goes at the top of the module, then the script is accessing the MyModule
functions, so 'use MyModule' goes at the top of the script... but, Perl
has only one @INC/%INC, so once a library is loaded... it is loaded.
This allows you to get away with a few coding problems, specifically you
may load and access a library in one module, then load a second that
accesses the library's code, but can only do so because the other module
had loaded it, so if you removed the 'use' for that module the code
would break. (I just found one of these in my own code while unit
testing...)

OP, Have you read:

perldoc perlmod
perldoc perlmodlib
perldoc perlmodstyle
perldoc -f use
perldoc -f package

And you may want to consider the Learning Perl Objects, References, and
Modules from O'Reilly...

http://danconia.org

Wiggins D'Anconia
Andrew Gaffney wrote:
QUOTE
Wiggins d'Anconia wrote:

[snip]

QUOTE
And you may want to consider the Learning Perl Objects, References,
and Modules from O'Reilly...


I will look into it. I've already read Learning Perl and I own Perl
Cookbook, Advanced Perl Programming, and the Perl Pocket Reference,
which is very nifty and actually fits in my pocket! :)


Advanced Perl Programming covers most of what is in LPORM, but not in as
readable of a fashion. It is also a bit dated, but you can probably get
most of what you need from the perldoc's. If you just like being a
zookeeper (like some of us) then LPORM is worth picking up and the time
to read.

http://danconia.org

Drieux
On Feb 17, 2004, at 5:07 PM, Andrew Gaffney wrote:

QUOTE
In my module, I created a function that connects to a MySQL database
and then returns a DB handle. In the script that receives the DB
handle, I removed the 'use DBI;' line. I can make queries against the
database without problems. Why does this work?

I presume that you did the

package My::DB
use DBI;

and that your function is say

sub do_get_db_handle
{
....
$dbh = DBI->connect($data_source, $user, $pass, $driver);
...
$dbh;
}

hence it is called like

use My::DB;
...
my $db = do_get_db_handle(@arglist);
...
$rv = $db->do($statement);

as Will has noted he thinks that the $dbh is a 'blessed' referece,
if you rummage around inside it you will find that yes, it is a
blessed reference. Since your module has already asserted the

'use DBI'

it will have already both 'required' and 'imported' all of
the stuff 'needed' to understand what a blessed reference to
a DBI is all about.

As such your calling script did not need to make the 'use DBI'
since it would be required IN your module. This is a part of
how/why one likes to encapsulate things into perl modules so
that one can hide the common repeatively repeated redundent parts
without having to keep retyping them over and over and over again
in each new script that you need.

HTH.


ciao
drieux

---

Andrew Gaffney
Wiggins d'Anconia wrote:
QUOTE
[Email Removed] wrote:

You are in fact using the DBI module. The DB handle is probably (I
think it is) a bless reference that will send you straight to the DBI
module that's called in the function you've created. Although you're
not using the DBI module in your code, you're indirectly using it
without ever knowing it if you made a good enough class.
I think. I hope. Maybe I'm completely wrong. I don't know. Someone
will correct me though. Right or Wrong.

Will


Generally you are correct, though there is a little "and then in step 3
black magic happens and..." to your answer.

Presumably the 'use DBI' is still in the top of the personal module.  It
must be somewhere (obviously), and that is generally where it would go.
In this case the module is accessing the DBI functions, so 'use DBI'
goes at the top of the module, then the script is accessing the MyModule
functions, so 'use MyModule' goes at the top of the script... but, Perl
has only one @INC/%INC, so once a library is loaded... it is loaded.

Yes, I have 'use DBI', well actually 'use Apache::DBI', in MyModule. So, if I were to try
to create the database handle in my script instead of MyModule, it would fail since my
script knows nothing of the DBI module.

QUOTE
OP, Have you read:

perldoc perlmod
perldoc perlmodlib
perldoc perlmodstyle
perldoc -f use
perldoc -f package

No, but I will.

QUOTE
And you may want to consider the Learning Perl Objects, References, and
Modules from O'Reilly...

I will look into it. I've already read Learning Perl and I own Perl Cookbook, Advanced
Perl Programming, and the Perl Pocket Reference, which is very nifty and actually fits in
my pocket! :)

--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.


Andrew Gaffney
drieux wrote:
QUOTE

On Feb 17, 2004, at 5:07 PM, Andrew Gaffney wrote:

In my module, I created a function that connects to a MySQL database
and then returns a DB handle. In the script that receives the DB
handle, I removed the 'use DBI;' line. I can make queries against the
database without problems. Why does this work?


I presume that you did the

package My::DB
use DBI;

and that your function is say

sub do_get_db_handle
{
....
$dbh = DBI->connect($data_source, $user, $pass, $driver);
...
$dbh;
}

hence it is called like

use My::DB;
...
my $db = do_get_db_handle(@arglist);
...
$rv  = $db->do($statement);

That is the gist of my code, yes.

QUOTE
as Will has noted he thinks that the $dbh is a 'blessed' referece,
if you rummage around inside it you will find that yes, it is a
blessed reference. Since your module has already asserted the

'use DBI'

it will have already both 'required' and 'imported' all of
the stuff 'needed' to understand what a blessed reference to
a DBI is all about.

As such your calling script did not need to make the 'use DBI'
since it would be required IN your module. This is a part of
how/why one likes to encapsulate things into perl modules so
that one can hide the common repeatively repeated redundent parts
without having to keep retyping them over and over and over again
in each new script that you need.

This is what I didn't quite understand. I didn't realize that Perl's "black magic" allowed
the blessed reference to refer back to the object already in memory. In effect, the
blessed reference carries around the entire class object with it.

--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.


R. Joseph Newton
Andrew Gaffney wrote:

QUOTE

This is what I didn't quite understand. I didn't realize that Perl's "black magic" allowed
the blessed reference to refer back to the object already in memory. In effect, the
blessed reference carries around the entire class object with it.

Not quite. The beauty of references is that you don't have to lug the whole object around.
The reference is more like a map that shows where to very quickly find the object. So what
you carry around evcerywhere is access to the object, not the object itself.

Joseph

Andrew Gaffney
R. Joseph Newton wrote:
QUOTE
Andrew Gaffney wrote:


This is what I didn't quite understand. I didn't realize that Perl's "black magic" allowed
the blessed reference to refer back to the object already in memory. In effect, the
blessed reference carries around the entire class object with it.


Not quite.  The beauty of references is that you don't have to lug the whole object around.
The reference is more like a map that shows where to very quickly find the object.  So what
you carry around evcerywhere is access to the object, not the object itself.

Thanks for the clarification.

--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.


David Le Blanc
You also have the less obvious

$|=1

which sets autoflush to on for the currently
selected filehandle.

as well as

use IO::Handle;

FILEHANDLE->autoflush(x) x=0/undef for off, and 1 for on.

And again

use IO::Handle
printflush FILEHANDLE "stuff to print";

for a print with a built in flush :-)

Please don't ask for that in english..

QUOTE
-----Original Message-----
From: Rob Dixon [mailto:[Email Removed]]
Sent: Wednesday, 18 February 2004 5:23 AM
To: [Email Removed]
Subject: Re: Flushing FileHandle's Buffer

Balaji thoguluva wrote:

I would like to know if there is a function(other
than close FILEHANDLE) to flush current filehandle's
buffer data to the file. In otherwords, I want to make
sure the following PRINT statement to work immediately
after its execution.

print FILEHANDLE "Flush this data IMMEDIATELY to the
file";

Hi Balaji.

If you

use IO::Handle;

then you can

flush FILEHANDLE;

whenever you want, or turn on autoflush with

autoflush FILEHANDLE;

which will flush the buffer after every print.

HTH,

Rob



--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response




In a message dated 2/18/2004 9:48:24 AM Eastern Standard Time,
[Email Removed] writes:
IIRC, I believe Apache uses the shebang line in Perl scripts on the Windows
platform.
You are correct. It does.

Will

Keith
UNCLASSIFIED

IIRC, I believe Apache uses the shebang line in Perl scripts on the Windows
platform.

-----Original Message-----
From: R. Joseph Newton [mailto:[Email Removed]]
Sent: Wednesday, February 18, 2004 9:48 AM
To: Jan Eden
Cc: Joel; Perl Lists
Subject: Re: Why does this keep happening?


Jan Eden wrote:

QUOTE

Apart from the missing semicolon, I don't know if Perl on Windows like the
shebang line at the beginning. With Unix, it's the right way to do it.


It doesn't care. It ignores the path information, but does read any
switches on the line.

Joseph


--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Actually, this is a little bit of an incorrect question. If your first code
worked (which it doesn't for me by the way), and if in your second
$config->{URL_1} does equal http://rajeshd, they're the same. So, the problem is somewhere
else.

William M West
QUOTE
Is there any separate reason to use 'expect' Perl will happily
talk telnet and ftp on its own.

Rob


well- namely that it works- i honestly don't know /how/ to get perl
to talk telnet *laugh* i guess the proper module would help.

i will investigate this possibility and see what happens :)

off to CPAN.....

ttfn,
willy

Rob Dixon
William M West wrote:
QUOTE

Is there any separate reason to use 'expect'? Perl will happily
talk telnet and ftp on its own.

Rob


well- namely that it works- i honestly don't know /how/ to get perl
to talk telnet *laugh*  i guess the proper module would help.

i will investigate this possibility and see what happens :)

You need Net::FTP and/or Net::Telnet.

In my experience it's always better to solve a problem natively
in Perl than to shell out to another application. That way
everything that happens is under your control and the bugs
will be easier to find. I'm certain that, unless there's
a separate reason to use 'expect', you'd be better off
using straight Perl than getting Perl to run ftp (or telnet)
via expect.

Come back if you need any help with the modules.

Rob

Wc -Sx- Jones
West, William M wrote:
QUOTE
Is there any separate reason to use 'expect' Perl will happily
talk telnet and ftp on its own.


expect.nist.gov expect ???

A fine, if under valued, development system.

QUOTE

well- namely that it works- i honestly don't know /how/ to get perl
to talk telnet *laugh*  i guess the proper module would help.

i will investigate this possibility and see what happens :)


Off to get the correct 'starter' modules. Try -

http://www.cpan.org/authors/id/S/SN/SNEEX/smtp_Scanner_v3


A small test bed I have been messing with these past few days.

#__Sx___________________________________________________________________
$_=qq|for(ref bless{},chasecreek'systemhouse){s-:+-$"-g&&print$_.$/}|;
while(m|[WC -Sx - Jones @ http://inSecurity.org/]|ig) { print join(" ",
map { defined $_ ? $_ : ""} $`, $&, $', $+), "n";}
__DATA__
('> iudicium ferat
// Have Computer -
v_/_ Will Hack...

|/ ____ |/
"@'/ .. `@"
/_| __/ |_
__U_/

Smith Jeff D
Thanks for the feedback--maybe I screwed up but what happens for me is that
the ordered array (1) only lists the keys, not the array values the hash key
points to and (2) I still don't get an ordered list of the keys that are put
in the "ordered" array--it comes out un-ordered.

I took your line and just added a for loop/print for the ordered array and
got "red,yellow,blue, orange, violet, green" only as the result.

I must be dense but using just a Keys expression can't return the values,
can it??--wouldn't it be better to do a while/each and get both key and
value for the HofA somehow??

-----Original Message-----
From: James Edward Gray II [mailto:[Email Removed]]
Sent: Wednesday, February 18, 2004 6:49 PM
To: Smith Jeff D
Cc: [Email Removed]
Subject: Re: An Old Question on Sorting Hash of Arrays by Array element and
th en by key


On Feb 18, 2004, at 1:58 PM, Smith Jeff D wrote:

QUOTE
I am trying to sort a hash of arrays that is similar to the example
below.
I have a hash of arrays that I want to sort, first by the first
element of
the array, then by the key to the hash and don't care about other
elements
of the array (for sorting and without regard to case.

%HofA = (orange=>['ZZZ', 'ANDY'],
    red=>['AAA', 'AL'],
    blue=>['mmm','Betty'],
    yellow=>['aaa', 'ZEUS'],
    green=>['DDD','Mary Joe']
    violet=>['MMM','Hugo']
    );

my @ordered_keys = sort { $HofA{$a}[0] cmp $HofA{$b}[1]
||
$a cmp $b } keys %HofA;

I believe that will do it. See if that gets you going.

James

Wc -Sx- Jones
Smith Jeff D wrote:
QUOTE
Thanks for the feedback--maybe I screwed up but what happens for me is that
the ordered array (1) only lists the keys, not the array values the hash key
points to and (2) I still don't get an ordered list of the keys that are put
in the "ordered" array--it comes out un-ordered.


Perl uses its own internal storage method for Hashes.
There is no guarantee of order unless you sort it and
save the results yourself.


QUOTE
I took your line and just added a for loop/print for the ordered array and
got "red,yellow,blue, orange, violet, green" only  as the result.

I must be dense but using just a Keys expression can't return the values,
can it??--wouldn't it be better to do a while/each and get both key and
value for the HofA somehow??


You need the key value to get the hash data.
Keys, in of themselves, are of little value.



QUOTE
-----Original Message-----


QUOTE
%HofA = (orange=>['ZZZ', 'ANDY'],
    red=>['AAA', 'AL'],
    blue=>['mmm','Betty'],
    yellow=>['aaa', 'ZEUS'],
    green=>['DDD','Mary Joe']
    violet=>['MMM','Hugo']
    );


QUOTE
my @ordered_keys = sort { $HofA{$a}[0] cmp $HofA{$b}[1]
        ||
        $a cmp $b } keys %HofA;


James Edward Gray II
On Feb 19, 2004, at 10:25 AM, Smith Jeff D wrote:

QUOTE
Thanks for the feedback--maybe I screwed up but what happens for me is
that
the ordered array (1) only lists the keys, not the array values the
hash key
points to and (2) I still don't get an ordered list of the keys that
are put
in the "ordered" array--it comes out un-ordered.

First, did you catch the later post that pointed out my mistake?
Here's the corrected code:

my @ordered_keys = sort { $HofA{$a}[0] cmp $HofA{$b}[0]
||
$a cmp $b } keys %HofA;


QUOTE
I took your line and just added a for loop/print for the ordered array
and
got "red,yellow,blue, orange, violet, green" only  as the result.

I must be dense but using just a Keys expression can't return the
values,
can it??--wouldn't it be better to do a while/each and get both key and
value for the HofA somehow??

Yes, I only ordered the keys. Sorry if I didn't make that clear.
That's all you need though, right? ;)

foreach (@ordered_keys) {
print "$HofA{$_}[0], $_, $HofA{$_}[1]n";
}

Hope that helps.

James

Wc -Sx- Jones
%HofA = (orange=>['ZZZ', 'ANDY'],
red=>['AAA', 'AL'],
blue=>['mmm','Betty'],
yellow=>['aaa', 'ZEUS'],
green=>['DDD','Mary Joe'],
violet=>['MMM','Hugo']
);

my @ordered_keys = sort { $HofA{$a}[0] cmp $HofA{$b}[1]
||
$a cmp $b } keys %HofA;


foreach $value (@ordered_keys) { print "$valuen"; }


FYI:

Perl Cookbook - 5.4. Traversing a Hash

Problem -- You want to perform an action on each entry (i.e., each
key-value pair) in a hash.

Solution -- Use each with a while loop:

while(($key, $value) = each(%HASH)) {
# do something with $key and $value
}

Or use keys with a foreach loop, unless the hash is potentially very large:

foreach $key (keys %HASH) {
$value = $HASH{$key};
# do something with $key and $value
}

-Sx-

Rob Dixon
Jeff Smith wrote:
QUOTE

Thanks for the feedback--maybe I screwed up but what happens for me is that
the ordered array (1) only lists the keys, not the array values the hash key
points to and (2) I still don't get an ordered list of the keys that are put
in the "ordered" array--it comes out un-ordered.

I took your line and just added a for loop/print for the ordered array and
got "red,yellow,blue, orange, violet, green" only  as the result.

I must be dense but using just a Keys expression can't return the values,
can it??--wouldn't it be better to do a while/each and get both key and
value for the HofA somehow??

-----Original Message-----
From: James Edward Gray II [mailto:[Email Removed]]
Sent: Wednesday, February 18, 2004 6:49 PM
To: Smith Jeff D
Cc: [Email Removed]
Subject: Re: An Old Question on Sorting Hash of Arrays by Array element and
th en by key


On Feb 18, 2004, at 1:58 PM, Smith Jeff D wrote:

I am trying to sort a hash of arrays that is similar to the example
below.
I have a hash of arrays that I want to sort, first by the first
element of
the array, then by the key to the hash and don't care about other
elements
of the array (for sorting and without regard to case.

%HofA = (orange=>['ZZZ', 'ANDY'],
red=>['AAA', 'AL'],
blue=>['mmm','Betty'],
yellow=>['aaa', 'ZEUS'],
green=>['DDD','Mary Joe']
violet=>['MMM','Hugo']
);

my @ordered_keys = sort { $HofA{$a}[0] cmp $HofA{$b}[1]
||
$a cmp $b } keys %HofA;

I believe that will do it.  See if that gets you going.

Hi Jeff.

James was offering you a starting place. The only way to order
a hash is to order its keys, hence 'my @ordered_keys'. What you
do with this sorted list depends on what you want. For instance

foreach (@ordered_keys) {
printf "%s => %sn", $_, $HofA{$_};
}

Also, I wasn't sure whether, by

QUOTE
(for sorting and without regard to case)

you meant that the string comparisons for the sort should be
case-insensitive. If so, then your sort should look like

my @ordered_keys = sort {
my ($aa, $bb) = lc $a, lc $b;
$HofA{$aa}[0] cmp $HofA{$bb}[0] or $aa cmp $bb
} keys %HofA;

HTH,

Rob

Smith Jeff D
I really need to order both the keys and one of the elements in the array
stored as a value in the hash, preferably sort first on the first element of
the array (my real application has four elements but the snippet I'm testing
with has a two-element array) and then sort secondly on the key.

So if the reference to the array stored as the hash value is called
"$value", then there are two elements, $$value[0] and $$value[1], that it
references, right?
I want to sort first on $$value[0], then on the key for HofA, and to then be
able to print a report that lists all the data I need, including the two
sorted values plus also the other, non-sorted values stored in the HofA
array. That way the output is easier to read and summarize for the user.

Does this make sense? I know how to sort by keys but sort first by the
value element and then by keys is my block...

-----Original Message-----
From: James Edward Gray II [mailto:[Email Removed]]
Sent: Thursday, February 19, 2004 11:37 AM
To: Smith Jeff D
Cc: '[Email Removed]'
Subject: Re: An Old Question on Sorting Hash of Arrays by Array element an d
th en by key


On Feb 19, 2004, at 10:25 AM, Smith Jeff D wrote:

QUOTE
Thanks for the feedback--maybe I screwed up but what happens for me is
that
the ordered array (1) only lists the keys, not the array values the
hash key
points to and (2) I still don't get an ordered list of the keys that
are put
in the "ordered" array--it comes out un-ordered.

First, did you catch the later post that pointed out my mistake?
Here's the corrected code:

my @ordered_keys = sort { $HofA{$a}[0] cmp $HofA{$b}[0]
||
$a cmp $b } keys %HofA;


QUOTE
I took your line and just added a for loop/print for the ordered array
and
got "red,yellow,blue, orange, violet, green" only  as the result.

I must be dense but using just a Keys expression can't return the
values,
can it??--wouldn't it be better to do a while/each and get both key and
value for the HofA somehow??

Yes, I only ordered the keys. Sorry if I didn't make that clear.
That's all you need though, right? ;)

foreach (@ordered_keys) {
print "$HofA{$_}[0], $_, $HofA{$_}[1]n";
}

Hope that helps.

James

The Advantage is you get to use the switch statement. The disadvantage is
your code will run extraordinarily slow because the Switch module uses a run time
Filter. In short: isn't an is-elsif-else statement enough??

In a message dated 2/19/2004 4:40:56 PM Eastern Standard Time, [Email Removed]
writes:
I don't know the advantages/disadvantages of using it.



-Will
-----------------------------------
Handy Yet Cryptic Code.
Just to Look Cool to Look at and try to decipher without running it.

Windows
perl -e "printf qq.%3ix20x3dx20x27%cx27x09.,$_,$_ for 0x20..0x7e"

Unix
perl -e 'printf qq.%3ix20x3dx20x27%cx27%7c.,$_,$_,0x20 for 0x20..0x7e'

Wc -Sx- Jones
Smith Jeff D wrote:
QUOTE
I really need to order both the keys and one of the elements in the array
stored as a value in the hash, preferably sort first on the first element of
the array (my real application has four elements but the snippet I'm testing
with has a two-element array) and then sort secondly on the key.


Research:

Tie::IxHash

Rob Dixon
Jeff Smith wrote:
QUOTE

I really need to order both the keys and one of the elements in the array
stored as a value in the hash, preferably sort first on the first element of
the array (my real application has four elements but the snippet I'm testing
with has a two-element array) and then sort secondly on the key.

So if the reference to the array stored as the hash value is called
"$value", then there are two elements, $$value[0] and $$value[1], that it
references, right?
I want to sort first on $$value[0], then on the key for HofA, and to then be
able to print a report that lists all the data I need, including the two
sorted values plus also the other, non-sorted values stored in the HofA
array.  That way the output is easier to read and summarize for the user.

Does this make sense?  I know how to sort by keys but sort first by the
value element and then by keys is my block...

Hi Jeff.

I was hoping James would be around, but, briefly:

A hash element is a key/value pair, but it is accessed uniquely
by its key. You can sort the elements on any function of the key,
the value, or both; but the result will be an ordered list of keys.

Does that help? I hope so!

Rob

This:
if ($op == 0) {}
elsif ($op == 1) {}
elsif ($op == 2) {}
elsif ($op == 3) {}
elsif ($op == 4) {}
elsif ($op == 5) {}

is faster than this:

use Switch;
switch ($op) {
case 0 { last }
case 1 { last }
case 2 { last }
case 3 { last }
case 4 { last }
case 5 { last }
}

By a noticably amount.


In a message dated 2/19/2004 5:47:47 PM Eastern Standard Time,
[Email Removed] writes:
Switch allows coding that runs no more slowly than 'if'



-Will
-----------------------------------
Handy Yet Cryptic Code.
Just to Look Cool to Look at and try to decipher without running it.

Windows
perl -e "printf qq.%3ix20x3dx20x27%cx27x09.,$_,$_ for 0x20..0x7e"

Unix
perl -e 'printf qq.%3ix20x3dx20x27%cx27%7c.,$_,$_,0x20 for 0x20..0x7e'

I just benchmarked Filter, and it was worse than 100 times slower


In a message dated 2/19/2004 6:53:56 PM Eastern Standard Time,
[Email Removed] writes:
last time i benchmark a source filter, it's about 100 times slower. has that
change since v5.8?



-Will
-----------------------------------
Handy Yet Cryptic Code.
Just to Look Cool to Look at and try to decipher without running it.

Windows
perl -e "printf qq.%3ix20x3dx20x27%cx27x09.,$_,$_ for 0x20..0x7e"

Unix
perl -e 'printf qq.%3ix20x3dx20x27%cx27%7c.,$_,$_,0x20 for 0x20..0x7e'

James Edward Gray II
On Feb 19, 2004, at 10:48 AM, Smith Jeff D wrote:

QUOTE
I really need to order both the keys and one of the elements in the
array
stored as a value in the hash, preferably sort first on the first
element of
the array (my real application has four elements but the snippet I'm
testing
with has a two-element array) and then sort secondly on the key.

Are you playing with the code I'm posting? :P

I know how you would like it sorted. I did that. (Actually, I believe
I did miss the case insensitive part, but Rob has already fixed that.)

A Perl hash is an unordered structure. However, if we put the keys in
the order we want and then use those to access the values, we're good
to go. I posted a loop showing this in my last message.

I ordered the keys based on the first value and the key itself, just
like you said. In order to avoid more confusion though, here's a proof
of concept:

#!/usr/bin/perl

use strict;
use warnings;

# create some data
my %HofA = ( orange => ['ZZZ', 'ANDY'],
red => ['AAA', 'AL'],
blue => ['mmm','Betty'],
yellow => ['aaa', 'ZEUS'],
green => ['DDD','Mary Joe'],
violet => ['MMM','Hugo'] );

# sort it
my @ordered_keys = sort { lc($HofA{$a}[0]) cmp lc($HofA{$b}[0])
||
lc($a) cmp lc($b) } keys %HofA;

# print it
foreach (@ordered_keys) {
print "$HofA{$_}[0], $_, $HofA{$_}[1]n";
}

__END__

You'll notice that the above is really just a summary of this thread.
When I run it, I get:

AAA, red, AL
aaa, yellow, ZEUS
DDD, green, Mary Joe
mmm, blue, Betty
MMM, violet, Hugo
ZZZ, orange, ANDY

Which is the output you requested in your original message.

Hope that helps.

James

Smith Jeff D
Thanks, I must have missed it--I'll be getting back to it tomorrow morning
to see what I missed in the original response. I thought I had run as
printed below.

Anyway, thank for the help...I think I may have braced when I should have
paren'd... It so simple when someone else does it first.... I was trying to
nest the sorts rather
than logically or'ing them together and it wasn't working.

-----Original Message-----
From: James Edward Gray II [mailto:[Email Removed]]
Sent: Thursday, February 19, 2004 3:12 PM
To: Smith Jeff D
Cc: '[Email Removed]'
Subject: Re: An Old Question on Sorting Hash of Arrays by Array element an d
th en by key


On Feb 19, 2004, at 10:48 AM, Smith Jeff D wrote:

QUOTE
I really need to order both the keys and one of the elements in the
array
stored as a value in the hash, preferably sort first on the first
element of
the array (my real application has four elements but the snippet I'm
testing
with has a two-element array) and then sort secondly on the key.

Are you playing with the code I'm posting? :P

I know how you would like it sorted. I did that. (Actually, I believe
I did miss the case insensitive part, but Rob has already fixed that.)

A Perl hash is an unordered structure. However, if we put the keys in
the order we want and then use those to access the values, we're good
to go. I posted a loop showing this in my last message.

I ordered the keys based on the first value and the key itself, just
like you said. In order to avoid more confusion though, here's a proof
of concept:

#!/usr/bin/perl

use strict;
use warnings;

# create some data
my %HofA = ( orange => ['ZZZ', 'ANDY'],
red => ['AAA', 'AL'],
blue => ['mmm','Betty'],
yellow => ['aaa', 'ZEUS'],
green => ['DDD','Mary Joe'],
violet => ['MMM','Hugo'] );

# sort it
my @ordered_keys = sort { lc($HofA{$a}[0]) cmp lc($HofA{$b}[0])
||
lc($a) cmp lc($b) } keys
%HofA;

# print it
foreach (@ordered_keys) {
print "$HofA{$_}[0], $_, $HofA{$_}[1]n";
}

__END__

You'll notice that the above is really just a summary of this thread.
When I run it, I get:

AAA, red, AL
aaa, yellow, ZEUS
DDD, green, Mary Joe
mmm, blue, Betty
MMM, violet, Hugo
ZZZ, orange, ANDY

Which is the output you requested in your original message.

Hope that helps.

James

James Edward Gray II
On Feb 19, 2004, at 3:20 PM, Smith Jeff D wrote:

QUOTE
Thanks, I must have missed it--I'll be getting back to it tomorrow
morning
to see what I missed in the original response.  I thought I had run as
printed below.

No worries. You probably did run my original response, which was
flawed. Mark gave a fix for it shortly after I posted it. Then Rob
pointed out that it was ignoring your case insensitive request today.
All their suggestions are included below.

The Moral: Don't use the original message. Use this one.

QUOTE
Anyway, thank for the help...I think I may have braced when I should
have
paren'd... It so simple when someone else does it first.... I was
trying to
nest the sorts rather
than logically or'ing them together  and it wasn't working.

Oring sort() conditions is a common Perl idiom. Now you know. ;)

James

QUOTE
#!/usr/bin/perl

use strict;
use warnings;

# create some data
my %HofA = ( orange => ['ZZZ', 'ANDY'],
    red => ['AAA', 'AL'],
    blue => ['mmm','Betty'],
    yellow => ['aaa', 'ZEUS'],
    green => ['DDD','Mary Joe'],
    violet => ['MMM','Hugo'] );

# sort it
my @ordered_keys = sort { lc($HofA{$a}[0]) cmp lc($HofA{$b}[0])
        ||
        lc($a) cmp lc($b) } keys
%HofA;

# print it
foreach (@ordered_keys) {
print "$HofA{$_}[0], $_, $HofA{$_}[1]n";
}

__END__

You'll notice that the above is really just a summary of this thread.
When I run it, I get:

AAA, red, AL
aaa, yellow, ZEUS
DDD, green, Mary Joe
mmm, blue, Betty
MMM, violet, Hugo
ZZZ, orange, ANDY

Which is the output you requested in your original message.

Hope that helps.

James


Rob Dixon
<[Email Removed]> wrote:
QUOTE

The Advantage is you get to use the switch statement. The disadvantage is
your code will run extraordinarily slow because the Switch module uses a run time
Filter. In short: isn't an is-elsif-else statement enough??

Are you saying that 'if-elsif-else' is a compile-time filter? It isn't.
So I guess 'if-elsif-else' runs extraordinarily slow too.

AFAIK Switch allows coding that runs no more slowly than 'if', and lets
you express some solutions much more neatly.

Rob

Rob Dixon
James Edward Gray II wrote:
QUOTE

The Moral:  Don't use the original message.  Use this one.

Far more reliable is: "Don't do what I say, do what I mean." :)

Rob

Paul Johnson
On Thu, Feb 19, 2004 at 10:46:33PM -0000, Rob Dixon wrote:
QUOTE
<[Email Removed]> wrote:

The Advantage is you get to use the switch statement. The disadvantage is
your code will run extraordinarily slow because the Switch module uses a run time
Filter. In short: isn't an is-elsif-else statement enough??

Are you saying that 'if-elsif-else' is a compile-time filter? It isn't.
So I guess 'if-elsif-else' runs extraordinarily slow too.

AFAIK Switch allows coding that runs no more slowly than 'if', and lets
you express some solutions much more neatly.

The problem is that Switch.pm is a source filter, which is effectively
an extra level of processing before compilation.

Personally, I wouldn't use a source filter for anything I cared about.

The docs for Switch state:

There are undoubtedly serious bugs lurking somewhere in code this
funky :-)

and also

Due to the heuristic nature of Switch.pm's source parsing, the
presence of regexes specified with raw "?...?" delimiters may cause
mysterious errors.

--
Paul Johnson - [Email Removed]
http://www.pjcj.net

James Edward Gray II
On Feb 19, 2004, at 5:21 PM, Paul Johnson wrote:

QUOTE
The problem is that Switch.pm is a source filter, which is effectively
an extra level of processing before compilation.

Personally, I wouldn't use a source filter for anything I cared about.

The docs for Switch state:

There are undoubtedly serious bugs lurking somewhere in code this
funky :-)

and also

Due to the heuristic nature of Switch.pm's source parsing, the
presence of regexes specified with raw "?...?" delimiters may cause
mysterious errors.

I haven't spent the time to verify the exact cause, but I think I have
had trouble with the compiler giving me incorrect line numbers for
errors, when I've used Switch. I don't know if it was an isolated
couple of cases or if this a common problem with the module, but it has
turned me off to it.

I am looking forward to the native switch in Perl 6 though.

James

No. $self you'll usually see in a lot of Object Oriented applications. When a
subroutine is called using the -> operator ($mw = MainWindow->new, for
example) the first arguement passed to that subroutine is the name of the
package/class. So, $self is usually used to display this. So, a common constructor is

sub new {
my ($self, %args) = @_;
return bless %args, $self;
}

Look into perldoc perlboot, perldoc perltoot, perldoc -f bless, etc. Like the
name "new" for a constructor, you can name it anything you want, but many
choose to name it $self, because in a constructor it is literally the name of
your own package, and otherwhere it is a hash blessed to your own package. Hope
that helps.


In a message dated 2/19/2004 11:24:11 PM Eastern Standard Time,
[Email Removed] writes:
is $self a special scalar?



-Will
-----------------------------------
Handy Yet Cryptic Code.
Just to Look Cool to Look at and try to decipher without running it.

Windows
perl -e "printf qq.%3ix20x3dx20x27%cx27x09.,$_,$_ for 0x20..0x7e"

Unix
perl -e 'printf qq.%3ix20x3dx20x27%cx27%7c.,$_,$_,0x20 for 0x20..0x7e'

David
Rob Dixon wrote:

QUOTE
<[Email Removed]> wrote:

The Advantage is you get to use the switch statement. The disadvantage is
your code will run extraordinarily slow because the Switch module uses a
run time Filter. In short: isn't an is-elsif-else statement enough??

Are you saying that 'if-elsif-else' is a compile-time filter? It isn't.
So I guess 'if-elsif-else' runs extraordinarily slow too.

AFAIK Switch allows coding that runs no more slowly than 'if', and lets
you express some solutions much more neatly.

Rob

last time i benchmark a source filter, it's about 100 times slower. has that
change since v5.8?

david
--
sub'_{print"@_ ";* = * __ , & }
sub'__{print"@_ ";* = * ___ , & }
sub'___{print"@_ ";* = * ____ , & }
sub'____{print"@_,n"}&{_+Just}(another)->(Perl)->(Hacker)

John W. Krahn
Rob Dixon wrote:
QUOTE

James Edward Gray II wrote:

The Moral:  Don't use the original message.  Use this one.

Far more reliable is: "Don't do what I say, do what I mean." :)

After all, Perl is a DWIM language. :-)


John
--
use Perl;
program
fulfillment

znur tatan
I am still dealing with the same problem.
Rob has suggested me a good solution for macthing consecutive patterns like
H K D but not more looser ones like for K[ED]{3,5}? L.{3}A
andn my poor perl knowledge doesn't help me to generalize it: /

In the below link I came across

http://www.perl.com/pub/a/2002/06/04/apo5.html?page=8

$_ = "abracadabra";
@all = m:any /a.*?a/;
produces:

abra abraca abracada abracadabra aca acada acadabra ada adabra abra
Is there a version available that supports this structure?
Or are there any creative ideas of Perl wizards?
Thanks in advance
cheers
oznur
znur Tastan wrote:
QUOTE

Your suggestion was quite helpful but I got stuck when I try to modify it for
general purpose. May be you will have an idea and want to help.

The foreach $n( 1...length sequence) solves the combinatorial problem but the
combinations can happen in second or third cases

so I need to repeat the foreach loop
foreach $m(1..length $sequence){
foreach $n(1..length sequence){
foreach $f(1..length sequence){
foreach $r(1..length sequence){
next unless $sequence =~ /> (.{$m})H(,{$n})K(.{$f})D(.{$R})/;

But the string was an example an in general case there can be n letters of just
like H K and D so I need n+1 for each loops repeated and I don't have any idea
how to write a foreach loop that can should be repeated n times.

Hi znur.

Yes, I did suspect my previous answer wouldn't handle the general case, but
I hoped it may be good enough. I'm sure it's not possible using a simple regex.

I've written subroutine split_list() below, which takes a string of characters to
split on and a string to split, a little like the split() built-in. It finds all
the ways to split the string at each of the characters in sequence.

It's a recursive subroutine to make it neater. It works by finding a way to
split on the first character in the list and then calling itself to split
the right-hand half on the remaining characters.

The return value is an array of all the possibilities with the split
characters replaced with hyphens.

Post to the list if you need any help with any part of it.

(Thanks, this was an interesting little problem!)

Cheers,

Rob



use strict;
use warnings;

sub split_list {

my ($list, $string) = @_;

return ($string) unless $list =~ s/(.)//;
my $split = $1;

my @splits;

while ( $string =~ /$split/g ) {

my $pos = pos $string;
my $left = substr $string, 0, $pos - 1;
my $right = substr $string, $pos;

push @splits, map "$left-$_", split_list($list, $right);
}

return @splits;
}

my @ret = split_list ('HKD', 'xHxxHyyKzDt');

print map "$_n", @ret;


**OUTPUT

x-xxHyy-z-t
xHxx-yy-z-t




--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Wc -Sx- Jones
znur Tatan wrote:

QUOTE
In the below link I came across

http://www.perl.com/pub/a/2002/06/04/apo5.html?page=8

$_ = "abracadabra";
@all = m:any /a.*?a/;
produces:

abra abraca abracada abracadabra aca acada acadabra ada adabra abra


That only shows substrings within string that begin with start letter.

Visually -
abracadabra becomes:
abra
abraca
abracada
abracadabra
....aca
....acada
....acadabra
......ada
......adabra
........abra

So, you see what you get as a substring, with 'a' as the starting
letter. Is that what you wanted?

What Rob wrote splits combinations of class set from class group
strings. Two totally different things.

I guess I am confused.
-Sx-

znur tatan
You are rigth to confuse beacuse I couldn't find the mail I wrote at the
beginning that defines the problem.Sorry..
My problem was this
I have a sets of patterns
and a string
and I know in which order these patterns are supposed to exists
What I try to do was to extract the substrings when the patterns match to
the string but all the matches including the overlapping ones.
The simplified version is the one that Rob solves. That is when the sets of
pattern is H K D (H is the first pattern)
So in the link I give as far as i understood it says

$_ = "abracadabra";
@all = m:any /a.*?a/;
can find all matches of substrings starting with a and ending with a.
So I thought putting willcard of (.*) in between my sets of patterns can
achieve the solution am I wrong?
Thanks
oznur

----- Original Message -----
From: "WC -Sx- Jones" <[Email Removed]>
Cc: "Perl Lists" <[Email Removed]>
Sent: Friday, February 20, 2004 11:11 AM
Subject: Re: all matches of a regex-continued


QUOTE
znur Tatan wrote:

In the below link I came across

http://www.perl.com/pub/a/2002/06/04/apo5.html?page=8

$_ = "abracadabra";
@all = m:any /a.*?a/;
produces:

abra abraca abracada abracadabra aca acada acadabra ada adabra abra


That only shows substrings within string that begin with start letter.

Visually -
abracadabra becomes:
abra
abraca
abracada
abracadabra
...aca
...acada
...acadabra
.....ada
.....adabra
.......abra

So, you see what you get as a substring, with 'a' as the starting
letter. Is that what you wanted?

What Rob wrote splits combinations of class set from class group
strings.  Two totally different things.

I guess I am confused.
-Sx-

--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response


Randy W. Sims
On 02/20/04 04:31, znur Tatan wrote:
QUOTE
You are rigth to confuse beacuse I couldn't find the mail I wrote at the
beginning that defines the problem.Sorry..
My problem was this
I have a sets of patterns
and a string
and I know in which order these patterns are supposed to exists
What I try to do was to extract the substrings when the patterns match to
the string but all the matches including the overlapping ones.
The simplified version is the one that Rob solves. That is when the sets of
pattern is H K D (H is the first pattern)
So in the link I give as far as i understood it says

If I understand the problem, what you want to do is vary the greediness
of the match. One way to do that is to generate the regexs. Try this:

#!/usr/bin/perl

use strict;
use warnings;


my $seq = 'xHxxHyyKzDt';

my @pat = ( '(.+)', '(.+?)' );

my %uniq;

for my $a (0..1) {
for my $b (0..1) {
for my $c (0..1) {
for my $d (0..1) {
my $pat = '^' .
$pat[$a] . 'H' .
$pat[$b] . 'K' .
$pat[$c] . 'D' .
$pat[$d] . '$' ;

$uniq{"$1 $2 $3 $4"}++ if $seq =~ /$pat/;
}
}
}
}

print join "n", keys %uniq;

znur tatan
You are right but the sets of patterns can include any number of patterns
which will mean a variable number of foreach loop and I don't know how to
achieve.
thanks
oznur




QUOTE
On 02/20/04 04:31, znur Tatan wrote:
You are rigth to confuse beacuse I couldn't find the mail I wrote at the
beginning that defines the problem.Sorry..
My problem was this
I have a sets of patterns
and a string
and I know in which order these patterns are supposed to exists
What I try to do was to extract the substrings when the patterns match
to
the string but all the matches including the overlapping ones.
The simplified version is the one that Rob solves. That is when the sets
of
pattern is H K D (H is the first pattern)
So in the link I give as far as i understood it says

If I understand the problem, what you want to do is vary the greediness
of the match. One way to do that is to generate the regexs. Try this:

#!/usr/bin/perl

use strict;
use warnings;


my $seq = 'xHxxHyyKzDt';

my @pat = ( '(.+)', '(.+?)' );

my %uniq;

for my $a (0..1) {
for my $b (0..1) {
for my $c (0..1) {
for my $d (0..1) {
my $pat =    '^' .
$pat[$a] . 'H' .
$pat[$b] . 'K' .
$pat[$c] . 'D' .
$pat[$d] . '$' ;

$uniq{"$1 $2 $3 $4"}++ if $seq =~ /$pat/;
}
}
}
}

print join "n", keys %uniq;

--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response


znur tatan
----- Original Message -----
From: "David le Blanc" <[Email Removed]>
To: "znur Tatan" <[Email Removed]>; "Perl Lists"
<[Email Removed]>
Sent: Friday, February 20, 2004 1:35 PM
Subject: RE: all matches of a regex-continued


QUOTE

In the below link I came across

http://www.perl.com/pub/a/2002/06/04/apo5.html?page=8

$_ = "abracadabra";
@all = m:any /a.*?a/;
produces:



What version of perl are we talking about here?

5.8 or 6 maybe?

I don't know actually what version supports it?

David Le Blanc
QUOTE

In the below link I came across

http://www.perl.com/pub/a/2002/06/04/apo5.html?page=8

$_ = "abracadabra";
@all = m:any /a.*?a/;
produces:



What version of perl are we talking about here?

5.8 or 6 maybe?

Randy W. Sims
On 02/20/04 06:06, znur Tatan wrote:
QUOTE
You are right but the sets of patterns can include any number of patterns
which will mean a variable number of foreach loop and I don't know how to
achieve.
thanks
oznur


Ok, welcome to the wonderful world of dynamic code generation. The code
below is ugly as sin, but see if it does what you want.

#!/usr/bin/perl

use strict;
use warnings;


my $seq = 'xHxxHyyKzDt';

my @any = ( '(.+)', '(.+?)' );

my @vars = ('a'..'z');
my @keys = qw( H K D );


my $cmd;

my $index = 0;
for my $key (@keys) {
$cmd .= "for my $$vars[$index++] (0..1) {n";
}
$cmd .= "for my $$vars[$index++] (0..1) {n";


$index = 0;
$cmd .= "my $any = '^' .n";
for my $key (@keys) {
$cmd .= "$any[$$vars[$index]] . '$keys[$index++]' .n";
}
$cmd .= "$any[$$vars[$index]] . '$';n";

$cmd .= "$uniq{"";
$index = 0;
for my $i (1..@keys+1) {
$cmd .= "$$i ";
}
chop $cmd; #remove the last space we added
$cmd .= ""}++ if $seq =~ /$any/;n";

$index = 0;
for (0..@keys) {
$cmd .= "}n";
}

#print $cmd;
my %uniq;
eval $cmd;

print join("n", keys(%uniq)) . "n";

Randy W. Sims
On 02/20/04 07:03, znur Tatan wrote:
QUOTE
I didn't know something exists like this, thanks that will be very helpful
but still
does this solve the problem of regular expression patterns istead of keys
just as I mentioned in the first mail.
" I am still dealing with the same problem.
Rob has suggested me a good solution for macthing consecutive patterns like
H K D  but not more looser ones like for K[ED]{3,5}?  L.{3}A
andn my poor perl knowledge doesn't help me to generalize it: /"

The @keys array can hold regexs as well as strings:

my @keys = ('H', qr/K[ED]?/, qr/D{1,2}/);

Regards,
Randy.

znur tatan
----- Original Message -----
From: "Randy W. Sims" <[Email Removed]>
To: "znur Tatan" <[Email Removed]>
Cc: <[Email Removed]>
Sent: Friday, February 20, 2004 2:11 PM
Subject: Re: all matches of a regex-continued


QUOTE
On 02/20/04 07:03, znur Tatan wrote:
I didn't know something exists like this, thanks that will be very
helpful
but still
does this solve the problem of regular expression patterns istead of
keys
just as I mentioned in the first mail.
" I am still dealing with the same problem.
Rob has suggested me a good solution for macthing consecutive patterns
like
H K D  but not more looser ones like for K[ED]{3,5}?  L.{3}A
andn my poor perl knowledge doesn't help me to generalize it: /"

The @keys array can hold regexs as well as strings:

my @keys = ('H', qr/K[ED]?/, qr/D{1,2}/);

Regards,
Randy.


THANKS A LOT :)
oznur
QUOTE
--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response


Rob Dixon
znur tastan wrote:
QUOTE

I am still dealing with the same problem.
Rob has suggested me a good solution for macthing consecutive patterns like
H K D  but not more looser ones like for K[ED]{3,5}?  L.{3}A
andn my poor perl knowledge doesn't help me to generalize it: /

Are you just saying that the patterns that you want to split on
can be regexes themselves instead of plain strings? I think my solution will
do that fine, but you need to give us an example. All the solutions
you've been offered so far are based on your original example. You said:

znur tastan wrote:
QUOTE

I have been trying to solve a problem which is about to drive me crazy.
May be some one know the answer(hopefully:)

I want to get all macthes of a pattern in a string including the overlaping ones.
For example
the string is "xHxxHyyKzDt"
and the pattern is /^(.*)H(.*)K(.*)D(.*)$/

so in one round of match $1=x  $2=xxHyy $3=z $4=t
in another                      $1=xHxx $2=yy $3=x $4=t


while ($sequence=~/$pattern/g )
doesn't work I think becaue the matches are overlapping

while ($sequence=~/(?=$pattern)/g )

which has been solved. Can you explain the full problem a little better?

QUOTE
In the below link I came across

http://www.perl.com/pub/a/2002/06/04/apo5.html?page=8

$_ = "abracadabra";
@all = m:any /a.*?a/;
produces:

abra abraca abracada abracadabra aca acada acadabra ada adabra abra
Is there a version available that supports this structure?
Or are there any creative ideas of Perl wizards?

This is a proposal for Perl 6.0. The latest development source is at 5.9
and the latest stable release is 5.8.3.

Rob

znur tatan
----- Original Message -----
From: "Rob Dixon" <[Email Removed]>
To: <[Email Removed]>
Sent: Friday, February 20, 2004 4:17 PM
Subject: Re: all matches of a regex-continued


QUOTE
znur tastan wrote:

I am still dealing with the same problem.
Rob has suggested me a good solution for macthing consecutive patterns
like
H K D  but not more looser ones like for K[ED]{3,5}?  L.{3}A
andn my poor perl knowledge doesn't help me to generalize it: /

Are you just saying that the patterns that you want to split on
can be regexes themselves instead of plain strings? I think my solution
will
do that fine, but you need to give us an example. All the solutions
you've been offered so far are based on your original example. You said:

I think there has been a discontuinty in replies (the mails didn't contain
everything) and I couldn't be very explanatory
(:( sorry)
you are right the patterns can ve regexes themselves. I just couldn't figure
out how to modify your code.


QUOTE
znur tastan wrote:

I have been trying to solve a problem which is about to drive me crazy.
May be some one know the answer(hopefully:)

I want to get all macthes of a pattern in a string including the
overlaping ones.
For example
the string is "xHxxHyyKzDt"
and the pattern is /^(.*)H(.*)K(.*)D(.*)$/

so in one round of match $1=x  $2=xxHyy $3=z $4=t
in another                      $1=xHxx $2=yy $3=x $4=t


while ($sequence=~/$pattern/g )
doesn't work I think becaue the matches are overlapping

while ($sequence=~/(?=$pattern)/g )

which has been solved. Can you explain the full problem a little better?

My problem was this
I have a sets of patterns
and a string
and I know in which order these patterns are supposed to exists
What I want is to extract all the substrings when the patterns match to
the string.

QUOTE

In the below link I came across

http://www.perl.com/pub/a/2002/06/04/apo5.html?page=8

$_ = "abracadabra";
@all = m:any /a.*?a/;
produces:

abra abraca abracada abracadabra aca acada acadabra ada adabra abra
Is there a version available that supports this structure?
Or are there any creative ideas of Perl wizards?

This is a proposal for Perl 6.0. The latest development source is at 5.9
and the latest stable release is 5.8.3.

Rob

thanks

oznur
QUOTE
--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response


Rob Dixon
znur tastan wrote:
QUOTE

----- Original Message -----
From: "Rob Dixon" <[Email Removed]
To: <[Email Removed]
Sent: Friday, February 20, 2004 4:17 PM
Subject: Re: all matches of a regex-continued


znur tastan wrote:

I am still dealing with the same problem.
Rob has suggested me a good solution for macthing consecutive patterns
like
H K D  but not more looser ones like for K[ED]{3,5}?  L.{3}A
andn my poor perl knowledge doesn't help me to generalize it: /

Are you just saying that the patterns that you want to split on
can be regexes themselves instead of plain strings? I think my solution
will
do that fine, but you need to give us an example. All the solutions
you've been offered so far are based on your original example. You said:

I think there has been a discontuinty in replies (the mails didn't contain
everything) and I couldn't be very explanatory
(:( sorry)
you are right the patterns can ve regexes themselves. I just couldn't figure
out how to modify your code.

OK, but may we have an example of a target string and a set of patterns that
you need to work with?

Rob

Wc -Sx- Jones
znur Tatan wrote:

QUOTE
http://www.perl.com/pub/a/2002/06/04/apo5.html?page=8

$_ = "abracadabra";
@all = m:any /a.*?a/;
produces:

abra abraca abracada abracadabra aca acada acadabra ada adabra abra
Is there a version available that supports this structure?
Or are there any creative ideas of Perl wizards?
Thanks in advance

That is what will be supported in Perl 6 - so unless you have - say -
Perl 5.9 bleeding running in production -- you will prolly have to wait
a few more days? weeks? months? before Perl 6 is considered production.


In the meantime, try this test for development of R/E tests:

perl -w -Mre=debug -e '"abracadabra" =~ m/a.*a/g;'


For example -

perl -w -Mre=debug -e '"abracadabra" =~ m/a.*a/g;'
Freeing REx: `","'
Compiling REx `a.*a'
size 7 Got 60 bytes for offset annotations.
first at 1
1: EXACT <a>(3)
3: STAR(5)
4: REG_ANY(0)
5: EXACT <a>(7)
7: END(0)
anchored `a' at 0 floating `a' at (checking floating)
minlen 2
Offsets: [7]
1[1] 0[0] 3[1] 2[1] 4[1] 0[0] 5[0]
Guessing start of match, REx `a.*a' against `abracadabra'...
Found floating substr `a' at offset 3...
Found anchored substr `a' at offset 0...
Guessed: match at offset 0
Matching REx `a.*a' against `abracadabra'
Setting an EVAL scope, savestack=3
0 <> <abracadabra> | 1: EXACT <a>
1 <a> <bracadabra> | 3: STAR
REG_ANY can match 10 times out of...
Setting an EVAL scope, savestack=3
10 <abracadabr> <a> | 5: EXACT <a>
11 <abracadabra> <> | 7: END
Match successful!
Freeing REx: `"a.*a"'


Or, another example:

perl -w -Mre=debug -e '"abracadabra" =~ /r.*a/;'
Freeing REx: `","'
Compiling REx `r.*a'
size 7 Got 60 bytes for offset annotations.
first at 1
1: EXACT <r>(3)
3: STAR(5)
4: REG_ANY(0)
5: EXACT <a>(7)
7: END(0)
anchored `r' at 0 floating `a' at (checking floating)
minlen 2
Offsets: [7]
1[1] 0[0] 3[1] 2[1] 4[1] 0[0] 5[0]
Guessing start of match, REx `r.*a' against `abracadabra'...
Found floating substr `a' at offset 3...
Found anchored substr `r' at offset 2...
Starting position does not contradict /^/m...
Guessed: match at offset 2
Matching REx `r.*a' against `racadabra'
Setting an EVAL scope, savestack=3
2 <ab> <racadabra> | 1: EXACT <r>
3 <abr> <acadabra> | 3: STAR
REG_ANY can match 8 times out of...
Setting an EVAL scope, savestack=3
10 <abracadabr> <a> | 5: EXACT <a>
11 <abracadabra> <> | 7: END
Match successful!
Freeing REx: `"r.*a"'


Note the line:
REG_ANY can match 10 times out of...


.... it will help you avoid infinity.
HTH/Sx


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.