Variable updated by $put() has an old value inside a sub-loop

Hi!

I'm trying to export some data to a simple text file and have some trouble now.
I wannt to have some text alignment. I'm using $puts() to set some variables for additional calculaition.
A variable updated by $put() has an old value inside a sub-loop!

Well, there may be a simple solution but... here it is...
The difference is inside the $loop(%track%) loop at variable 'tw'.

$filename(txt)$loop(%_folderpath%,1)$loop(%artist%,1)%artist%
$repeat(-,$len(%artist%))
$loop(%album%)$loop(%title%)$puts(tw,$ifgreater($len(%title%),$get(tw),$len(%title%),$get(tw)))$loopend()
$puts(tlw,$add($get(tw),14))$puts(aw,$add(18,$add($len(%album%),$add($len(%_total_time%),$len(%_total_files%)))))
$puts(aw,$ifgreater($get(tlw),$get(aw),$get(tlw),$get(aw)))
$puts(tw,$ifgreater($get(aw),$get(tlw),$add($get(tw),$sub($get(aw),$get(tlw))),$get(tw)))
    %album% - Tracks: %_total_files% Time: %_total_time%
    $repeat(-,$get(aw))$loop(%track%)
    $num(%track%,2). %title%$repeat( ,$sub($get(tw),$len(%title%)))  -  %_length%$loopend()
$loopend()$puts(tw,0)$puts(aw,0)$puts(tlw,0)
$loopend()$loopend()

Between, the variable is updated outside the loop in the line:

...
$puts(tw,$ifgreater($get(aw),$get(tlw),$add($get(tw),$sub($get(aw),$get(tlw))),$get(tw)))
...

I don't know what to do now. Is there a logical error on my script?
I would appreciate any help!

01: $filename($getEnv('USERPROFILE')'\Desktop\Export.Whatever.txt',UTF-8)$loop(1,1)$puts(crlf,$char(13)$char(10))$puts(br,'{br}')$puts(tw,0)$puts(aw,0)$puts(tlw,0)$puts(ml,' ')$loopend()$replace(

02:
03: $loop(%_folderpath%,1)$replace(
04:
05: $loop(%ARTIST%,1)$replace(
06:
07: %ARTIST%
08: $get(br)$repeat('-',$len(%ARTIST%))
09:
10: $loop(%ALBUM%)$replace(
11:
12: $loop(%TITLE%)$puts(tw,$ifgreater($len(%TITLE%),$get(tw),$len(%TITLE%),$get(tw)))$loopend()
13:
14: $puts(tlw,$add($get(tw),14))$puts(aw,$add(18,$add($len(%ALBUM%),$add($len(%_total_time%),$len(%_total_files%)))))
15: $puts(aw,$ifgreater($get(tlw),$get(aw),$get(tlw),$get(aw)))
16: $puts(tw,$ifgreater($get(aw),$get(tlw),$add($get(tw),$sub($get(aw),$get(tlw))),$get(tw)))
17:
18: $get(br)$get(ml)%ALBUM%' - Tracks: '%_total_files%' Time: '%_total_time%
19: $get(br)$get(ml)$repeat('-',$get(aw))
20:
21: $loop($num(%TRACK%,2))$replace(
22:
23: $get(br)$get(ml)$num(%TRACK%,2)'. '%TITLE%$repeat(' ',$sub($get(tw),$len(%TITLE%)))' - '%_length%
24:
25: ,$get(crlf),,$get(br),$get(crlf))$loopend()
26:
27: ,$get(crlf),,$get(br),$get(crlf))$loopend()
28:
29: $puts(tw,0)$puts(aw,0)$puts(tlw,0)
30: $get(br)$get(br)
31:
32: ,$get(crlf),,$get(br),$get(crlf))$loopend()
33:
34: ,$get(crlf),,$get(br),$get(crlf))$loopend()
35:
36: ,$get(crlf),,$get(br),$get(crlf))

Example output ...

Madonna
-------
    Drowned World P1 - Tracks: 3 Time: 00:18:55
    ---------------------------------------------------------------------------------------
    01. Drowned World - Substitute For Love (Album Version)                        -  05:14
    02. Drowned World - Substitute For Love (BT & Sasha's Bucklodge Ashram Remix)  -  09:32
    03. Sky Fits Heaven (Sasha Remix Edit)                                         -  04:09

Madonna
-------
    Hung Up - Tracks: 3 Time: 00:15:37
    ------------------------------------------------------------------
    01. Hung Up (Radio Version)                               -  03:24
    02. Hung Up (Tracy Young's Get Up And Dance Groove Edit)  -  04:16
    03. Hung Up (SDP Extended Vocal)                          -  07:57

Mahmoud Ahmed
-------------
    ere mela mela - Tracks: 10 Time: 00:50:44
    -----------------------------------------
    01. sidetegnash negn - samiraye  -  05:34
    02. indenesh gedaow              -  03:47
    03. bemin sebeb litlash          -  04:34
    04. abay mado - imbwa belew      -  06:58
    05. atawurulign lela             -  03:59
    06. ohoho gedama                 -  04:45
    07. ere mela mela - meche neu    -  08:03
    08. fetsum dink lij nesh         -  04:32
    09. belomy benna                 -  03:57
    10. asheweyna                    -  04:35

Manhattan Transfer
------------------
    Extensions - Tracks: 10 Time: 00:44:34
    -----------------------------------------
    01. Birdland                     -  06:12
    02. Wacky Dust                   -  03:17
    03. Nothin' You Can Do About It  -  04:33
    04. Coo Coo U                    -  02:18
    05. Body And Soul                -  04:35
    06. Twilight Zone/Twilight Tone  -  05:21
    07. Trickle Trickle              -  02:24
    08. Shaker Song                  -  04:38
    09. Foreign Affair               -  03:56
    10. Unknown Title                -  07:20

DD.20140304.1119.CET