Find the answer to your Linux question:
Results 1 to 4 of 4
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 instead of if ($test[0] != 1 && $test[1] != 1){

    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:-
    Code:
    @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
    Last edited by devils casper; 02-05-2008 at 08:04 AM. Reason: Added [code].....[/code] tags.

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    It should be something like:

    Code:
    for i in "${test[@]}"
    do
      echo "$i"
    done
    Regards

  3. #3
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114
    And what about my if condition???

    This will only print the values of array

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    The previous example shows how to loop through the elements of an array.
    In your situation you can use a temporary variable for the first element.

    It should be something like:

    Code:
    for i in "${test[@]}"
    do
      if [ flag == 0 ]; then
        temp=$i
        flag=1
      else
        flag=0
        if [ $1 != 1 ] && [ $temp != 1 ]; then
          'Do your stuff here
        fi
      fi
    done
    Have a read of some useful links for bash scripting:

    BASH Programming - Introduction HOW-TO
    http://tldp.org/LDP/abs/html/


    Regards

Posting Permissions

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