Author Topic: How to extract multiple values from a string seperated by spaces [Solved]  (Read 2606 times)

SOLUTION:
Code: [Select]
function testF(%path)
{
if(!isfile(%path))
{
Echo("No file found");
return;
}
$Swol::SavVars = 0;
$Swol::BrickDats = 0;
$Swol::CurBrickDatStart = 0;
%file = new fileobject();
%file.openforread(%path);
Echo("A");
while(!%file.iseof())
{
%line = %file.readline();
%a = strpos(%line, "\"");
%varBrickCount += 1;
%db = getsubstr(%line, 0, %a);
%varString = getsubstr(%line, %a+1, strlen(%line));
%varStringBackup = %varString;
for(%i=0;%i<getWordCount(%varString);%i++)
{
%var[%i] = getWord(%varString,%i);
Echo(%var[%i]);
}
%varX = %var[0];
%varY = %var[1];
%varZ = %var[2];
%varRot = %var[3];
%varCol = %var[5];
}
%file.close();
%file.delete();
}


Code: [Select]
function storeBrickVars(%path)
{
if(!isfile(%path))
{
Echo("No file found");
return;
}
$Swol::SavVars = 0;
$Swol::BrickDats = 0;
$Swol::CurBrickDatStart = 0;
%file = new fileobject();
%file.openforread(%path);
Echo("A");
while(!%file.iseof())
{
%line = %file.readline();
%a = strpos(%line, "\"");
%db = getsubstr(%line, 0, %a);
%varString = getsubstr(%line, %a+1, strlen(%line));
%varStringBackup = %varString;
Echo(%varString);
for(%i=0;%i<strLen(%varString);%i++)
{
if(getsubstr(%varString,%i,1) $= " ")
{
$Swol::SavVar[$Swol::SavVars] = %i;
$Swol::SavVars++;
Echo("SAVVARS" SPC $Swol::SavVars SPC strLen(%varString));
echo(%varString);
%varString = getsubstr(%varStringBackup,%i+1,strLen(%varStringBackup));
}
Echo("i" SPC %i);
}
for(%i=0;%i<$Swol::SavVars;%i++)
{
$Swol::BrickDat[$Swol::BrickDats] = getsubstr(%varString,$Swol::CurBrickDatStart,$Swol::SavVar[%i]-$Swol::CurBrickDatStart);
$Swol::BrickDats++;
$Swol::CurBrickDatStart = $Swol::SavVar[%i];
Echo("BRICK DATS" SPC $Swol::BrickDats);
}
%spc = strPos(%line," ");
Echo(%db SPC %b @ "" @ %spc);
}
%file.close();
%file.delete();
}
so far not so good
these are my attempts to get each variable that are separated by spaces in a save file in a format like this
Code: [Select]
64x64 Base"128.5 122.5 0.1 2 1 21 0 0 1 1 1
This is what I have coded so far, it doesn't work, can someone please help me out and tell me what I'm doing wrong?
Some explanation on how the code is supposed to work:
first off we need to remove the first part of the string, the part that has the name of the brick and the quote, this works properly
after we need to loop through the string and find the location of all the spaces, this kindof works
then we need to go back in and extract the variables in between each of those space locations and put them into other variables
« Last Edit: June 11, 2013, 09:28:18 AM by swollow »


You could have searched first: http://forum.blockland.us/index.php?topic=225439.msg6374214#msg6374214
thats not what I'm asking, I want to know how to make a script read a save file and place the blocks through the script itself, not normal loading

thats not what I'm asking, I want to know how to make a script read a save file and place the blocks through the script itself, not normal loading
You would have to write that yourself. It might be easier to copy the save into a temporary file with increased z values and load that file

You would have to write that yourself. It might be easier to copy the save into a temporary file with increased z values and load that file
this isn't what I need, I'm making like a map generator, and I have multiple rooms I need to have auto built next to eachother in random orders

I don't really know how to go about reading the save file

Code: [Select]
function storeBrickVars(%path)
{
if(!isfile(%path))
{
Echo("No file found");
return;
}
$Swol::SavVars = 0;
$Swol::BrickDats = 0;
$Swol::CurBrickDatStart = 0;
%file = new fileobject();
%file.openforread(%path);
Echo("A");
while(!%file.iseof())
{
%line = %file.readline();
%a = strpos(%line, "\"");
%db = getsubstr(%line, 0, %a);
%varString = getsubstr(%line, %a+1, strlen(%line));
%varStringBackup = %varString;
Echo(%varString);
for(%i=0;%i<strLen(%varString);%i++)
{
if(getsubstr(%varString,%i,1) $= " ")
{
$Swol::SavVar[$Swol::SavVars] = %i;
$Swol::SavVars++;
Echo("SAVVARS" SPC $Swol::SavVars SPC strLen(%varString));
echo(%varString);
%varString = getsubstr(%varStringBackup,%i+1,strLen(%varStringBackup));
}
Echo("i" SPC %i);
}
for(%i=0;%i<$Swol::SavVars;%i++)
{
$Swol::BrickDat[$Swol::BrickDats] = getsubstr(%varString,$Swol::CurBrickDatStart,$Swol::SavVar[%i]-$Swol::CurBrickDatStart);
$Swol::BrickDats++;
$Swol::CurBrickDatStart = $Swol::SavVar[%i];
Echo("BRICK DATS" SPC $Swol::BrickDats);
}
%spc = strPos(%line," ");
Echo(%db SPC %b @ "" @ %spc);
}
%file.close();
%file.delete();
}
so far not so good
these are my attempts to get each variable that are separated by spaces in a save file in a format like this
Code: [Select]
64x64 Base"128.5 122.5 0.1 2 1 21 0 0 1 1 1
somebody halp plez

I created this for a Gamemode I was making, I never really did finish it. You can see how I loaded the variables in.
Code: [Select]
function HeroAttack::LoadHeroes(%client)
{
%fo = new FileObject();
%FirstFile = FindFirstFile("./Heroes/*.sav");
%File = %FirstFile;
while(%File !$= "")
{
%fo.openforRead($HeroAttack.FilePath @ "/Heroes/" @ fileBase(%file) @ ".sav");

%i = 1;
while(!%fo.isEOF())
{
%fo.line[%i] = %fo.readline();
%i++;
}

%heroname = getword(%fo.line1,1);
$HeroAttack.Hero[%heroname] = true;
$HeroAttack.Hero[%heroname,"Occupied"] = false;
$HeroAttack.Hero[%heroname,"Description"] = getwords(%fo.line2,1, getwordcount(%fo.line2));
$HeroAttack.Hero[%heroname,"Job"] = getword(%fo.line3,1);
$HeroAttack.Hero[%heroname,"Attribute"] = getword(%fo.line4,1);
$HeroAttack.Hero[%heroname,"PlayerType"] = getword(%fo.line5,1);
$HeroAttack.Hero[%heroname,"Scale"] = getwords(%fo.line6,1,3);
$HeroAttack.Hero[%heroname,"MaxHealth"] = getword(%fo.line7,1);
$HeroAttack.Hero[%heroname,"MaxMana"] = getword(%fo.line8,1);
$HeroAttack.Hero[%heroname,"Attack"] = getword(%fo.line9,1);
$HeroAttack.Hero[%heroname,"Armor"] = getword(%fo.line10,1);
$HeroAttack.Hero[%heroname,"NormalAttack"] = getword(%fo.line11,1);
$HeroAttack.Hero[%heroname,"Skill1"] = getwords(%fo.line12,1, getwordcount(%fo.line12));
$HeroAttack.Hero[%heroname,"Skill1Description"] = getwords(%fo.line13,1, getwordcount(%fo.line13));
$HeroAttack.Hero[%heroname,"Skill2"] = getwords(%fo.line14,1, getwordcount(%fo.line14));
$HeroAttack.Hero[%heroname,"Skill2Description"] = getwords(%fo.line15,1, getwordcount(%fo.line15));
$HeroAttack.Hero[%heroname,"Skill3"] = getwords(%fo.line16,1, getwordcount(%fo.line16));
$HeroAttack.Hero[%heroname,"Skill3Description"] = getwords(%fo.line17,1, getwordcount(%fo.line17));
$HeroAttack.Hero[%heroname,"Skill4"] = getwords(%fo.line18,1, getwordcount(%fo.line18));
$HeroAttack.Hero[%heroname,"Skill4Description"] = getwords(%fo.line19,1, getwordcount(%fo.line19));
$HeroAttack.Hero[%heroname,"Skill5"] = getwords(%fo.line20,1, getwordcount(%fo.line20));
$HeroAttack.Hero[%heroname,"Skill5Description"] = getwords(%fo.line21,1, getwordcount(%fo.line21));

%fo.close();

%FileName[%FileCount++ - 1] = fileBase(%File);
%File = FindNextFile("./Heroes/*.sav");
}

%fo.delete();
}
« Last Edit: June 11, 2013, 01:39:44 AM by Honorabl3 »

I understand how to read lines but I need to know how to extract strings seperated by from a string, and have no idea how long each value could be

If I read the title and OP correctly, I think you're looking for getWord(string, index);

I understand how to read lines but I need to know how to extract strings seperated by from a string, and have no idea how long each value could be
If you looked at the code I posted, you would have noticed:
Code: [Select]
getWord(%string,4);
- Returns the exact word weeded out by the number you provided. (Zero being the very first word in that string.)
getWordCount(%string);
- Returns the number of words in the following string.

Now, you must be wondering. What can I do with this information?

Well, if you have a string of information like:
Code: [Select]
%blah = "5 9 8 2";

You can weed out numbers like so:
Code: [Select]
%five = getWord(%blah, 0); //Zero being the very first word mentioned.
%nine = getWord(%blah, 1);
%eight = getWord(%blah, 2);
%two = getWord(%blah, 3);

Now you can do random things with the variables of which you just collected.
Code: [Select]
return (%five + %nine);
That would return 14.

oh thanks I didn't realize I could use getWord, that would have saved me a lot of troubles :p

Yes, the answer is to use getWord