Find the answer to your Linux question:
Results 1 to 3 of 3
I need to compare two values in same arrar and I don't want to hardcode it, need the logic (still trying to create some logic).. Following is hardcoded example need ...
  1. #1
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114

    How to compare values in same array in of doing hardcoded in Perl

    I need to compare two values in same arrar and I don't want to hardcode it, need the logic (still trying to create some logic)..

    Following is hardcoded example need to change it:-

    @test;


    if ($test[0] != 1 && $test[1] != 1){
    print "Test Passed\n";
    }else {
    Print "Test Failed\n";
    }

    Want to in this way
    foreach $t1 (@test){
    if (
    Some thing like this, not hardcoded


    Please suggest me

    Thanks

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    How about something like this:
    Code:
    my $all_match = 1;
    
    for my $val (@test)
    {
        $all_match = 0 if $val == 1;
    }
    $all_match acts as a flag, and you can check its value after the loop.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114
    No this can not work, see below if condition, its passing following combinations:-

    1 0, 0 1, 0 0

    if ($test[0] != 1 && $test[1] != 1){
    print "Test Passed\n";
    }else {
    Print "Test Failed\n";
    }

    but

    my $all_match = 1;

    for my $val (@test)
    {
    $all_match = 0 if $val == 1;
    }

    not passing all, suggest me

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...