How to check 2 charAt(i) - char

I have this lines:
812.12 135.14 646.17 1
812.12 135.14 646.18 1
812.12 135.14 646.19 10
812.12 135.14 646.20 10
812.12 135.14 646.21 100
812.12 135.14 646.22 100
I want to delete only the last group so I did code like this:
if(lines[i].charAt(lines[i].length())-1>= '0'&&lines[i].charAt(lines[i].length()-1)<= '9'){
lines[i] = lines[i].substring(0, lines[i].length()-1);
}
else if(lines[i].charAt(lines[i].length())>='10'+c&&lines[i].charAt(lines[i].length()-1)<='99'){
lines[i] = lines[i].substring(0, lines[i].length()-2);
}
else if(lines[i].charAt(lines[i].length())>='100'&&lines[i].charAt(lines[i].length()-1)<='999){
lines[i] = lines[i].substring(0, lines[i].length()-3);
}
And it's now working for me I need help please

It isn't working it says a wrong on this line:
lines[i] = lines[i].substring(0, index);
#Everyone

You have characters and trying to compare them as numbers. It doesn't work that way.
A char in most higher-level (Java, .NET languages...etc) programming languages follows UTF-16 format by default. Since each char size is 2 bytes it can follow any encoding you need.
You cannot compare a char to string and you cannot attempt to put string in char like '10'. Those are two characters, not one, it's a string.
Now based on your desired output, you would like to remove everything after the last space. To do that, you can use the following code:
static void updateLines(String[] lines){
for(int i=0;i<lines.length; i++){
// get index of the last space
int index = lines[i].lastIndexOf(" ");
// remove everything after the last space
lines[i] = lines[i].substring(0, index);
}
}
public static void main(String[] args) {
String[] lines = new String[]{
"812.12 135.14 646.17 1",
"812.12 135.14 646.18 1",
"812.12 135.14 646.19 10",
"812.12 135.14 646.20 10",
"812.12 135.14 646.21 100",
"812.12 135.14 646.22 100",
"812.12 135.14 646.23 1000",
"812.12 135.14 646.24 1000"
};
updateLines(lines);
for(int i=0;i<lines.length;i++)
System.out.println(lines[i]);
}
Output:
812.12 135.14 646.17
812.12 135.14 646.18
812.12 135.14 646.19
812.12 135.14 646.20
812.12 135.14 646.21
812.12 135.14 646.22
812.12 135.14 646.23
812.12 135.14 646.24
Is this enough or do you need to compare numbers?

Related

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

awk on a text file to csv

I have a massive text file that I need to convert to an CSV file so I can import it to an MySQL database.
The text file looks like this:
Original text file
VL;1;1001;Productname 1;Description 1;2;MTR;METER;217883;10000;20180402;1;010206;&10;PRODUCER1;;N;10000;;
VA;2;4044773815245;V;
VA;3;0036453;V;
VL;1;1002;Productname 2;This is product decrtiption for 2 product;2;MTR;METER;140365;10000;20180402;1;010206;&10;PRODUCER1;;N;10000;;
VX;WEIGHT;7500
VX;VOLUME;3249
VX;DIMENSJON;57x57x1000
VA;2;4044773452884;V;
VA;3;0036479;V;
VL;1;1003;Productname 3;Description......;2;MTR;METER;1575;10000;20171006;1;010606;&10;PRODUCER1;;N;10000;;
VX;PDF;1003.pdf
VX;IMAGE;1003.png
VX;BASEINFO;http://127.0.0.1/1003/
VX;WEIGHT;20
VX;DIMENSJON;0x7x0
VX;UNSPSC;26121616
VA;2;7070613017149;V;
VA;3;1000116;V;
Wanted result
I will need to convert it into a CSV file that looks like this:
type; Productnumber; Productname; Description; measurement_unit; price_unit; price_unit_txt; price; crowd; price_date; status; block_number; discount_group; manufac; type; stocked; sales_package; discount; price_type; PDF; IMAGE; baseinfo; WEIGHT; VOLUME; dimensjon; UNSPSC; va_2; va_3;
1; 1001; Productname 1; Description 1; 2; MTR; METER; 217883; 10000; 20180402 1; 010206; &10; PRODUCER1; ; N; 10000; ; ; ; ; ; ; ; ; ; 4044773815245; 0036453;
1; 1002; Productname 2; Description 2; 2; MTR; METER; 140365; 10000; 20180402; 1; 010206; &10; PRODUCER2 ; N; 10000; ; ; ; ; ; 7500; 3249; 57x57x1000; ; 4044773452884; 0036479;
1; 1003; Productname 3; Description ABC 3; 2; MTR; METER; 1575; 10000; 20171006; 1; 010606; &10; PRODUCER3; ; N; 10000; ; ; 1003.pdf; 1003.png; http://127.0.0.1/1003/; 20; ; 0x7x0; 26121616; 7070613017149; 1000116;
Explanation of the original file
The fist product line is always starting with VL and then continue in this order:
type;Productnumber;Productname;Description;measurement_unit;price_unit;price_unit_txt;price;crowd;price_date;status;block_number;discount_group;manufac;type;stocked;sales_package;discount;price_type;
PDF is always on a new line starting with VX;PDF;
IMAGE is always on a new line starting with VX;IMAGE;
baseinfo is always on a new line starting with VX;BASEINFO;
WEIGHT is always on a new line starting with VX;WEIGHT;
VOLUME is always on a new line starting with VX;VOLUME;
dimensjon is always on a new line starting with VX;DIMENSJON;
UNSPSC is always on a new line starting with VX;UNSPSC;
va_2 is always on a new line starting with VA;2;
va_3 is always on a new line starting with VA;3;
Hope someone can help me out with this :)
a way possible (not the wole solution)
#!/bin/bash
awk -F';' '
function init() {
# formation line to print_line
line = vl pdf image baseinfo weight volume dimensjon unspsc va_2 va_3
# erase ^M (\r)
gsub( /\r/;"";line )
# print a block
print line
# initialisation variables
vl = pdf = image = baseinfo = weight = volume = dimensjon = unspsc = va_2 = va_3 = ";"
}
# head/title, note that "%12s" format with 12 characters width
BEGIN { printf ( "%12s; %s; %s; %s; %s; %s; %s; %s; %s; %s;","vl","pdf","image","baseinfo ","weight","volume","dimensjon","unspsc","va_2","va_3" ) }
/^VL/ { init(); ; vl = sprintf( "%12s; %s; %s; %s; ", $3, $4, $5, $6 ) }
/^VX;WEIGHT;/ { weight = sprintf( "%s; ", $3 )}
# .. another conditions
END { init() }
' file.dat # > outputfile.csv
for test:
cat << end > file.dat
VL;1;1001;Productname 1;Description 1;2;MTR;METER;217883;10000;20180402;1;010206;&10;PRODUCER1;;N;10000;;
VA;2;4044773815245;V;
VA;3;0036453;V;
VL;1;1002;Productname 2;This is product decrtiption for 2 product;2;MTR;METER;140365;10000;20180402;1;010206;&10;PRODUCER1;;N;10000;;
VX;WEIGHT;7500
VX;VOLUME;3249
VX;DIMENSJON;57x57x1000
VA;2;4044773452884;V;
VA;3;0036479;V;
VL;1;1003;Productname 3;Description......;2;MTR;METER;1575;10000;20171006;1;010606;&10;PRODUCER1;;N;10000;;
VX;PDF;1003.pdf
VX;IMAGE;1003.png
VX;BASEINFO;http://127.0.0.1/1003/
VX;WEIGHT;20
VX;DIMENSJON;0x7x0
VX;UNSPSC;26121616
VA;2;7070613017149;V;
VA;3;1000116;V;
end
ouput
vl; pdf; image; baseinfo ; weight; volume; dimensjon; unspsc; va_2; va_3;
1001; Productname 1; Description 1; 2; ;;;;;;;;;
1002; Productname 2; This is product decrtiption for 2 product; 2; ;;;7500; ;;;;;
1003; Productname 3; Description......; 2; ;;;20; ;;;;;

How to Add a Slice Array inside of a Struct

I'm looking to add an array for a string variable inside of a struct I have created in Go.
type Recipes struct { //Struct for recipe information
name string
prepTime int
cookTime int
recipeIngredient string
recipeID int
recipeYield int
}
It is called by
Recipe1.name = "BBQ Pulled Chicken"
Recipe1.prepTime = 25
Recipe1.cookTime = 5
Recipe1.recipeIngredient = "1 8-ounce can reduced-sodium tomato sauce, two"
Recipe1.recipeID = 1
Recipe1.recipeYield = 8
recipeIngredient will have multiple ingredients so it just can't be one string. I would like to have multiple array/slice elements inside recipeIngredient. Anyone have an idea on how i would be able to do this please?
Use a slice of string. For example,
package main
import "fmt"
type Recipe struct {
Name string
PrepTime int
CookTime int
Ingredients []string
ID int
Yield int
}
func main() {
var recipe Recipe
recipe.Name = "BBQ Pulled Chicken"
recipe.PrepTime = 25
recipe.CookTime = 5
recipe.Ingredients = append(recipe.Ingredients,
"1 8-ounce can reduced-sodium tomato sauce",
)
recipe.Ingredients = append(recipe.Ingredients,
"1/2 medium onion, grated ",
)
recipe.ID = 1
recipe.Yield = 8
fmt.Println(recipe)
fmt.Printf("Ingredients: %q\n", recipe.Ingredients)
}
Output:
{BBQ Pulled Chicken 25 5 [1 8-ounce can reduced-sodium tomato sauce 1/2 medium onion, grated ] 1 8}
Ingredients: ["1 8-ounce can reduced-sodium tomato sauce" "1/2 medium onion, grated "]

how to sort a table in lua?

I have a lua table that contains 2 key pieces of data. I would like to sort the table in ascending order by the "num1" column, or if thats not possible, they by the key value in ascending order
Here's what I have so far:
local widgets = {}
widgets[1568] = {}
widgets[1568]["num1"] = 99999
widgets[1568]["val2"] = "NA"
widgets[246] = {}
widgets[246]["num1"] = 90885
widgets[246]["val2"] = "NA"
widgets[250] = {}
widgets[250]["num1"] = 95689
widgets[250]["val2"] = "NA"
widgets[251] = {}
widgets[251]["num1"] = 95326
widgets[251]["val2"] = "NA"
widgets[252] = {}
widgets[252]["num1"] = 95301
widgets[252]["val2"] = "NA"
widgets[256] = {}
widgets[256]["num1"] = 95303
widgets[256]["val2"] = "NA"
-- ATTEMPT TO SORT
--table.sort(widgets, function(a,b) return tonumber(a.num1.value) < tonumber(b.num1.value) end)
--table.sort(widgets, function(a,b) return tonumber(a.num1) < tonumber(b.num1) end)
--TRY SORTING BY ID:
table.sort(widgets, function(a,b) return tonumber(a) < tonumber(b) end)
for i, v in pairs(widgets) do
print(v.num1)
end
Any suggestions would be appreciated. Right now, I'm reviewing Sort a Table[] in Lua to try to understand the "spairs" function. But that example is slightly different because I have a table within a table...
Thanks.
SOLUTION
In line with the answer below, I created a new table and added the records from the old table, one by one, using table insert like so:
local new_widgets = {}
for i, v in pairs(widgets) do
table.insert(new_widgets, id=v.id, num1= v.num1, num2 = v.num2)
end
then I sorted new_wigets.
Lua tables are hashtables. Their entries have no specific order.
You fake it by using consecutive numerical indices then iterating by incrementing a number (note: internally Lua actually will implement this as an array, but that's an implementation detail; conceptually, table entries have no specific order).
t[2] = "two"
t[3] = "three"
t[1] = "one"
for i=1,#t do print(t[i]) end
ipairs creates an iterator that does the same thing as this for loop.
So, if you want your data sorted, you need to put it in a table with consecutive numeric indices.
In your case, there's a lot of different ways you can do it. Here's one way to skin that cat:
Instead of this:
local widgets = {
[246] = { num1 = 90885, val2 = "NA" }
[250] = { num1 = 95689, val2 = "NA" }
[251] = { num1 = 95326, val2 = "NA" }
[252] = { num1 = 95301, val2 = "NA" }
[256] = { num1 = 95303, val2 = "NA" }
}
You want this:
local widgets = {
{ id = 246, num1 = 90885, val2 = "NA" },
{ id = 250, num1 = 95689, val2 = "NA" },
{ id = 251, num1 = 95326, val2 = "NA" },
{ id = 252, num1 = 95301, val2 = "NA" },
{ id = 256, num1 = 95303, val2 = "NA" },
}
-- sort ascending by num1
table.sort(widgets, function(a,b) return a.num1 < b.num1 end)
for i, widget in ipairs(widgets) do
print(widget.num1)
end
If you need the ability to then lookup a widget quickly by id, you can create a lookup table for that:
local widgetById = {}
for i,widget in pairs(widgets) do
widgetById[widget.id] = widget
end

Sorting a Table Containing Tables

I can sort a table with two pieces of information (the name and a second piece, such as age) with the following code:
t = {
Steve = 4,
Derek = 1,
Mike = 5,
Steph = 10,
Mary = 7,
Danny = 2
}
function pairsByKeys (t,f)
local a = {}
for x in pairs (t) do
a[#a + 1] = x
end
table.sort(a,f)
local i = 0
return function ()
i = i + 1
return a[i], t[a[i]]
end
end
for a,t in pairsByKeys (t) do
print (a,t)
end
Result:
Danny 2
Derek 1
Mary 7
Mike 5
Steph 10
Steve 4
I have a scenario where at a convention each person's name tag contains a barcode. This barcode, when scanned, enters four pieces of information about each person into a table database. This database is made up of the following pieces:
t = {
{name = "Mike", addr = "738 Rose Rd", age = 30, phone = "333-902-6543"}
{name = "Steph", addr = "1010 Mustang Dr", age = 28, phone = "555-842-0606"}
{name = "George", addr = "211 Glass St", age = 34, phone = "111-294-9903"}
}
But how would I change my code to sort all four entries (name, addr, age, phone) by age and keep all variables in line with one another?
I've been trying to experiment and am getting the hang of sorting a table by pairs and have a better idea how to perform table.sort. But now I want to take this another step.
Can I please receive some help from one of the programming gurus here?! It is greatly appreciated guys! Thanks!
You could use the age as the key of your table:
t = {
[30] = {name = "Mike", addr = "738 Rose Rd", age = 30, phone = "333-902-6543"},
[28] = {name = "Steph", addr = "1010 Mustang Dr", age = 28, phone = "555-842-0606"},
[34] = {name = "George", addr = "211 Glass St", age = 34, phone = "111-294-9903"},
}
function pairsByKeys (t,f)
local a = {}
for x in pairs (t) do
a[#a + 1] = x
end
table.sort(a,f)
local i = 0
return function ()
i = i + 1
return a[i], t[a[i]]
end
end
for a,t in pairsByKeys (t) do
print (t.name, t.addr, t.age, t.phone)
end
EDIT
Otherwise, if you don't want to change the structure of t, you could change your iterator generating function to keep track of the indexing:
t = {
{name = "Mike", addr = "738 Rose Rd", age = 30, phone = "333-902-6543"},
{name = "Steph", addr = "1010 Mustang Dr", age = 28, phone = "555-842-0606"},
{name = "George", addr = "211 Glass St", age = 34, phone = "111-294-9903"},
}
function pairsByAgeField(t,f)
local a = {}
local index = {}
for _, x in pairs(t) do
local age = x.age
a[#a + 1] = age
index[age] = x
end
table.sort(a,f)
local i = 0
return function ()
i = i + 1
return a[i], index[a[i]]
end
end
for a,t in pairsByAgeField(t) do
print (t.name, t.addr, t.age, t.phone)
end
Of course this makes pairsByAgeField less generally applicable than your original pairsByKeys (it assumes that the table being iterated has a given structure), but this is not a problem if you often have to deal with tables such as t in your application.

Resources