if,then with array containing scalars

Discussion in 'Perl Misc' started by erik, Mar 26, 2005.

  1. erik

    erik Guest

    I want to check all these scalars for the value of OK in them. The code
    below does not works. Maybe I should do a foreach with $line but that
    is not working either. How can I check the re OK on each element in the
    array?

    (this code does not work)
    @final_report = ($log_action,$crc_action,$voyence_action);
    if (@final_report =~ /OK/)
    {
    print "<body><font color=33CC33>YOU CAN CUTOVER THIS DEVICE TO
    OPERATIONS</font></body>";
    }
    else{
    print "<body><font color=FF0000>NOT OK TO CUTOVER, PLEASE FIX AND
    RETEST</font></body>";
    }
    erik, Mar 26, 2005
    #1
    1. Advertising

  2. erik wrote:
    > I want to check all these scalars for the value of OK in them. The code
    > below does not works.


    And since you have warnings enabled, you already know one of the reasons
    why, right?

    > Maybe I should do a foreach with $line but that
    > is not working either.


    Why not? Did you really try hard enough?

    > How can I check the re OK on each element in the
    > array?
    >
    > (this code does not work)
    > @final_report = ($log_action,$crc_action,$voyence_action);
    > if (@final_report =~ /OK/)


    This is one possibility:

    if ( ( grep $_ eq 'OK', @final_report ) == @final_report )

    See "perldoc -f grep".

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Mar 26, 2005
    #2
    1. Advertising

  3. erik wrote:
    > I want to check all these scalars for the value of OK in them. The
    > code below does not works. Maybe I should do a foreach with $line but
    > that is not working either. How can I check the re OK on each element
    > in the array?
    >
    > (this code does not work)
    > @final_report = ($log_action,$crc_action,$voyence_action);
    > if (@final_report =~ /OK/)


    Well, let's see what the documentation has to say about this construct.
    "perldoc perlop" about the binding operator:
    Binary "=~" binds a scalar expression to a pattern match.[...]
    Well, why did you think =~ would work on an array?

    Did you consider to simply grep() for your OK elements (or non-OK) elements
    and just count the returned items?
    If zero non-OK items, then everything must be OK, right?

    jue
    Jürgen Exner, Mar 26, 2005
    #3
  4. erik wrote:

    > I want to check all these scalars for the value of OK in them. The code
    > below does not works. Maybe I should do a foreach with $line but that
    > is not working either. How can I check the re OK on each element in the
    > array?
    >
    > (this code does not work)
    > @final_report = ($log_action,$crc_action,$voyence_action);
    > if (@final_report =~ /OK/)


    You've misunderstood what a match does in list context, like you're using it
    here. What it *doesn't* do is match against all the list elements - you've
    already figured out that part! :)

    What it *does* do is return a list of matched subexpressions. But the regex
    you're matching against above doesn't have any subexpressions. So the
    returned list is empty.

    For details, have a look at:
    perldoc perlre
    perldoc perlretut

    If what you want to do is perform a given match against each item in a list,
    you want to use the grep() function, like this:

    if (grep(/OK/, @final_report)) {
    # ... do stuff
    }

    For details, have a look at:
    perldoc -f grep

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Mar 26, 2005
    #4
  5. erik

    robin Guest

    the problem is that =~ does not do much with arrays.
    -robin
    robin, Mar 26, 2005
    #5
  6. robin wrote:

    > the problem is that =~ does not do much with arrays.


    A match in list context returns a list of matched values - that's a far cry
    from "not much". It's quite useful, in fact, in helping to produce more
    compact, readable code.

    Here's an example from 'perlretut', first using a match in scalar context:

    # Extract hours, minutes, seconds
    $_ = '10:15:30';
    my ($hours, $minutes, $seconds);
    if (/(\d\d):(\d\d):(\d\d)/) {
    $hours = $1;
    $minutes = $2;
    $seconds = $3;
    }

    Now, here's the same code, using an array:

    $_ = '10:15:30';
    my ($hours, $minutes, $seconds) =~ /(\d\d):(\d\d):(\d\d)/;

    For details, have a look at:
    perldoc perlretut

    The whole thing is a good read, but the section titled "Extracting matches"
    is particularly relevant to this particular subject.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Mar 26, 2005
    #6
  7. erik wrote:

    > I want to check all these scalars for the value of OK in them. The code
    > below does not works. Maybe I should do a foreach with $line but that
    > is not working either. How can I check the re OK on each element in the
    > array?


    perldoc -f grep
    >
    > (this code does not work)
    > @final_report = ($log_action,$crc_action,$voyence_action);
    > if (@final_report =~ /OK/)


    This uses @final_report in a scalar context. You are asking if
    the number "3" contains the string "OK".

    > {
    > print "<body><font color=33CC33>YOU CAN CUTOVER THIS DEVICE TO
    > OPERATIONS</font></body>";
    > }
    > else{
    > print "<body><font color=FF0000>NOT OK TO CUTOVER, PLEASE FIX AND
    > RETEST</font></body>";
    > }


    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
    Chris Mattern, Mar 26, 2005
    #7
  8. Sherm Pendley wrote:

    > erik wrote:
    >
    >> (this code does not work)
    >> @final_report = ($log_action,$crc_action,$voyence_action);
    >> if (@final_report =~ /OK/)

    >
    > If what you want to do is perform a given match against each item in a
    > list, you want to use the grep() function, like this:
    >
    > if (grep(/OK/, @final_report)) {


    D'oh! grep() returns the number of matched elements, so obviously you want
    to compare that to the number of elements in @final_report:

    if(grep(/OK/, @final_report) == @final_report) {

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Mar 26, 2005
    #8
  9. Gunnar Hjalmarsson wrote:
    > erik wrote:
    >> I want to check all these scalars for the value of OK in them. The
    >> code below does not works.

    >
    > And since you have warnings enabled, you already know one of the
    > reasons why, right?
    >
    >> Maybe I should do a foreach with $line but that
    >> is not working either.

    >
    > Why not? Did you really try hard enough?
    >
    >> How can I check the re OK on each element in the
    >> array?
    >>
    >> (this code does not work)
    >> @final_report = ($log_action,$crc_action,$voyence_action);
    >> if (@final_report =~ /OK/)

    >
    > This is one possibility:
    >
    > if ( ( grep $_ eq 'OK', @final_report ) == @final_report )


    Close, but the OP explicitely asked for an RE match of 'OK' on each element,
    not for equality to 'OK'.
    However, because 'OK' doesn't contain any regular expression a simple
    index() should work. No need to deploy the big RE gun.

    jue
    Jürgen Exner, Mar 26, 2005
    #9
  10. erik

    Bob Walton Guest

    erik wrote:

    > I want to check all these scalars for the value of OK in them. The code
    > below does not works. Maybe I should do a foreach with $line but that
    > is not working either. How can I check the re OK on each element in the
    > array?
    >
    > (this code does not work)
    > @final_report = ($log_action,$crc_action,$voyence_action);
    > if (@final_report =~ /OK/)


    The =~ operator accepts a scalar left-hand-side, so in this case,
    it will be equivalent to:

    if ('3' =~ /OK/)

    as the scalar value of @final_report is the number of elements in
    the array. Which won't match. You'll need to check each array
    element for a match, then decide what action to take based on how
    many array elements matched. See below for an example.

    > {
    > print "<body><font color=33CC33>YOU CAN CUTOVER THIS DEVICE TO
    > OPERATIONS</font></body>";
    > }
    > else{
    > print "<body><font color=FF0000>NOT OK TO CUTOVER, PLEASE FIX AND
    > RETEST</font></body>";
    > }
    >


    Here is maybe what you meant:

    use warnings;
    use strict;
    #change one or more of following 3 lines to something
    #not containing OK for testing
    my $log_action='OK';
    my $crc_action='OK';
    my $voyence_action='OK';
    my @final_report = ($log_action,$crc_action,$voyence_action);
    my $OKcount=0;
    for (@final_report){
    $OKcount++ if /OK/;
    }
    if ($OKcount>=@final_report){ #assumes all must be OK
    print "<body><font color=33CC33>YOU CAN CUTOVER THIS DEVICE
    TO OPERATIONS</font></body>";
    }
    else{
    print "<body><font color=FF0000>NOT OK TO CUTOVER, PLEASE FIX
    AND RETEST</font></body>";
    }

    ....

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Mar 26, 2005
    #10
  11. Sherm Pendley wrote:
    > robin wrote:
    >
    >> the problem is that =~ does not do much with arrays.

    >
    > A match in list context returns a list of matched values


    True.
    But the return value of the m operator has little to do with the left
    argument of the binding operator.

    jue
    Jürgen Exner, Mar 26, 2005
    #11
  12. Sherm Pendley wrote:
    > robin wrote:
    >> the problem is that =~ does not do much with arrays.

    >
    > A match in list context returns a list of matched values - that's a far cry
    > from "not much".


    I'd give robin more right this time. See my comments on your other post
    in this thread.

    > Here's an example from 'perlretut', first using a match in scalar context:
    >
    > # Extract hours, minutes, seconds
    > $_ = '10:15:30';
    > my ($hours, $minutes, $seconds);
    > if (/(\d\d):(\d\d):(\d\d)/) {
    > $hours = $1;
    > $minutes = $2;
    > $seconds = $3;
    > }
    >
    > Now, here's the same code, using an array:
    >
    > $_ = '10:15:30';
    > my ($hours, $minutes, $seconds) =~ /(\d\d):(\d\d):(\d\d)/;


    my ($hours, $minutes, $seconds) = /(\d\d):(\d\d):(\d\d)/;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Mar 26, 2005
    #12
  13. Jürgen Exner wrote:
    > Gunnar Hjalmarsson wrote:
    >>
    >> if ( ( grep $_ eq 'OK', @final_report ) == @final_report )

    >
    > Close, but the OP explicitely asked for an RE match of 'OK' on each element,
    > not for equality to 'OK'.


    Well, he said "check ... for the value of 'OK'". It's true that he used
    the m// operator, but people often do so also when it's not necessary. I
    simply made an assumption.

    > However, because 'OK' doesn't contain any regular expression a simple
    > index() should work. No need to deploy the big RE gun.


    We seem to be agreed on the latter, at least. :)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Mar 26, 2005
    #13
  14. Sherm Pendley, Mar 26, 2005
    #14
  15. Gunnar Hjalmarsson wrote:

    > No, no. I'm afraid that also you have misunderstood what it does.


    Not so much a misunderstanding as a late-night, half-asleep brain freeze.
    One of those things where you look back the next day, with a clear head,
    and think "WTF was I thinking? I know better than that..."

    I'd better get some sleep, before I start making idiotic comparisons between
    Python and Perl.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Mar 26, 2005
    #15
  16. Sherm Pendley wrote:
    > I'd better get some sleep, before I start making idiotic comparisons between
    > Python and Perl.


    On behalf of all sane readers of this group: Thanks!! :)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Mar 26, 2005
    #16
  17. erik <> wrote:

    > if (@final_report =~ /OK/)



    You should always enable warnings when developing Perl code!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 26, 2005
    #17
  18. Gunnar Hjalmarsson <> wrote:
    > Sherm Pendley wrote:
    >> erik wrote:
    >>>
    >>> if (@final_report =~ /OK/)

    >>
    >> You've misunderstood what a match does in list context, like you're using it
    >> here.

    >
    > True.



    False!

    The pattern match above is in *scalar* context.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 26, 2005
    #18
  19. Tad McClellan wrote:
    > Gunnar Hjalmarsson wrote:
    >> Sherm Pendley wrote:
    >>> erik wrote:
    >>>>
    >>>> if (@final_report =~ /OK/)
    >>>
    >>> You've misunderstood what a match does in list context, like
    >>> you're using it here.

    >>
    >> True.

    >
    > False!
    >
    > The pattern match above is in *scalar* context.


    Thanks for correcting my attempt at a correction. :-/

    Sometimes I wish you could just delete a thread and start it all over.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Mar 26, 2005
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    276
  2. Rich Grise

    Putting 3 scalars at a time into an array

    Rich Grise, Jun 6, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    105
    Ben Morrow
    Jun 6, 2004
  3. Aaron Anodide
    Replies:
    9
    Views:
    112
    Aaron Anodide
    Aug 26, 2004
  4. Robert
    Replies:
    5
    Views:
    98
    Nathan Wagner
    Sep 22, 2005
  5. Replies:
    36
    Views:
    312
    Gunnar Hjalmarsson
    Mar 13, 2006
Loading...

Share This Page