Function NameGen(): attempt to compare nil with number - random

The error I'm getting is
[string "function NameGen()..."]:14: attempt to compare nil with number
stack traceback:
[string "function NameGen()..."]:14: in function 'NameGen'
[string "function NameGen()..."]:23: in main chunk
[C]: ?
My code:
function NameGen()
preftest = math.random(100) ;
syltest = math.random(100) ;
sufftest = math.random(100) ;
pref1 = "New ";
_1syl1 = "Lon";
_2syl1 = "Don";
suff1 = " City";
prefchoice = pref1;
_1sylchoice = _1syl1;
_2sylchoice = _2syl;
suffchoice = suff1;
if preftest < 50 and _2syltest < 50 and sufftest < 50 then
cityname = prefchoice .. _1sylchoice .. _2sylchoice .. suffchoice;
elseif preftest < 50 and _2syltest < 50 then
cityname = prefchoice .. _1sylchoice .. _2sylchoice;
else
cityname = _1sylchoice;
end
end
NameGen();
print(cityname);

I don't see where _2syltest is being assigned -- only syltest. If _2syltest isn't coming from somewhere else, that could be the issue, since that's the condition in your if uses that value.

From the structure of your code it seems that you wanted to use syltest < 50 in both if and elseif conditions, instead you used _2sylchoice < 50. Moreover probably you meant _2sylchoice = _2syl1 (a typo there?).
See if this is what you meant:
function NameGen()
preftest = math.random(100)
syltest = math.random(100)
sufftest = math.random(100)
pref1 = "New "
_1syl1 = "Lon"
_2syl1 = "Don";
suff1 = " City"
prefchoice = pref1
_1sylchoice = _1syl1
_2sylchoice = _2syl1
suffchoice = suff1
if preftest < 50 and syltest < 50 and sufftest < 50 then
cityname = prefchoice .. _1sylchoice .. _2sylchoice .. suffchoice
elseif preftest < 50 and syltest < 50 then
cityname = prefchoice .. _1sylchoice .. _2sylchoice
else
cityname = _1sylchoice
end
end
for i = 1, 100 do
NameGen()
print(cityname)
end
As an aside, you are using way too much global variables. All the variables you use should be local ones unless you have a strong reason to do otherwise (although I didn't change this aspect of your code in order not to confuse you in the case you weren't aware of what I'm talking about).

Related

I want to compare the price of stock with stoploss and alert me by email

I want to compare my list of stocks price with my set Stoploss which is stored and once the condition trigger alert by email. Below is my code
function emailAlert()
{
var stock1nameRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Watchlist").getRange("A5");
var stock1name = stock1nameRange.getValues();
var stock1cmpRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Watchlist").getRange("B5");
var stock1cmp = stock1cmpRange.getValues();
var stock1s1Range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Watchlist").getRange("AK5");
var s1 = stock1s1Range.getValues();
var stock1s2Range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Watchlist").getRange("AL5");
var s2 = stock1s2Range.getValues();
var ui = SpreadsheetApp.getUi();
if(stock1cmp<s1)
{
if(stock1cmp<s2)
{
ui.alert( stock1name + stock1cmp + 'is less than' +s2 );
var message = stock1name + stock1cmp + 'is less than' +s2 ;
MailApp.sendEmail("#gmail.com", "Stock Watchlist alert", message)
return s2
}
else
{
ui.alert( stock1name + stock1cmp + 'is less than' +s1 );
var message = stock1name + stock1cmp + 'is less than' +s1 ;
MailApp.sendEmail("#gmail.com", "Stock Watchlist alert", message )
return s1
}
}
}
This is for single stock. How can I make it more generic and compile all the stock list which pass the condition into single mail. Thanks.
Sen
In order to be able to select all the values you will be needing a for loop.
Snippet
function emailAlert() {
var ui = SpreadsheetApp.getUi();
var sheet = SpreadsheetApp.getActive().getSheetByName("Watchlist");
var stockName = sheet.getRange("A5:FINAL_RANGE").getValues();
var stockCmp = sheet.getRange("B5:FINAL_RANGE").getValues();
var s1 = sheet.getRange("AK5:FINAL_RANGE").getValues();
var s2 = sheet.getRange("AL5:FINAL_RANGE").getValues();
for (var i = 0; i < stockName.length; i++) {
if (stockCmp[i][0] < s1[i][0]) {
if (stockCmp[i][0] < s2[i][0]) {
ui.alert(stockName[i][0] + stockCmp[i][0] + ' is less than ' + s2[i][0]);
var message = stockName[i][0] + stockCmp[i][0] + ' is less than ' + s2[i][0];
MailApp.sendEmail("#gmail.com", "Stock Watchlist Alert", message);
return s2[i][0];
} else {
ui.alert(stockName[i][0] + stockCmp[i][0] + ' is less than ' + s1[i][0]);
var message = stockName[i][0] + stockCmp[i][0] + ' is less than ' + s1[i][0];
MailApp.sendEmail("#gmail.com", "Stock Watchlist Alert", message);
return s1[i][0];
}
}
}
}
Explanation
The above code loops through all the values from your columns by using a for loop and then based on the conditions you set, is sending the email and alerting the user. The range is retrieved by using the getRange() method with the a1Notation parameter. The a1Notation parameter here is represented by the start and the end of the range in which you have the values you need for the script.
Note
The above script is built considering the fact that the stockName, stockCmp, s1, s2 are all associated, meaning that they all have the same number of values stored in them.
Reference
Apps Script Range Class - getValues();
Apps Script Sheet Class - getRange(a1Notation);
JavaScript For Loop.

"for limit must be a number" error in roblox (Data Saving and Loading)

I leveled up my character and got some items, when i rejoined, it didn't load my previous data, It gave me level 0 and didn't load any items (it didn't give any output for level problem, but it gave this output for item problem: 'for' limit must be a number), but PLD.ItemNumber is a number, because It is from "ItemNumber" variable in the saving function. How to fix it? Code:
local DataStoreService = game:GetService("DataStoreService");
local PD = DataStoreService:GetDataStore("PlayerData");
function SaveData(Player)
local TableSave = {};
TableSave.Level = Player.PlayerData.Stats.Level.Value;
local TableItems = {};
local ItemNumber = 0;
for i,v in pairs(Player.PlayerData.Items:GetChildren()) do
ItemNumber = ItemNumber + 1;
TableItems["Item"..ItemNumber]={};
local TI = TableItems["Item"..ItemNumber];
TI.ItemName = v.Name;
TI.Level = v.Level.Value;
TI.Damage = v.Damage.Value;
end
table.insert(TableSave,TableItems);
TableSave.ItemNumber = ItemNumber;
PD:SetAsync(Player.userId,TableSave);
end
function LoadData(Player)
local success, err = pcall(function()
PLD = PD:GetAsync(Player.userId);
end)
if success then
Player.PlayerData.Stats.Level.Value = PLD.Level;
for i = 1,PLD.ItemNumber do
local CurrentItem = Instance.new("Folder");
CurrentItem.Name = PLD[1]["Item"..i].ItemName;
local Lv = Instance.new("IntValue");
Lv.Name = "Level";
Lv.Value = PLD[1]["Item"..i].Level;
Lv.Parent = CurrentItem;
local Dm = Instance.new("IntValue");
Dm.Name = "Damage";
Dm.Value = PLD[1]["Item"..i].Damage;
Dm.Parent = CurrentItem;
CurrentItem.Parent=Player.PlayerData.Items;
end
else
print("ERROR IN GET ASYNC");
end
end
game.Players.PlayerRemoving:Connect(function(plr)
repeat wait() until plr.PlayerData;
SaveData(plr);
end)
game.Players.PlayerAdded:Connect(function(plr)
repeat wait() until plr.PlayerData;
LoadData(plr);
end)

Stuck filling an array using VBscript and a For Loop

I have created an array to store some excel cell addresses, but while running through a portion of the array with a For Loop (because the values are in order for that portion of the data and its easier than doing each one at a time), but I get an error at line 22,(saData(i,1) = "D" count), right at the "count" variable but I cant figure out why.
'LOADING EXCEL CELL ADDRESS INTO INPUT ARRAY
saData(0,1) = "C3"
saData(1,1) = "F3"
saData(2,1) = "C4"
saData(3,1) = "F4"
saData(4,1) = "F6"
saData(5,1) = "C7"
saData(6,1) = "F7"
saData(7,1) = "C8"
saData(8,1) = "C9"
saData(9,1) = "F9"
saData(10,1) = "C11"
saData(12,1) = "F11"
saData(13,1) = "C13"
saData(14,1) = "F13"
Dim count : count = 16
For i = 15 to 54
saData(i,1) = "D" count
count = count + 1
next'
saData(55,1) = "G59"
saData(56,1) = "F60"
saData(57,1) = "B64"
saData(58,1) = "E64"
For i = 0 to 59
Msgbox saData(i,1)
next
Just try concatenating saData(i,1) = "D"&count

How do I fix these errors in my awk programs

I have this awk code:
BEGIN {
valid_name[0] = "CEO"
valid_name[1] = "maffu"
valid_name[2] = "gerry"
valid_name[3] = "bob"
valid_name[4] = "cath"
valid_name[5] = "tom.tom.the.son.of.the.piper"
valid_name[6] = "Insuro_Corp"
valid_name[7] = "who-pays-the-piper"
valid_name[8] = "a_hat_at_a_party"
valid_name[9] = "do_dot_the_eyes"
valid_name[10]= "Kim_dot_COM"
valid_domain[0] = "InsuroCorp"
valid_domain[1] = "cs.otago"
valid_domain[2] = "gmail"
valid_domain[3] = "enron"
valid_domain[4] = "research.techies"
valid_domain[5] = "1st.national"
valid_extension[0] = "co.nz"
valid_extension[1] = "com.au"
valid_extension[2] = "co.uk"
valid_extension[3] = "co.us"
valid_extension[4] = "co.ca"
valid_extension[5] = "com"
valid_numeric[0] = "[139.80.81.50]"
valid_numeric[1] = "[127.0.0.0]"
valid_numeric[2] = "[139.80.32.68]"
valid_numeric[3] = "[255.255.25.255]"
invalid_name[0] = "-foo"
invalid_name[1] = "f--d"
invalid_name[2] = "_at_"
invalid_name[3] = "Top$"
invalid_name[4] = "tom/tom"
invalid_name[5] = ".com.au"
invalid_name[6] = "white space"
invalid_name[7] = " white-space"
invalid_domain[0] = "Insuro-Corp"
invalid_domain[1] = "cs_otago"
invalid_domain[3] = "100%"
invalid_domain[4] = "AT&T"
invalid_extension[0] = "ac.nz"
invalid_extension[1] = "edu.au"
invalid_extension[2] = "tv"
invalid_extension[3] = "com.us"
invalid_extension[4] = "edu"
invalid_numeric[0] = "139.80.81.50"
invalid_numeric[1] = "[1..2]"
invalid_numeric[2] = "[139-80-81-50]"
invalid_numeric[3] = "[1][2][3]"
}
function generate_invalid_e_mail_address() {
at = rand() < 0.3 ? "_at_" : rand() < 0.1 ? "" : "#"
dot = dot = rand() < 0.3 ? "_dot_" : rand() < 0.1 ? "" : "."
if (rand() < 0.5) {
name = valid_name[int(rand()*11)]
if (rand() < 0.3) {
numeric = invalid_numeric[int(rand()*4)]
print name at numeric
} else {
if (rand() < 0.5) {
domain = valid_domain[int(rand()*6)]
extension = invalid_extension[int(rand()*5)]
} else {
domain = invalid_domain[int(rand()*5)]
extension = valid_extension[int(rand()*6)]
}
print name at domain dot extension
}
} else {
name = invalid_name[int(rand()*8)]
if (rand() < 0.3) {
numeric = valid_numeric[int(rand()*4)]
print name at numeric
} else {
domain = valid_domain[int(rand()*5)]
extension = valid_extension[int(rand()*6)]
print name at domain dot extension
}
}
}
BEGIN {
print "maffu#cs.otago.ac.nz"
print "bob.gmail.com"
for (i = 0; i < 518; i++) generate_invalid_e_mail_address()
}
This program should generate email address test cases and put
them in a file called 'bad.data' with the command:
awk -f bad.awk >bad.data
Instead bad.data is created as an empty file because of these
errors:
awk: bad.awk:70: extension = invalid_extension[int(rand()*5)]
awk: bad.awk:70: ^ syntax error
awk: bad.awk:73: extension = valid_extension[int(rand()*6)]
awk: bad.awk:73: ^ syntax error
awk: bad.awk:76: print name at domain dot extension
awk: bad.awk:76: ^ unexpected newline or end of string
awk: bad.awk:84: extension = valid_extension[int(rand()*6)]
awk: bad.awk:84: ^ syntax error
awk: bad.awk:86: print name at domain dot extension
awk: bad.awk:86: ^ unexpected newline or end of string
This is the first awk code I have seen. How do I fix it?
It looks like extension is a keyword. Just replace all extensions to extension1 for example:
extension1 = invalid_extension[int(rand()*5)];

String: replacing spaces by a number

I would like to replace every blank spaces in a string by a fixnum (which is the number of blank spaces).
Let me give an example:
s = "hello, how are you ?"
omg(s) # => "hello,3how10are2you1?"
Do you see a way (sexy if possible) to update a string like this?
Thank you Rubists :)
gsub can be fed a block for the "replace with" param, the result of the block is inserted into place where the match was found. The argument to the block is the matched string. So to implement this we capture as much whitespace as we can ( /\s+/ ) and feed that into the block each time a section is found, returning that string's length, which gets put back where the whitespace was originally found.
Code:
s = "hello, how are you ?"
res = s.gsub(/\s+/) { |m| m.length }
puts res
# => hello,3how10are2you1?
it is possible to do this via an array split : Javascript example
var s = "hello, how are you ?";
function omg( str ) {
var strArr = str.split('');
var count = 0;
var finalStr = '';
for( var i = 0; i < strArr.length; i++ ) {
if( strArr[i] == ' ' ) {
count++;
}
else
{
if( count > 0 ) {
finalStr += '' + count;
count = 0;
}
finalStr += strArr[i];
}
}
return finalStr
}
alert( omg( s ) ); //"hello,3how10are2you1?"
Lol, this seems the best it can be for javascript

Resources