Re: How to put multiple values into a variable.



Pascal J. Bourguignon wrote:

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 = 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, 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


def foo *objects
objects
end
==>nil
foo( 2, 4, 9 )
==>[2, 4, 9]



# ...

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 )


file_link = [Dir.pwd, filename, mp3_title, mp3_artist].join(", ")


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 )
.



Relevant Pages

  • Re: How to save several variables with the same prefix?
    ... well the problem is that if I write fprintf(' filename', X) in each iteration it will create only one file named filename, it will overwrite it each time and I end up with only one file that has the value of X but just for s=11. ... is create the string this is why I put it inicialy without ' '. ... Thanks for answer me why fwrite did not work, that was somenthing I did not know but after fprintf did not work I begun to explore other ideas, I need it with format so fwrite is not an option. ... name that references the size of the window (filter) I am using in my ...
    (comp.soft-sys.matlab)
  • 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: unit testing C++ code from perl
    ... void ReportAssert(char const* description, char const* filename, int const ... That's probably the complaint string for an error. ... Then we have a filename and lineNumber. ... void FormatToStream(MemoryOutStream& stream, char const* format, ValueType const& value) ...
    (comp.programming)
  • 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)

Loading