Results 1 to 13 of 13
  1. #1
    Fouad is offline Member
    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Default most unique delimiter between key and value

    Hi,



    I need to write some implementation of key delimiter value, the delimiter which received as parameter will used to write it to the DB as "keyDelimeterValue".



    after that, I need to retrieve the key and the value , the issue that this delimiter is defined by some custom code and could be any thing.





    my problem begins when the delimiter which defined by custom code will be part of the 'key' or\and 'value' . in this case the retrieval of the key and value base on parsing and split or any regex will give the wrong key and value.


    note:- the delimiter will be received as parameter and will be used during the writing to the DB and the retrieving of the key and value from the DB in case of the needs.


    Please advice, what is your suggestion since m looking on the internet to find any piece of information or posted code from your implementation which can help.







    Thanks in advance.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: most unique delimiter between key and value

    You're better off giving an example you know. I think you mean this; Lets say the delimiter is a comma, and you get this data stream:

    Java Code:
    value1,value2,val,ue3,value4
    Which if you use a dumb split would turn into the tokens

    Java Code:
    value1
    value2
    val
    ue3
    value4
    The way that is dealt with generally in CSV files is to actually put quotes around either all tokens (easier to parse) or only the token that has a delimiter in it (harder to parse, but less data). Like so:

    Java Code:
    value1,value2,"val,ue3",value4
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Fouad is offline Member
    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Default Re: most unique delimiter between key and value

    Hi,

    lets say I have two parameters of Delimiters , one is the field delimiter and list delimiter .

    the field delimiter is comes as '+' and list delimiter in this specific case comes as ';' from the custom code , in my code I have them as variables which already populated as described.

    now , m saving the key in the value in the DB as following:-

    key1+value;key2+value2;key3+value3

    the issue begins when the key1 or/and value1 or/and key2 or/and value2 key3 or/and value3, contains the one of the delimiters , means:-

    ke+y1+val+u;e;+ke;y2++va+lue2;+k+e;y3+value+3

    now , I still save them in the same manner , but during retrieving from the db all the key value will be retrieved wrongly if I still using the same procedure for parsing.

    I need to mention that the same delimiters which used for writing to the DB will be used for reading the value , the issue that the delimiters will be exist as part of the key or/and value as described above.


    Thanks in advance.

  4. #4
    Fouad is offline Member
    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Default Re: most unique delimiter between key and value

    Hi,


    XML Code:
    Java Code: 
    
    
    
    
    
    1 
    
    value1,value2,val,ue3,value4 
    Which if you use a dumb split would turn into the tokens
     
    
    
    Java Code: 
    
    
    
    
    
    12345 
    
    value1value2value3value4 
    The way that is dealt with generally in CSV files is to actually put quotes around either all tokens (easier to parse) or only the token that has a delimiter in it (harder to parse, but less data). Like so:
     
    
    
    Java Code: 
    
    
    
    
    
    1 
    
    value1,value2,"val,ue3",value4

    the idea of wrapp the key word with quotes as following won't help.

    because the user can enter some thing with quotes and break the speration procedure which m using.


    user can enter any string includes the inverted comma and also string inside.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: most unique delimiter between key and value

    What can I say: garbage in is garbage out.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Fouad is offline Member
    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Default Re: most unique delimiter between key and value

    Quote Originally Posted by gimbal2 View Post
    What can I say: garbage in is garbage out.

    the issue i"m the one who need to deal with this garbase

    any way Man , Your desire to help and try to find the thing which m looking for is welcomed and appreciated.



    Thank you.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: most unique delimiter between key and value

    The question is: HOW do you deal with it. I've seen people try to accept garbage and try to fix that by throwing piles and piles of correcting code at it and fail miserably.

    There is only one way to properly deal with it: create boundaries. Like you are NOT allowed to input data-breaking quotes.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    raz0r is offline Member
    Join Date
    Aug 2013
    Location
    Cuxhaven, Germany
    Posts
    5
    Rep Power
    0

    Default Re: most unique delimiter between key and value

    lets say you use the '+' as delimiter.

    after the user inserted his/her string, replace all '+' by '-+' (key and value individual) and also add a '"' (or any other caracter but an '-') to the end of the key. then you concat the three parts and write it to your database.

    when you get the data from the database just split it by the '+' not follwing a '-'. now remove the last character of the key and replace all '-+' with '+' and you have your key and value back ;)

    Examples:

    name , Alex -> name"+Alex
    no+asd , aut++! -> no-+asd"+aut-+-+!
    no-+asd- , aut-+! -> no--+asd-"+aut--+!

    Regards,

    raz0r

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,086
    Rep Power
    4

    Default Re: most unique delimiter between key and value

    What the f*ck did I just read?

  10. #10
    raz0r is offline Member
    Join Date
    Aug 2013
    Location
    Cuxhaven, Germany
    Posts
    5
    Rep Power
    0

    Default Re: most unique delimiter between key and value

    Quote Originally Posted by SurfMan View Post
    What the f*ck did I just read?
    i don't get what you mean?

  11. #11
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: most unique delimiter between key and value

    ROTFLOL!

    Actually, I think he is describing something similar to zero bit insertion. It's a way to allow the delimiter as a character in the values you are delimiiting. I first encountered it when I was implementing ADCCP (think HDLC) many decades ago. The start and ending flags of a data frame are 01111110. So how do you transmit that sequence in the data? The hardware inserts a 0 every time it encounters five 1 bits in a row during transmission. And upon reception, the hardware removes any zero bit which trails five consecutive 1 bits.

    I didn't quite follow the post either (nor did I spend much time trying). But I found your reaction to his comment was hilarious.

    Regards,
    Jim
    Last edited by jim829; 08-28-2013 at 03:51 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: most unique delimiter between key and value

    Quote Originally Posted by jim829 View Post
    ROTFLOL!

    But I found your reaction to his comment was hilarious.
    Old geezers: so easily amused.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    raz0r is offline Member
    Join Date
    Aug 2013
    Location
    Cuxhaven, Germany
    Posts
    5
    Rep Power
    0

    Default Re: most unique delimiter between key and value

    Quote Originally Posted by jim829 View Post
    ROTFLOL!

    Actually, I think he is describing something similar to zero bit insertion. [...]
    my solution is pretty similar to zero bit insertion / bitstuffing. many data transparency solution work like this (e.g. escaping).

    Quote Originally Posted by jim829 View Post
    I didn't quite follow the post either (nor did I spend much time trying).
    here is some example code:

    Java Code:
    private static final char kvDelimiter = '+';
    
    private static final String dbStringRegex = "$([\\S\\s]+)\"" + kvDelimiter + "([\\S\\s]+)";
    
    public static String processToDB(String key, String value) {
        String oKey = key.replaceAll(kvDelimiter, '-' + kvDelimiter);
        String oValue = value.replaceAll(kvDelimiter, '-' + kvDelimiter);
    
        return oKey + '\"' + kvDelimter + oValue;
    }
    
    public static String processKeyFromDB(String dbString) {
        String oKey = dbString.replaceAll(dbStringRegex, "$1");
    
        return oKey.replaceAll('-' + kvDelimiter, kvDelimiter);
    }
    
    public static String processValueFromDB(String dbString) {
        String oKey = dbString.replaceAll(dbStringRegex, "$2");
    
        return oKey.replaceAll('-' + kvDelimiter, kvDelimiter);
    }
    Last edited by raz0r; 08-28-2013 at 06:01 PM. Reason: kvDelimiter variable

Similar Threads

  1. Scanner Delimiter
    By Games2Design in forum New To Java
    Replies: 1
    Last Post: 03-22-2013, 06:16 PM
  2. identify the delimiter..
    By saidinu in forum New To Java
    Replies: 2
    Last Post: 09-28-2011, 06:16 PM
  3. How to use delimiter in java
    By adityasirohi in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 11:01 PM
  4. How do you Use Backslash as a Delimiter?
    By Mobius-1 in forum New To Java
    Replies: 3
    Last Post: 12-14-2009, 01:39 AM
  5. delimiter
    By satin in forum New To Java
    Replies: 2
    Last Post: 11-17-2008, 11:50 PM

Posting Permissions

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