[PHP] Duplicating multiple lines but increasing one value every time

Mave

TMS Founder
Administrator
Messages
234,196
Location
Belgium
I have these lines of objects:

Code:
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2645.1599121094" posZ="233.39700317383" rotX="0" rotY="312" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2616.0129394531" posZ="210.02699279785" rotX="0" rotY="1" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2632.96875" posZ="218.43699645996" rotX="0" rotY="335.99487304688" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2650.1540527344" posZ="251.81100463867" rotX="0" rotY="288" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2647.2219238281" posZ="270.71200561523" rotX="0" rotY="264" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2636.8701171875" posZ="286.76300048828" rotX="0" rotY="240" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2621.3010253906" posZ="296.90798950195" rotX="0" rotY="216" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2602.5810546875" posZ="299.91799926758" rotX="0" rotY="192" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2569.4689941406" posZ="283.11898803711" rotX="0" rotY="143.99993896484" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2584.318359375" posZ="295.07800292969" rotX="0" rotY="167.99743652344" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2560.876953125" posZ="266.33898925781" rotX="0" rotY="120" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2559.8640136719" posZ="247.63900756836" rotX="0" rotY="96" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2580.1201171875" posZ="216.18899536133" rotX="0" rotY="48" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2566.6923828125" posZ="229.68899536133" rotX="0" rotY="71.998901367188" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2598.0048828125" posZ="209.26899719238" rotX="0" rotY="24" rotZ="270"></object>

What I want to do is repeatedly decrease posX by 20, in total 20 times (or more)

So keep this entire block, next block of lines is gonna have posX as 2660 instead of 2680, then next block is going to be 2640 instead of 2660 etc.

Not really sure how I would approach this.

Of course I could manually duplicate the block, replace posX="2680" in the new block and repeat, but I would like to automate this.
 
Last edited:
I'm not familiar with PHP, but wouldn't something like
Code:
var x = 2680;
for x in amount:
    <object yaddayadda value=x>
    x -= 20
be possible?
 
I'm not familiar with PHP, but wouldn't something like
Code:
var x = 2680;
for x in amount:
    <object yaddayadda value=x>
    x -= 20
be possible?
For now I used a quick function that does nothing else besides adding a value to posX for one line.
Downside is that I have to call this 14 times (number of lines), and takes a while to setup. Won't be practical with larger blocks.
 
Yooo I think I got you. You want to generate source code. Gimme a sec I'll cook something up with bash
 
Okay this turned out more challenging than I thought in bash, so here's the result using a python approach:
Code:
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2680" posY="-2645.1599121094" posZ="233.39700317383" rotX="0" rotY="312" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2660" posY="-2616.0129394531" posZ="210.02699279785" rotX="0" rotY="1" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2640" posY="-2632.96875" posZ="218.43699645996" rotX="0" rotY="335.99487304688" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2620" posY="-2650.1540527344" posZ="251.81100463867" rotX="0" rotY="288" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2600" posY="-2647.2219238281" posZ="270.71200561523" rotX="0" rotY="264" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2580" posY="-2636.8701171875" posZ="286.76300048828" rotX="0" rotY="240" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2560" posY="-2621.3010253906" posZ="296.90798950195" rotX="0" rotY="216" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2540" posY="-2602.5810546875" posZ="299.91799926758" rotX="0" rotY="192" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2520" posY="-2569.4689941406" posZ="283.11898803711" rotX="0" rotY="143.99993896484" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2500" posY="-2584.318359375" posZ="295.07800292969" rotX="0" rotY="167.99743652344" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2480" posY="-2560.876953125" posZ="266.33898925781" rotX="0" rotY="120" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2460" posY="-2559.8640136719" posZ="247.63900756836" rotX="0" rotY="96" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2440" posY="-2580.1201171875" posZ="216.18899536133" rotX="0" rotY="48" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2420" posY="-2566.6923828125" posZ="229.68899536133" rotX="0" rotY="71.998901367188" rotZ="270"></object>
    <object id="object(tmsramp)(1)" interior="0" collisions="true" doublesided="false" model="18784" scale="1" dimension="0" alpha="255" posX="2400" posY="-2598.0048828125" posZ="209.26899719238" rotX="0" rotY="24" rotZ="270"></object>

Is that what you were looking for? I put what you posted above in a text file called test and read it in the program.
Source:


Code:
#!/usr/bin/env python3
import re
contents = open('test', 'r').readlines()
value = 2680
dec = 20
for line in contents:
        print(re.sub('posX="[0-9]*"', 'posX="{}"'.format(value), line), end='')
        value -= 20

> tfw this forum has syntax highlighting for shitty languages like AppleScript and fucking INIs which don't even need any fucking highlighting
> but not for python
 
Looks like you keep lowering posX, but I actually want 1660 for the entire block, then another repeat of the entire block but with 2640 as value, then 2620, 2600,... So 20 iterations would be 20x15 = 300 lines

Thanks for helping btw!
And yeah seems pretty weird indeed.. idk why they didn't include Python.
 
See the attached file, is this what you wanted? it goes down all the way until 2280, so 300 lines in total.
Source:
Code:
#!/usr/bin/env python3
import re
contents = open('input', 'r').readlines()
value = 2660
dec = 20
while value > 2260:
    for line in contents:
        print(re.sub('posX="[0-9]*"', 'posX="{}"'.format(value), line), end='')
    value -= dec
 

Attachments

  • output.txt
    70.1 KB · Views: 14
See the attached file, is this what you wanted? it goes down all the way until 2280, so 300 lines in total.
Source:
Code:
#!/usr/bin/env python3
import re
contents = open('input', 'r').readlines()
value = 2660
dec = 20
while value > 2260:
    for line in contents:
        print(re.sub('posX="[0-9]*"', 'posX="{}"'.format(value), line), end='')
    value -= dec
Oi mate that's perfect! Now how do I run this.. :biggrin:
 
Oi mate that's perfect! Now how do I run this.. :biggrin:
Install Python 3.
Save the input (what you posted in the OP) as a file called input, make a second file called "script.py" and place the source code in there.
Open a shell, navigate to the folder and then
python3 script.py
OR, if you want the output to a file:
python3 script.py > output
 
Install Python 3.
Save the input (what you posted in the OP) as a file called input, make a second file called "script.py" and place the source code in there.
Open a shell, navigate to the folder and then
python3 script.py
OR, if you want the output to a file:
python3 script.py > output
Cheers!
 
Back
Top Bottom