Re: How to put multiple values into a variable.



Harry Nash <hjnash@xxxxxxxxxxxxxxxx> writes:

I am new to coding, I have tried to place a number of data strings into
one variable

This is not possible. By definition a variable has only one value.
This is not quantic computing.

What you could do is to put the values in some object, and put that
object in the variable. You may choose between a custom object, a
hash-table, an array, a list, etc. For example:

filename = "/tmp/example.mp3"
mp3_title = "Example"
mp3_artist = "Musician"

file_link = [ Dir.pwd, filename, mp3_title, mp3_artist ]
file_link[0]
file_link[1]

file_link = { :directory => Dir.pwd,
:file => filename,
:title => mp3_title,
:artist => mp3_artist }
file_link[:directory]
file_link[:file]

class Cons
attr_accessor :first,:rest

def initialize(f,r)
@first=f
@rest=r
end

def Cons.list(*objects)
result = nil
objects . reverse . each { | object | result = Cons.new(object,result) }
result
end

# ...

end

file_link = Cons.list( Dir.pwd, filename, mp3_title, mp3_artist )
file_link . first
file_link . rest . first


but I am missing something in the format. See example
below.

What format? Are you speaking of the syntax of the language, or do
you want to format a string? I ask because the expression you show us
is not even syntactically valid...


Note each part of the line after = does have a real value,
I just can't format the string.

file-link = Dir.pwd, "#{filename}", "#{mp3-title}", "#{mp3-artist}"

So, assuming you want to build a new string formated with these
elements, separated by commas, you can do that with sprintf:

file_link = sprintf("%s, %s, %s, %s", Dir.pwd, filename, mp3_title, mp3_artist )

If you have several lines to format, you may want to specify column widths:

file_link = sprintf("%-20s, %-20s, %-30s, %s", Dir.pwd, filename, mp3_title, mp3_artist )


Also, Ruby is rather more limited than Lisp, you cannot put a dash
inside an identifier.


--
__Pascal Bourguignon__
.



Relevant Pages

  • Re: Date format detection
    ... Specifies the locale for which the date string is to be formatted. ... date format for this locale. ... the system default-date format for the specified locale. ... be enclosed within single quotation marks in the date format picture. ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: Date format detection
    ... > Specifies the locale for which the date string is to be formatted. ... > date format for this locale. ... > the system default-date format for the specified locale. ... > be enclosed within single quotation marks in the date format picture. ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: Date confusion
    ... Read my reply elsewhere in this string. ... > integer portion of the value represents the date as the number of days ... >> If you are working with a numeric date and you want the format DDMMYYYY, ... Another reason for preferring to store a date as text would be personal preference. ...
    (microsoft.public.access.modulesdaovba)
  • Re: Regular Expression - old regex module vs. re module
    ... If you want to include backslashes in a string, ... a one character string, which is unlikely to be what you wanted. ... formating characters before a format, then you should use a negative ... #read in and parse a format template ...
    (comp.lang.python)
  • Re: Regular Expression - old regex module vs. re module
    ... The ReportTemplate class reads a template file or string containing a ... header and line format for multiple calls with sequence data. ... def _format: ...
    (comp.lang.python)

Loading