Page 1 of 1
Words from a phrase into Variables
Posted: 28 Aug 2011 08:55
by Ranguna173
Hello Everyone!!
Long time no see!
I have another question..
Is it possible to dived a phrase and save the words into variables?
Like this:
The Phrase
Now i would like a code to separate those two word and save them into two different variables:
Code: Select all
"Hello / everyone!"
set a=Hello ::the 1st word.
set b=everyone! ::the 2nd word.
cls
echo %a% %b%
pause
And You Would see
So.. That's all!
If you want more information just ask!
And please reply if you know something.
Re: Words from a phrase into Variables
Posted: 28 Aug 2011 10:41
by !k
Code: Select all
@echo off &setlocal enableextensions
set "phrase=Hello everyone! Hello! Helllooo!!1"
set /a word=0
call :loop "%phrase%"
set word
goto :eof
:loop
for /f "tokens=1,*" %%a in ("%~1") do (
set "word#%word%=%%a"
set /a word+=1
call :loop "%%b"
)
goto :eof
Re: Words from a phrase into Variables
Posted: 28 Aug 2011 15:26
by Ranguna173
!k wrote:Code: Select all
@echo off &setlocal enableextensions
set "phrase=Hello everyone! Hello! Helllooo!!1"
set /a word=0
call :loop "%phrase%"
set word
goto :eof
:loop
for /f "tokens=1,*" %%a in (%1) do (
set "word#%word%=%%a"
set /a word+=1
call :loop "%%b"
)
goto :eof
Everyone uses "for"...
Anyway, where are the a and the b variables?
I want to edit the words..
Re: Words from a phrase into Variables
Posted: 28 Aug 2011 15:47
by aGerman
Ranguna173 wrote:Everyone uses "for"...
What else? It's the best way I can think.
Ranguna173 wrote:Anyway, where are the a and the b variables?
Why a and b? If "phrase" has mor than 26 words you can't handle it. !k used %word#0%, %word#1%, ... instead.
In %word% you can find the number of words.
If your "phrase" has always 2 words you could use
Code: Select all
for /f "tokens=1,2" %%a in ("%phrase%") do (set "a=%%a" &set "b=%%b")
Regards
aGerman
Re: Words from a phrase into Variables
Posted: 28 Aug 2011 16:40
by Ranguna173
aGerman wrote:Why a and b?
It was a metaphor.. When i asked you where the a and b were I was referring to the variables in general..
aGerman wrote:!k used %word#0%, %word#1%, ... instead.
Okay.. I tried to use them but..
Code: Select all
C:\>test.cmd
word=4
word#0=Hello
word#1=everyone!
word#2=Hello!
word#3=Helllooo!!1
C:\>echo %word#1%
%word#1%
C:\>echo %word%
%word%
Am I doing something wrong?
I'm gonna use this for a encryption batch..
I write a notepad with the 1st line has:
Hello
Then i select the notepad with the batch and it encrypts it..
Output:
1001c00 0001101 0110100 0110100 100001b
Like that..
Re: Words from a phrase into Variables
Posted: 28 Aug 2011 17:24
by aGerman
It's because you didn't process the variables in the batch file (insert your ECHO commands into the batch code and it would work).
Delete " &setlocal enableextensions" to preserve the variables for the calling command prompt. I'm virtually certain you're working with "enabled extensions" by default (it's a registry setting).
Regards
aGerman
Re: Words from a phrase into Variables
Posted: 29 Aug 2011 06:51
by Ranguna173
aGerman wrote:Delete " &setlocal enableextensions" to preserve the variables for the calling command prompt.
Yeah.. I had to delete the "&setlocal enableextensions"..
Ok, all done!
But one last thing:
How do I split the characters in the word?
Sorry for being a pain in the ass, but the only thing i find in google is
this...
Re: Words from a phrase into Variables
Posted: 29 Aug 2011 09:23
by !k
Code: Select all
@echo off &setlocal enableextensions
set "word#1=everyone!"
call :ch "%word#1%"
set char
goto :eof
:ch
for /l %%c in (0,1,127) do (
set "w=%~1"
call set "char#%%c=%%w:~%%c,1%%"
)
goto :eof
By words
Code: Select all
@echo off &setlocal enableextensions
set "phrase=Hello everyone! Hello! Helllooo!!1"
set /a word=0
call :loop "%phrase%"
set word
pause
set /a words=word-1
for /l %%a in (0,1,%words%) do (
call :ch "%%word#%%a%%"
set char
pause
)
goto :eof
:loop
for /f "tokens=1,*" %%a in (%1) do (
set "word#%word%=%%a"
set /a word+=1
call :loop "%%b"
)
goto :eof
:ch
for /l %%c in (0,1,127) do (
set "w=%~1"
call set "char#%%c=%%w:~%%c,1%%"
)
goto :eof
All in Together
Code: Select all
@echo off &setlocal enableextensions
set "phrase=Hello everyone! Hello! Helllooo!!1"
set /a word=0
call :loop "%phrase%"
set word
pause
set /a words=word-1
for /l %%a in (0,1,%words%) do (
call :ch "%%word#%%a%%" word#%%a
)
set word
pause
goto :eof
:loop
for /f "tokens=1,*" %%a in (%1) do (
set "word#%word%=%%a"
set /a word+=1
call :loop "%%b"
)
goto :eof
:ch
for /l %%c in (0,1,127) do (
set "w=%~1"
call set "%2_char#%%c=%%w:~%%c,1%%"
)
goto :eof
Re: Words from a phrase into Variables
Posted: 29 Aug 2011 16:58
by Ranguna173
!k wrote:By words
All in Together
(the post was big so i replace the codes with "...")
Thank You !k !!
ALL Done!!
Now the only thing left to do is writing the encryption code and applying your code to mine....
Hell.. This is gonna take forever...
Re: Words from a phrase into Variables
Posted: 29 Aug 2011 17:03
by Ranguna173
Thank You aGerman and !k
You realy helped!!
People that have this question too can find the answerer on !k post.
Thank You!!