Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

regexp searches over array slices

by Reverend Phil (Pilgrim)
on Nov 09, 2001 at 03:18 UTC ( [id://124229]=perlquestion: print w/replies, xml ) Need Help??

Reverend Phil has asked for the wisdom of the Perl Monks concerning the following question:

Hello folks.

I was pulling in a line of data and unpacking it into an array. I was then going to perform some function on the condition that there was any non-space data in two of these array elements. What I had done was:
if(@array[3..4] =~ /\S/) { do stuff; }

For some odd reason, this was not working. In order to see if I could do this, I had gone directly into the interpreter (dos prompt, type perl) and populated an array and tried out the code. It all worked as I expected, and that's why I used it. For some reason though, I am unable to do this from a script on another Win2k box. I have to write it out as:
if( ($array[3] =~ /\S/) or ($array[4] =~ /\S/) ){ do stuff; }
That works. That's fine. But I'm not happy either way =)
Does anyone know why such a thing would work in the interpreter and not through a script?

Awaiting wisdom,
-=rev=-

Replies are listed 'Best First'.
Re: regexp searches over array slices
by japhy (Canon) on Nov 09, 2001 at 03:28 UTC
    Well, Perl 6 will let you do:
    if (@array[3,4] ^=~ /\S/) { ... }
    but in the meantime, grep() is the tool for you:
    if (grep /\S/, @array[3,4]) { ... }

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Um,

      Well, Perl 6 will let you do: if (@array[3,4] ^=~ /\S/) { ... }
      so, in Perl 6, will that mean "and" or "or"?

      That is, I recall ^=~ being able to work over a list, but what will if do with the resulting list? Will /\S/ be given a list context so that it returns an empty list upon failure so the final list is empty if and only if both matches failed?

      In other words, does it become this? if(  map {/\S/} @array[3,4]  ) { (which has the same effect as the grep version but for slightly different reasons).

      So, I think I've answered my own question but I'll post this anyway as I think some might find it interesting.

              - tye (but my friends call me "Tye")
        Well, Perl 6 will let you do:
        if (@array[3,4] ^=~ /\S/) { ... }
        so, in Perl 6, will that mean "and" or "or"?

        Neither. It's exactly the same as: if (@array[3] =~ /\S/, @array[4] =~ /\S/) { ... }

        which is true if @array[4] contains any non-whitespace.

        That is, I recall ^=~ being able to work over a list, but what will if do with the resulting list?

        Just what a Perl 5 if does with it.

        In other words, does it become this (a grep-ish map)?

        No, I don't believe so.

        BTW, what you're looking for is: if (any(@array[3,4]) =~ /\S/) {...} or: if (all(@array[3,4]) =~ /\S/) {...} both of which (I'm very hopeful) will be standard in Perl 6.

        I believe it acts like grep() here, but I could be wrong. Perhaps we should tap TheDamian.

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: regexp searches over array slices
by runrig (Abbot) on Nov 09, 2001 at 03:29 UTC
    if(@array[3..4] =~ /\S/) { do stuff; }
    is the same as:
    if($array[4] =~ /\S/) { do stuff; }
    The slice evaluates to the last element. You can try one of these to 'stringify' the array slice first (assuming $" is set to the default):
    if("@array[3,4]" =~ /\S/) { or if ("@array[3,4]" =~ tr/ \t\n//c) {
Re: regexp searches over array slices
by Fletch (Bishop) on Nov 09, 2001 at 03:30 UTC

    Um, there's no difference between putting code into a program and typing it into perl's STDIN yourself. Both are the exact same thing and do the exact same things with your code. Basically the slice appears to be working like the comma operator in scalar context and you're really just doing $array[4] =~ /\S/.

    At any rate, you might try something like:

    if( 2 == grep /\S/, @array[3..4] ) { stuff(); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://124229]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 19:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found