Sebastjan H. wrote in post #1059386:
Thank you all for your kind help. I’ll try to complete this task and
when I am done, I’ll post back if maybe someone else can benefit from
it.
regards,
seba
Hello again,
It seems I’m stuck:)
After rethinking what I actually need, I’ve come with the script below.
For some reason, it only works when there are 26 or more strings in the
array. I’ve been looking like crazy, but I can’t find the error. Also
any optimisation or pointing out additional errors is most welcome. Keep
in mind, I am a complete beginner:)
Note: I only need a list of unused characters, which may be used for
assignments.
1. Import strings from a file and add them to the array;
if there are more than 26 strings imported from the file,
an alert is given and user decision is required
User may decide to continue since there may still be a char
unused if others ore used more than once.
file = ARGV[0]
strings = []
used_chars = []
duplicates = []
all = ["&a", “&b”, “&c”, “&d”, “&e”, “&f”, “&g”, “&h”, “&i”, “&j”, “&k”,
“&l”,
“&m”, “&n”, “&o”, “&p”, “&r”, “&s”, “&t”, “&u”, “&v”, “&w”, “&x”, “&y”,
“&z”]
input_file = File.open(file, “a+”)
input_file.each_line do |line|
strings << line
end
strings.map!{|c| c.downcase.strip} # Eliminates the whole upcase issue.
if strings.length >= 26
puts "There are more than 26 strings, unique assignments are not
possible."
puts "Do you still want to continue with the process? Enter y or n"
print ">"
answer = STDIN.gets.chomp()
if answer == "n"
puts "Process aborted by user."
exit
else
2. Finds the ampersand combinations; adds them to used and duplicates;
checks for number of occurences ---- still under consideration if
needed
strings.each do|str|
char = str[/&[a-z]/]
duplicates << char if used_chars.include? char
used_chars << char
end
3. Compares the all and used arrays and adds the list of unused
chars to the file
unused = [(all)-(used_chars)]
input_file.write(“Here are the unused characters:” “\n”)
unused.each do |char|
input_file.write(char)
input_file.write("\n")
end
end
end
input_file.close()
ToDo:
1. The automatic suggestion would only be possible if the unused
characters
would be compared with the characters in the strings with duplicates.
If the unused character exists in the string, suggest the character
from the
unused array, otherwise move on to the next. If no match is found,
move to
the next string with duplicates.
2. use Shoes for GUI.
thx
kind regards
seba