Help with conditional selection of fields... 
Author Message
 Help with conditional selection of fields...

Here is what I am trying to do:

Take a list of grouped id numbers separated by newline.  The data look like
this:

1
17
19
24
35
67

2
17
22
24
83
91
100
111

3
19
109
58

etc.

I am treating groups of numbers as a single record, and the numbers within
groups as fields.  I am selecting the first number of every group if there
are four or more numbers total in the group.  Then I am trying to select
three numbers that haven't already been selected in previous groups before
moving on to the next group(record).

My problem is with the test for whether a number has already been selected.
I suspect I'm doing something really stupid, but I'm currently a bit
stumped.  Any help is greatly appreciated.

Thanks.

BEGIN { FS = "\n"; RS = "\n\n" }

BEGIN {  ## set up array for holding matches
  current = 0;
  matches[current] = ""}

{
  ## if more than 4 records in group, print first
  if (NF >=

    printf("replace match = 1 if id == " $1";\n");
    user++;

    match_count = 0;
    while (match_count < 3)
      if ($i in matches) next;
      else
 for(i = 2; i <= NF; i++){
   temp_match[match_count] = $i;
   match_count++}

    printf("replace match = 1 if id == " temp_match[0]";\n");
    printf("replace match = 1 if id == " temp_match[1]";\n");
    printf("replace match = 1 if id == " temp_match[2]";\n")

    matches[current] = temp_match[0];
    current++; non_user++;
    matches[current] = temp_match[1];
    current++; non_user++;
    matches[current] = temp_match[2];
    current++; non_user++}

Quote:
}



Fri, 06 Apr 2001 03:00:00 GMT  
 Help with conditional selection of fields...

% I am treating groups of numbers as a single record, and the numbers within
% groups as fields.  I am selecting the first number of every group if there
% are four or more numbers total in the group.  Then I am trying to select
% three numbers that haven't already been selected in previous groups before
% moving on to the next group(record).

% My problem is with the test for whether a number has already been selected.

I don't know whether I understand what you're trying to achieve, but
try using the amazing associative array propensities of awk.

 BEGIN {
   matches[27] # 27 is now selected
   matches[33] # as is 33
 }
 { for (i = 2; i <= NF; i++)
     if (!($i in matches))
       matches[$i] # value of field i is now selected
 }
 # print out all the matches
 END { for (i in matches) print i }

--

Patrick TJ McPhee
East York  Canada



Sat, 07 Apr 2001 03:00:00 GMT  
 Help with conditional selection of fields...

Quote:
>   The data look like this:

> 1
> 17
> 19
> 24
> 35
> 67

> 2
> 17
> 22
> 24
> 83
> 91
> 100
> 111

> 3
> 19
> 109
> 58

> etc.

> I am treating groups of numbers as a single record, and the numbers within
> groups as fields.  I am selecting the first number of every group if there
> are four or more numbers total in the group.  Then I am trying to select
> three numbers that haven't already been selected in previous groups before
> moving on to the next group(record).

This should print the selections you want to make.
Other action is left to you.
In my experience, you have to use RS="".
I assume that in a group starting
4
8
8
8
you do not want to select the 8 three times, if not chosen in another
group before that one, but select 8 only once.  I also assume that you
wish to select  exactly 3 new elements, i.e., selecting only 2 is impossible.
##########################################################
BEGIN { FS = "\n"; RS = "";ORS=" " }
NF>3    { selected[4*NR]=$1
                  alreadyUsed[$1]=1
                  for (k=2;(k<=NF)&&(usedInGroup[NR]<3);k++) {
                     if( alreadyUsed[$(k)]==0 ) {
                           usedInGroup[NR]++
                           selected[4*NR+usedInGroup[NR]]=$(k)
                           alreadyUsed[$(k)]=1
                     } #if
                   } #for
                 if (usedInGroup[NR]==3) {
                       for(k=0;k<=usedInGroup[NR];k++) {
                            print selected[4*NR+k]
                       } #for
                      print "\n"
                  } else {
                       for(k=1;k<=usedInGroup[NR];k++) {
                         alreadyUsed[selected[4*NR+k]]=0
                        } # for
                      print selected[4*NR] "\n"
                 } #if
                 ## other action
              } #NF>3
##########################################################

not tested
LMS
free sed/awk book:
      ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.ps.gz
      ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.tar.gz



Sun, 08 Apr 2001 03:00:00 GMT  
 Help with conditional selection of fields...

Quote:

> Here is what I am trying to do:

> Take a list of grouped id numbers separated by newline. The data look like
> this:

> 1
> 17
> 19
> 24
> 35
> 67

> 2
> 17
> 22
> 24
> 83
> 91
> 100
> 111

> 3
> 19
> 109
> 58

> etc.

> I am treating groups of numbers as a single record, and the numbers within
> groups as fields. I am selecting the first number of every group if there
> are four or more numbers total in the group. Then I am trying to select
> three numbers that haven't already been selected in previous groups before
> moving on to the next group(record).

> My problem is with the test for whether a number has already been selected.
> I suspect I'm doing something really stupid, but I'm currently a bit
> stumped. Any help is greatly appreciated.

The script below finds records that contain at least three discrete
IDs that have not previously been selected, reduces the record to the
identifying field (field 1) and the three selected IDs (which become
fields 2, 3, and 4), then prints the record. There are two versions
of the same script: the first version, which may be a little easier
to read, has magic numbers in it that are eliminated in the second
version.

Given the small sample of data you provided, this script generates
the following output:

1
17
19
24

2
22
83
91

It needs to be more rigorously tested, of course. Good luck.

#-----------8<-----------8<-----------8<-----------8<-----------8<-----------
# select.awk

BEGIN {
    RS  = ""
    FS  = "\n"
    ORS = "\n\n"
    OFS = "\n"

Quote:
}

NF >= 4 {
    count = 0
    field = 2
    for (i in group)
        delete group[i]

    # match at most 3 discrete not-yet-selected IDs
    # from fields 2 through NF
    while (count < 3 && field <= NF) {
        if (!($field in selected) && !($field in group))
            matched[++count] = $field
        group[$(field++)]
    }

    # if 3 discrete not-yet-selected IDs were matched,
    # then reduce the record and print it
    if (count == 3) {
        while (count) {
            $(count + 1) = matched[count]
            selected[matched[count]]
            count--
        }
        NF = 4
        print
    }

Quote:
}

# # select.awk
#
# BEGIN {
#     RS  = ""
#     FS  = "\n"
#     ORS = "\n\n"
#     OFS = "\n"
#
#     FIELD = 2                  # initial ID field number
#     COUNT = 3                  # number of IDs required for selection
#     MINIM = FIELD + COUNT - 1  # minimum number of fields required in record
# }
#
# NF >= MINIM {
#     count = 0
#     field = FIELD
#     for (i in group)
#         delete group[i]
#
#     # match at most COUNT discrete not-yet-selected IDs
#     # from fields FIELD through NF
#     while (count < COUNT && field <= NF) {
#         if (!($field in selected) && !($field in group))
#             matched[++count] = $field
#         group[$(field++)]
#     }
#
#     # if COUNT discrete not-yet-selected IDs were matched,
#     # then reduce the record and print it
#     if (count == COUNT) {
#         while (count) {
#             $(count + 1) = matched[count]
#             selected[matched[count]]
#             count--
#         }
#         NF = MINIM
#         print
#     }
# }
#-----------8<-----------8<-----------8<-----------8<-----------8<-----------

--
Jim Monty

Tempe, Arizona USA



Sun, 08 Apr 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. (PEP-308) Python's Conditional Selection Operators

2. Field Printing conditional

3. Non-function of conditional field in C55 Report Writer

4. Conditional Field

5. ReportWriter conditional field Clarion 5 Prof.

6. Reset Fields and conditional behavior

7. CW 2.003 - Conditional Colors on an ENTRY field?

8. How do I make a count field conditional?

9. Conditional Fields in CW Report Writer

10. How do I make a conditional Autonumber field?

11. Report Writer Question on Conditional Fields

12. Clarion4 - Edit-In-Place - Conditional Disable Of Field

 

 
Powered by phpBB® Forum Software