#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    5

    Variable separator inside printf() in ruby?


    Hello to all,

    I have a ruby script including a printf() separating the fields by commas in this way:

    Code:
    printf("%d,%s,%s",$1,$2,$3)
    How can I set a variable separator inside the printf() in order to do something like this:
    Code:
    sep=","
    printf("%d sep %s sep %s",$1,$2,$3)
    Thanks in advance for any help.
  2. #2
  3. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,131
    Rep Power
    2011
    Try look at how the other variables are set into the string. %s is a place holder for string.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    5
    Thanks MrFujin,

    It works as you said in the way below. I was doing the opposite before, due to that failed.
    Code:
    var=","
    printf("%d%s%s%s%s",$1,var,$2,var,$3)
    One more question.

    How to pass as input the option to write the separator?

    In console I thinkg would be something like this:
    Code:
    script.rb "," inputfile
    and inside the script something like this:
    Code:
    var=ARG[0]
    input=ARG[1]
    but is not working.

    Thanks for any suggestion
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    5
    I realize that testing again, with the way I shown in previous post it works.

    Thanks so much for the help

IMN logo majestic logo threadwatch logo seochat tools logo