LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2014, 12:16 PM   #1
Perseus
Member
 
Registered: Oct 2011
Posts: 179

Rep: Reputation: Disabled
How to insert separator within printf in ruby


Hello to everyone,

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)
I want to give the command options when run script.

Thanks in advance
 
Old 12-04-2014, 12:41 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Other than that it looks ugly, what's wrong with:
Code:
printf("%d%s%s%s%s",$1,sep,$2,sep,$3)
 
Old 12-04-2014, 12:57 PM   #3
Perseus
Member
 
Registered: Oct 2011
Posts: 179

Original Poster
Rep: Reputation: Disabled
Hello smalpond,

Thanks for your answer.

Yes. That way works fine!

One more question:
I run the script currently like this:
Code:
Myscript.rb inputfile
How to add the separator as input parameter (optional)? something like:

Code:
Myscript.rb -sep "," inputfile
Thanks again
 
Old 12-04-2014, 05:17 PM   #4
Perseus
Member
 
Registered: Oct 2011
Posts: 179

Original Poster
Rep: Reputation: Disabled
Testing I've found that I can do it in this way.

Code:
sep=ARG[0]
input=ARG[1]
and the script could be send like this:
Code:
Myscript.rb "," inputfile
Thanks for the help
 
Old 12-05-2014, 02:44 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well you could do it that way ... you might want to do some checking around how many parameters were passed to the script otherwise less that 2 will yield odd results and perhaps errors.

As for your original question, I would have said the easiest way would be:
Code:
puts [$1,$2,$3].join(sep)
 
Old 12-06-2014, 12:26 AM   #6
Perseus
Member
 
Registered: Oct 2011
Posts: 179

Original Poster
Rep: Reputation: Disabled
Hello grail,

Thanks for your help. Following your suggestion I've used
print [$1,$2,$3].join(sep) instead of puts since puts inserts a end of line.

Regarding the options to pass to the script, may you suggest me a way to handle when parameters are passed or not
in the console. I want to use 2 optional parameters (-a and -b and their respective values) and a mandatory one
that is the input file.

To run the script would be like this:
Code:
Script.rb -a "," -b "@" input
Is there a better way than set several "if" conditions to test if one, two or all parameters where passed?

This is how looks what I have so far.
Code:
if ARGV[0][0..1] == "-a" and ARGV[1].length > 0 and ARGV[2][0..1] == "-b" and ARGV[3].length > 0 then 
	...
end
Thanks again for the help.
 
Old 12-06-2014, 05:45 AM   #7
kevinmilan2014
LQ Newbie
 
Registered: Nov 2014
Posts: 17

Rep: Reputation: 1
Quote:
printf("the %s jumped over the %s, %d times", "cow", "moon", 2);
And here are three different printf examples, using different methods that are available to you in the Java programming language:

Quote:
System.out.format("the %s jumped over the %s, %d times", "cow", "moon", 2);
System.err.format("the %s jumped over the %s, %d times", "cow", "moon", 2);
String result = String.format("the %s jumped over the %s, %d times", "cow", "moon", 2);
 
Old 12-07-2014, 10:58 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
For parameter checking, I would suggest a similar tactic as with other languages:

1. test number of parameters passed to script (ARGV.length)

2. loop over each one using either 'if' (one or two options) or 'case' (multiple) to check and apply the switches as required

Another idea, based on your current example may also be to test that ARGV.length is always odd (ie. 1, 3, 5) cause if the user were to pass
2 arguments and they were '-a' and the input file, this would cause your if to still be correct but for the script to behave very strangely
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How to install Ruby and Ruby on Rails on CentOS LXer Syndicated Linux News 0 09-15-2014 07:30 PM
RVM + local install ruby + system ruby maintenance Corpus-Khu Linux - Software 0 02-13-2014 01:45 AM
Any issues installing Ruby Gems and Ruby on Rails in Slackware? Lufbery Slackware 8 02-09-2011 07:22 PM
Ruby SDL/ Ruby OpenGL tutorials/reference? LinuxNoob75 Programming 0 12-30-2007 12:47 PM
Bash command separator/arguments separator reverse Programming 11 12-02-2007 09:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration