Author Topic: Need to remove certain parts of an array  (Read 488 times)

I have a file that looks like this
Code: [Select]
$C2CMultilist::NumEntries = 0;
$C2CMultilist::user[$C2CMultilist::NumEntries++] = "White_Tiger";
$C2CMultilist::user[$C2CMultilist::NumEntries++] = "Lugnut1206";
$C2CMultilist::user[$C2CMultilist::NumEntries++] = "Derpington";

You get the idea.
suppose I want to remove the line that reads like this:
$C2CMultilist::user[$C2CMultilist::NumEntries++] = "Lugnut1206";
via a function or other method.
I have the following code, undoubtedly missing something important, is there anything that someone would like to add?

Updated: 3/4/12 1pm

Code: [Select]
function removeC2CGroupUser(%name)
{
%file = new FileObject();
%file.openForWrite("config/client/c2cgroup.cs");
for(%i = 0; %i < $C2CMultilist::NumEntries; %i++)
{
if($C2CMultilist::user[%i] $= %name)
{
$C2CMultilist::user[%i] = "";
%foundtarget = 1;
}
if(%foundtarget)
{
$C2CMultilist::user[%i--] = $C2CMultilist::user[%i--];
}
else
{
%file.writeline("$C2CMultilist::user[$C2CMultilist::NumEntries++] = \"" @ $C2CMultilist::user[%i] @ "\";");
}
}
%file.close();
%file.delete();
}
I.. uh.. haven't tested it yet, for a few real-world reasons.
« Last Edit: March 04, 2012, 02:01:05 PM by Lugnut1206 »

I'm pretty sure that will crash when it finds a match.

On another note, for the purpose you wan't it for, this would (assuming it worked), write all the variables BUT the one you specify to a file.
This isn't a hugely efficient way of doing it.

i'd suggest making a variable for the placement of it, and resetting the values after it to one behind them in a similar way you're setting them in the first place
and then you'd want to subtract one from numentries

%foundtarget isn't defined. Also, that 'if' statement is invalid

Code: [Select]
if(%foundtarget)
Code: [Select]
if(strstr($C2CMultilist::user[%i] $= %name) > -1)
The 2 things I listed are incorrect
1 - %foundtarget is not defined
2 - that is just so wrong, idk what it i supposed to do, but hell it's wrong. If you want to see if the $C2CMultilist::user[%i] is the %name, then just do if($C2CMultilist::user[%i] $= %name)

The 2 things I listed are incorrect
2 - that is just so wrong, idk what it i supposed to do, but hell it's wrong. If you want to see if the $C2CMultilist::user[%i] is the %name, then just do if($C2CMultilist::user[%i] $= %name)

oh. duh :(

Guys, %foundtarget is defined here:
Code: [Select]
if(strstr($C2CMultilist::user[%i] $= %name) > -1)
{
$C2CMultilist::user[%i] = "";
%foundtarget = 1;
admittedly, it's after the usage, but that shouldn't matter.

oh my god I had this
$C2CMultilist::user[%i--] == $C2CMultilist::user[%i];
jesus i need to rewrite this stuff