LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Else if or not else if ? That is the question. (https://www.linuxquestions.org/questions/linux-newbie-8/else-if-or-not-else-if-that-is-the-question-937291/)

Trd300 03-30-2012 11:43 AM

Else if or not else if ? That is the question.
 
%%%%%

grail 03-30-2012 01:32 PM

Have you printed all the values you are using? ie. all the values stored in each array?

Trd300 03-30-2012 10:29 PM

%%%%%

grail 03-31-2012 11:34 AM

Would appear you get different results to me:

Altered code:
Code:

#!/usr/bin/awk -f

BEGIN{ FS = OFS = "|" }

$1 == "" || $1 ~ / +/ && $2 == "" || $2 ~ / +/ {
    f3 = f4 = ""

    split($2,c2,"[ ;]")

    print c2[2],c2[3],c2[5],c2[6]
    a = split($1,c1," *; *")

    for( i = 1; i <= a; i++){
        split(c1[i], b, " ")

        print b[2],b[3]
        if ( b[2] >= c2[2] && b[3] <= c2[3] )
            f3 = f3 (f3?"; ":"")c1[i]
        if ( b[2] >= c2[5] && b[3] <= c2[6] )
            f4 = f4 (f4?"; ":"")c1[i]

    }

    $3 = f3
    $4 = f4

}
1

On running:
Code:

$ ./test.awk file
10|20|bb_b|20
12|18
23|27

aa_a 12 18; aa_a 23 27|bb_b 10 20; bb_b 20 30|aa_a 12 18|

The results in red would appear to be an issue?

rknichols 04-01-2012 11:43 AM

Note that the string fragment "; " contains two delimiters with a null string between them.

Trd300 04-01-2012 07:43 PM

Thanks grail !
Thanks rknichols !

I didn't know it counted the empty string between ";" and " ".

I just had to increment the indices of the second part by +1 and it works of course !

Thanks guys !

grail 04-02-2012 01:05 AM

Actually the null is not an issue, but rather your split as it says split on either " " OR ";" so as there is more than one of these in the middle it caused an issue.
Simply try adding "+" to your regex as it needs to have one or more of those as a separator.
Code:

split($2,c2,"[ ;]+")


All times are GMT -5. The time now is 08:01 PM.