Simplest way to perform attribute match

Hi,
I have a input record like
 
acct|N|Y|N|N|rose@gmail.com


Now I need to create a logic to append a code to the end of the file using the following matrix rules.

00 N N N N
01 N N N Y
02 N N Y N
03 N N Y Y
04 N Y N N
05 N Y N Y
06 N Y Y N
07 N Y Y Y
08 Y N N N
09 Y N N Y
10 Y N Y N
11 Y N Y Y
12 Y Y N N
13 Y Y N Y
14 Y Y Y N
15 Y Y Y Y


In the above example these four flags are "N|Y|N|N", so I need to append the matching code at the end of file "04".

desired output :
 
acct|N|Y|N|N|rose@gmail.com|04|

as it matches code '04':
04 N Y N N


can anyone help me with the logic need to be used here?
Thanks in advance:)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <sstream>
#include <string>
#include <bitset>
#include <iostream>
#include <iomanip>

int main()
{
    const std::string test[] = { "...|N|Y|N|N|...", "...|Y|N|Y|N|...", "...|N|Y|Y|N|..." } ;

    for( const std::string& str : test )
    {
        std::istringstream stm(str) ;

        // parse the four Y/N into bool values
        // and form a bitset of four bits with with 'Y' == 1

        constexpr std::size_t NBITS = 4 ;
        std::bitset<NBITS> bits ; // set of four bits
        char Y_or_N ;
        for( std::size_t i = 0 ; i < NBITS ; ++i )
        {
            stm.ignore( 100, '|' ) ; // throw characters away upto and including the next '|'
            stm >> Y_or_N ; // read the next char ('Y' or 'N')
            bits[ (NBITS-1) - i ] = Y_or_N == 'Y' ; // set the corrosponding bit to 1 if it is a 'Y'
        }

        // get the code by converting the bits to an integral value
        auto code = bits.to_ulong() ;

        // check it out
        std::cout << str << "  " << bits << "  " << std::setw(2) << std::setfill('0') << code << '\n' ;
    }
}

http://ideone.com/fAl0JZ
I'm a little unclear - are you trying to start with the string "N|Y|N|N", and find the corresponding numerical value?

Or are you trying to start with the numerical value 04, and find the corresponding string of Y/N flags?

Edit: In either case, I recommend you read up on the std::map container class, as it sounds like it will be suitable for this.
Last edited on
Hi Mikey,
I'm starting with string "N|Y|N|N" and finding the numeric value.
Here in this case the value is "04".
The rule is simple and clear if substitute N for 0 and Y for 1. So the combination

NYNN corresponds to binary number 0100 that is equal to 4.
I'm also trying to use strstr function for doing so.
like
if (strstr (rec,"|N|Y|N|N|")))!= NULL)
append "04";
else if (strstr (rec,"|N|Y|Y|N|")))!= NULL)
append "06";
.
.
.
so on..

correct me if it's not good way to do so.
Topic archived. No new replies allowed.