You are not logged in.

#1 10 May 2007 13:15

NDog
Member
From: New Zealand
Registered: 31 May 2006
Posts: 121
Website

tighten up some code

This code which took me all day to write since I am still learning mad is working fine, but I am curious if it can be tightened up a bit? Everytime I change it seems to get a bug sad

These are 2 separate batch files. I would like to make it only 1 however.

It loads this text file and strips the first digits representing the international dialing code and strips the + GST as well. It then outputs the country name and rate to a seperate variable

talk4less.txt
54 Argentina Mobile 30 + GST
374 Armenia 20 + GST
297 Aruba 30 + GST
297 Aruba Mobile 40 + GST
247 Ascension 90 + GST
61 Australia 2.7 + GST
61 Australia Mobile 25 + GST
6114 Australia Optus Satellite Mobile 350 + GST
6721 Australian Antarctic Bases 175 + GST

FOR /F "tokens=1*" %%g in (talk4less.txt) do (
 FOR /F "delims=+ tokens=1-2" %%i in ("%%h") do (
  FOR /F "tokens=1-5" %%j in ("%%i") do (
   echo %%i>>nathan.txt
  )
 )
)
FOR /F "tokens=1-6" %%g IN (nathan.txt) DO (
if "%%i"=="" set rate=%%h&set country=%%g
set ALL_!country!=!rate!
)

FOR /F "tokens=1-6" %%g IN (nathan.txt) DO (
if "%%j"=="" set rate=%%i&set country=%%g %%h
set ALL_!country!=!rate!
)

FOR /F "tokens=1-6" %%g IN (nathan.txt) DO (
if "%%k"=="" set rate=%%j&set country=%%g %%h %%i
set ALL_!country!=!rate!
)

FOR /F "tokens=1-6" %%g IN (nathan.txt) DO (
if "%%l"=="" set rate=%%k&set country=%%g %%h %%i %%j
set ALL_!country!=!rate!
)

Last edited by NDog (10 May 2007 13:21)


cmd, vbs, ps, bash
autoit, python, swift

Offline

#2 10 May 2007 20:01

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: tighten up some code

It will be easier to work with if you first swap around the columns

30 Argentina Mobile
20 Armenia
...

something like this (untested)

FOR /F "delims=+ tokens=1-2" %%G in (talk4less.txt) do (call :mysub %%G line_end)
goto :eof

:mysub
set _location=
:check
if "%2"="line_end" echo %1 %_location% >>demo.txt & goto:eof
set _location=%_location% %1
shift
goto check

Offline

#3 10 May 2007 20:23

NDog
Member
From: New Zealand
Registered: 31 May 2006
Posts: 121
Website

Re: tighten up some code

Im sorry I am not familiar with what you are doing. I see you are using a goto? command but I am not sure how it goes back into the for /f loop

This had an error message ="line_end" was unexpected at this time. Since I am not familiar with these subroutines I am not sure how to debug this sorry



D:\>FOR /F "delims=+ tokens=1-2" %G in (talk4less.txt) do (call :mysub %G line_end )

D:\>(call :mysub 93 Afghanistan 40  line_end )

D:\>set _location=
="line_end" was unexpected at this time.
D:\>if "Afghanistan"="line_end" echo 93  >>demo.txt & goto:eof
D:\>


cmd, vbs, ps, bash
autoit, python, swift

Offline

#4 10 May 2007 21:18

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: tighten up some code

Ah the line should have been this

if "%2"=="line_end" (echo %1 %_location% >>demo.txt & goto:eof)

The first FOR command is CALLing the rest of the script so it returns when it hits the goto:eof part

Offline

Board footer

Powered by