Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Binding array to pattern

Reply
Thread Tools

Binding array to pattern

 
 
Shmuel (Seymour J.) Metz
Guest
Posts: n/a
 
      06-08-2006
I'd like to bind an array to a pattern. I couldn't find anything in
the Camel book about the context for the left side of the binding
operator. I ran some tests, and it appears that I get scalar context
if I write

while (@anarray =~ /pattern/g) {
block;
}

which means that I match against the size rather than the contents. I
tried

while ("@anarray" =~ /pattern/g) {
block;
}

but that went into a loop. Is there a better way to do this than

$_="@anarray";
while (/pattern/g) {
block;
}

?

Is there a description that I missed of interpolation for the left
side of the binding operator?

--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>

Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      06-08-2006
"Shmuel (Seymour J.) Metz" <(E-Mail Removed)> wrote:
> I'd like to bind an array to a pattern. I couldn't find anything in
> the Camel book about the context for the left side of the binding
> operator. I ran some tests, and it appears that I get scalar context
> if I write


I don't know about the camel book, but from perldoc perlop:
Binding Operators
Binary "=~" binds a scalar
expression to a pattern
match.

So yes, it is a scalar.

>
> while ("@anarray" =~ /pattern/g) {
> block;
> }
>
> but that went into a loop.


Of course it did, what with the while there and all. Oh, you mean
an infinite loop. Yep, it does seem to. But then again, so does:

while ("$_" =~ /pattern/g) {

So apparently the reinterpolation is performed each time and thus the
string is not known to be the same.


> Is there a better way to do this than
>
> $_="@anarray";
> while (/pattern/g) {
> block;
> }


I have no idea why you want to do it in the first place, but I can't think
of a better way to convert an array into a string and then repeatedly
matching on it in a while loop. (I guess localizing $_ first might be a
good idea.) I guess one possibly better alternative would be:

foreach ("@anarray" =~ /pattern/g) {

As it seems unlikely that the intermediate list would break the memory
bank.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-08-2006
Shmuel (Seymour J.) Metz wrote:
> I'd like to bind an array to a pattern.


Why?

> Is there a better way to do this than
>
> $_="@anarray";
> while (/pattern/g) {
> block;
> }
>
> ?


This is more readable IMO:

foreach my $element ( @anarray ) {
while ( $element =~ /PATTERN/g ) {
...
}
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      06-09-2006
Shmuel (Seymour J.) Metz <(E-Mail Removed)> wrote:

> I'd like to bind an array to a pattern.



That makes no sense.

A pattern match is *defined* to operate on a string. An array
is not a string.

Why would you like to bind an array to a pattern?

(and what is your new definition of "pattern match" to go along with it?)

Do you instead want to apply a pattern match to each _element_
of an array? If so, then use foreach or grep.


> I
> tried
>
> while ("@anarray" =~ /pattern/g) {
> block;
> }
>
> but that went into a loop.



Errr, the "while" construct _is_ a loop. If you don't want a loop,
then don't use "while".


> Is there a description that I missed of interpolation for the left
> side of the binding operator?



Interpolation has nothing to do with any of Perl's other operators.

Interpolation happens with "strings" without regard to what
operator the string is an operand for.



What is it that you are ultimately trying to achieve?


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      06-09-2006

Shmuel (Seymour J.) Metz wrote:
> I'd like to bind an array to a pattern. I couldn't find anything in
> the Camel book about the context for the left side of the binding
> operator. I ran some tests, and it appears that I get scalar context
> if I write
>
> while (@anarray =~ /pattern/g) {
> block;
> }
>
> which means that I match against the size rather than the contents. I
> tried
>
> while ("@anarray" =~ /pattern/g) {
> block;
> }
>
> but that went into a loop.


There problem is that you are using an expression with =~ //g

There was a similar problem here a while back.

http://groups.google.com/group/comp....dc49d7f9d7bf31

> Is there a description that I missed of interpolation for the left
> side of the binding operator?


It's not the fact that it's interpolation, it's the fact that it's an
rvalue expression so each time round the while() loop the =~ is binding
to a new string and the /g position pointer is starts again at zero.

 
Reply With Quote
 
Shmuel (Seymour J.) Metz
Guest
Posts: n/a
 
      06-09-2006
In <(E-Mail Removed)>, on 06/09/2006
at 01:30 AM, Gunnar Hjalmarsson <(E-Mail Removed)> said:

>Shmuel (Seymour J.) Metz wrote:
>> I'd like to bind an array to a pattern.


>Why?


I want to test for a match anywhere in the array.

>This is more readable IMO:
> foreach my $element ( @anarray ) {
> while ( $element =~ /PATTERN/g ) {
> ...
> }
> }


I had simplified my code because I was primarily concerned with the
endless loop rather than style. What I actually wound up with[1] was

foreach (sort keys %{$host_info{$host}{Email}}) {
push @Contacts, @{$host_info{$host}{Email}{$_}};
my $scalarContacts="@{$host_info{$host}{Email}{$_}}";
push @abuseContacts, @{$host_info{$host}{Email}{$_}}
if (/abuse/ or $scalarContacts =~ /abuse/);
}

and I'd rather avoid replicating the push statement. Given that, is
there a better style?

Thanks.

[1] The code is quck and dirty and at some point I intend to
do some massive cleanup, but it's still a work in progress.

--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>

Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to (E-Mail Removed)

 
Reply With Quote
 
Shmuel (Seymour J.) Metz
Guest
Posts: n/a
 
      06-09-2006
In <(E-Mail Removed)>, on 06/08/2006
at 07:33 PM, Tad McClellan <(E-Mail Removed)> said:

>That makes no sense.


To you. Others had no trouble understanding it.

>Why would you like to bind an array to a pattern?


Because I want all of the matches on all of the strings in the array.

>(and what is your new definition of "pattern match" to go along with
>it?)


What is your definition of "new", and are you really older than
Griswold?

>Do you instead want to apply a pattern match to each _element_ of an
>array?


That would be obvious if you looked at the code. Do you know how
interpolation works inside quotes?

>If so, then use foreach or grep.


That would complicate the logic in this case.

>Errr, the "while" construct _is_ a loop. If you don't want a loop,
>then don't use "while".


Sorry, I meant nonterminating loop. Another poster has explained the
problem.

>What is it that you are ultimately trying to achieve?


Scan an array for a pattern as part of a larger boolean expression.
The code that I posted was part of debug scaffolding that I wrote
while trying to resolve the original problem.

--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>

Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to (E-Mail Removed)

 
Reply With Quote
 
Shmuel (Seymour J.) Metz
Guest
Posts: n/a
 
      06-09-2006
In <(E-Mail Removed). com>, on
06/09/2006
at 05:17 AM, "Brian McCauley" <(E-Mail Removed)> said:

>It's not the fact that it's interpolation, it's the fact that it's an
>rvalue expression so each time round the while() loop the =~ is
>binding to a new string and the /g position pointer is starts again
>at zero.


Thanks.

--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>

Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to (E-Mail Removed)

 
Reply With Quote
 
Shmuel (Seymour J.) Metz
Guest
Posts: n/a
 
      06-09-2006
In <20060608191324.722$(E-Mail Removed)>, on 06/08/2006
at 10:08 PM, (E-Mail Removed) said:

>So apparently the reinterpolation is performed each time and thus the
>string is not known to be the same.


Ouch!

>Of course it did, what with the while there and all. Oh, you mean
>an infinite loop.


Well, it ends when I do ^c Sorry, I should have been clearer.

>I have no idea why you want to do it in the first place,


I need a term in a boolean expression for a match anywhere in the
array.

>I guess one possibly better alternative would be:
>foreach ("@anarray" =~ /pattern/g) {


That would have the wrong semantics even if it didn't go into an
endless loop. IAC, the code that I posted was test cases intended to
help me track down the original problem. The original failing code was
a match inside a boolean expression, and I have currently changed it
to the following:

foreach (sort keys %{$host_info{$host}{Email}}) {
push @Contacts, @{$host_info{$host}{Email}{$_}};
my $scalarContacts="@{$host_info{$host}{Email}{$_}}";
push @abuseContacts, @{$host_info{$host}{Email}{$_}}
if (/abuse/ or $scalarContacts =~ /abuse/);
}

I could use

foreach my $type (sort keys %{$host_info{$host}{Email}}) {

and throw in a nested

foreach (@{$host_info{$host}{Email}{$type}}) {

but I'd consider replicating the push to be uglier than coercing the
array to a string.

Thanks.

--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>

Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to (E-Mail Removed)

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      06-09-2006
>>>>> "SJM" == Shmuel (Seymour J ) Metz <(E-Mail Removed)> writes:

SJM> foreach (sort keys %{$host_info{$host}{Email}}) {
SJM> push @Contacts, @{$host_info{$host}{Email}{$_}};
SJM> my $scalarContacts="@{$host_info{$host}{Email}{$_}}";
SJM> push @abuseContacts, @{$host_info{$host}{Email}{$_}}
SJM> if (/abuse/ or $scalarContacts =~ /abuse/);
SJM> }

SJM> and I'd rather avoid replicating the push statement. Given that, is
SJM> there a better style?

disregarding the loop issues, that is very hard to read code. notice the
massive redundant use of $host_info{$host}{Email} in there? factor that
out into a scalar before the loop. and then it can become almost
readable (with some needed whitespace too)

my $email_info = $host_info{$host}{Email} ;

foreach (sort keys %{$email_info}) {

my $emails = $email_info->{$_} ;
push @Contacts, @{$emails};
push @abuseContacts, @{$emails};
my $scalarContacts = "@{$emails}";

if (/abuse/ or $scalarContacts =~ /abuse/);

that method of checking a joined string vs scanning a array bothers
me. and why do you push the same stuff into 2 different arrays?
if you used List::Utils::first you can scan for the first abuse email in
the array and it could be faster as you don't make up the string first.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 0 01-22-2012 10:26 PM
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 0 01-22-2012 10:25 PM
May I have a example of design pattern of "composite", I still feel fuzzy after reading book of Addison-Wesley's"design pattern " jones9413@yahoo.com C++ 1 08-31-2007 04:09 AM
documents related to factory design pattern and Abstract foctory pattern. sunny C++ 1 12-07-2006 04:26 AM
boolean endsWith(String s, Pattern pattern) lepikhin@gmail.com Java 17 11-16-2005 10:31 AM



Advertisments