Jump to content

compare a variable to a list


 Share

Recommended Posts

i have been searching the forum for about an hour, and was wondering if someone could give me a link to a script or example of this... i thought i saw one before.

i need help :

checking a variable against a .txt file. and if the variable is the same as any line in the .txt file, then dont write it. if it doesnt match, then write it.

this is for taking duplicate lines out of a .txt file as its being populated.

thanks

~Todd

Edited by t0ddie

Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.

Link to comment
Share on other sites

i have been searching the forum for about an hour, and was wondering if someone could give me a link to a script or example of this... i thought i saw one before.

i need help :

checking a variable against a .txt file. and if the variable is the same as any line in the .txt file, then dont write it. if it doesnt match, then write it.

this is for taking duplicate lines out of a .txt file as its being populated.

thanks

~Todd

<{POST_SNAPBACK}>

Simple... write a function that contains two loops. One that loops through each entry and then one outside that which will get the string to compare it to.

POC:

OutsideLoop starts at first line and reads line by line
   Set tempVar1 = to the current line
   InsideLoop Starts at beginning of file and loops through file
      make sure the currentline for this loop is not the same as the line of the outside loop
      Set tempVar2 = to the current line
      If tempVar1 = tempVar2 then do something
   end InsideLoop
end OutsideLoop

There you go!

*** Matt @ MPCS

Link to comment
Share on other sites

k, digging through the help file...

_FileReadToArray("C:\Documents and Settings\TODDIE\Desktop\test.txt", $x )

MsgBox(4096, "Test", $x, )

cant get that to work.. it LOOKS right.. gives unknown function error

Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.

Link to comment
Share on other sites

this reads bob.txt, and does a comparision and then writes if not matching. Customise to your liking.

$filename="bob.txt"
$fullfile=StringReplace(Fileread($filename, FileGetSize($filename)),@lf,"")
$filelines=Stringsplit($filename,@CR); now each line is in an array

filedelete("secondfile.txt")
for $i= 1 to $filelines[0] ; $filelines[0]=the # of lines
if $fileline[$i]<>"whatever you had as a line" then filewriteline("secondfile.txt", $fileline[$i])
next

wrote from my head so might have missed a comma or somethin

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

i changed it slightly

$bak = clipget()

$filename="test.txt"

$fullfile=StringReplace(Fileread($filename, FileGetSize($filename)),@lf,"")

$filelines=Stringsplit($filename,@CR); now each line is in an array

for $i= 1 to $filelines[0]

if $fileline[$i]<> $bak then filewriteline("test.txt", $fileline[$i])

next

i get an error for line 7 saying that a variable was used without being declared?

Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.

Link to comment
Share on other sites

hopefully my last post on the subject lol

first, i want to say that the code isnt working as expected.. and could someone possibly help a noob out :)

secondly, i wanted to comment on the problem i had a couple days ago.. with filewriteline. i seem to have re-created the scenerio where i had the issue.

heres what i have right now.

;==============================================

$bak = clipget()

$filename="test.txt"

$fullfile=StringReplace(Fileread($filename, FileGetSize($filename)),@lf,"")

$fileline=Stringsplit($filename,@CR); now each line is in an array

for $i= 1 to $fileline[0]

if $fileline[$i] <> $bak then filewriteline("test.txt", $bak)

next

;==============================================

ok, this does not work,

i put 333 into my clipboard, and it wrote it to the file.. instead of omiting it since it already existed. i had tweaked it from before.. and it worked for not writing the variable if it was in the file... but if it wasnt in there.. it had created a file instead and named it the new variable. i had it ALL messed up lol. so this code is olmost done.. just needs to be slightly tweeked again lol. a little help here?

and this ALSO gives a perfect example of what i was talking about before VALIK...

about the blank space when first writing to a file. this is what was happening to me.

if you run that script above.. in this post... with something in the clipboard.. it will skip the first line you try to write to. (if file is empty, 1st line. if its full, last line)

i guess if the file doesnt exist.. it does not show this anomaly. it creates the file, and there is no blank space

this also happens ONLY when you run this script the first time. for example...

if test.txt does not exist, i run this script a FIRST time and it exists. and the first line contains whatever was in the clipboard.

i run it a SECOND time.. and it still functions normally. with the next line (line 2) being populated.

HOWEVER, if you create test.txt YOURSELF... and either leave it blank, or fill it up. doesnt matter. it still writes a blank time the first time you run the script before writing the line, instead of writing it to where the blank line is.

this is why i was so confused before. because this does not show this anomaly EVERY TIME with the SAME SCRIPT!

i had this in the file

1

22

333

4444

55555

666666

7777777

and when i ran this script with "hello" in the clipboard, i opened the .txt file and it looked like this

1

22

333

4444

55555

666666

7777777

hello

i then ran the script again, and it looked like this.

1

22

333

4444

55555

666666

7777777

hello

hello

do you see the "bug" im talking about now?

it should look like this when you write to the file... no matter what.

1

22

333

4444

55555

666666

7777777

hello

but for some reason, the first time you run the code it makes a blank space.

why is this happening?

Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.

Link to comment
Share on other sites

$filelines=Stringsplit($filename,@CR); now each line is in an array

for $i= 1 to $filelines[0]
if $fileline[$i]<> $bak then filewriteline("test.txt", $fileline[$i])

one little letter, welcome to the world of debugging...

$filelines=.....
if $fileline[$i].....

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

i figured that out at 2:30.. you replied at 3:01 scriptkitty lol

read above further.. i have ANOTHER question LOL in a few posts up. the BIG one

Edited by t0ddie

Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.

Link to comment
Share on other sites

Actually it looks like you want to write the clipboard data if the clipboard data is not in any line.

if so, you can do it a bit faster.

$clip=clipget()
$filename="bob.txt"
$fullfile=StringReplace(Fileread($filename, FileGetSize($filename)),@lf,"")
$fullfile=@cr & $fullfile; put @cr in front
if StringInstr($fullfile,@cr & $clip & @CR) =0 then filewriteline($filename,$clip)

I might be wrong about your intended use.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

thats EXACTLY what i want to do

you can actual understand what i want.. and thats kinda amazing. most people cant lol

even though i explain it clearly

i shall test this now. thanks

Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.

Link to comment
Share on other sites

watch this part:

if StringInstr($fullfile,@cr & $clip & @CR) =0 then filewriteline($filename,$clip)

might be > , but I think should be = for your app

Failure: Returns 0 if substring not found.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

Funny, after looking at your question, I need to go change my PHP script on my server. It will save a bit of log space for folks who keep hitting the save button too fast.

Same application basically, slightly different language. :)

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

well, you got the idea i want, but i have test.txt and it contains this

1

22

333

4444

55555

666666

7777777

and i copy

333

into the clipboard.

then i run this and.... i get

1

22

333

4444

55555

666666

7777777

333

so, its not still not working correctly lol

it DOES however fail to put anything from the clipboard that is NOT in the .txt file into the .txt file LOL

Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.

Link to comment
Share on other sites

watch this part:

if StringInstr($fullfile,@cr & $clip & @CR) =0 then filewriteline($filename,$clip)

might be > , but I think should be = for your app

<{POST_SNAPBACK}>

yep, that fixed it. now only one more small issue. it doesnt cr before writing. it does this

7777777sdfgsdfgsdfg

instead of this

7777777

sdfgsdfgsdfg

where do i add the extra cr?

Edited by t0ddie

Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.

Link to comment
Share on other sites

Try this, it should add a CR to the end if it doesn't have one.

$clip=clipget()
$filename="bob.txt"
$fullfile=StringReplace(Fileread($filename, FileGetSize($filename)),@lf,"")
$fullfile=@cr & $fullfile; put @cr in front
if StringInstr($fullfile,@cr & $clip & @CR) =0 then
; the quick @cr fix.
if StringRight ( $fullfile, 1 )<>@cr  then
 filewriteline($filename,@crlf & $clip)
else
 filewriteline($filename, $clip)
endif

edit... notice I actually added a @CRLF and not just a @CR. This will give better compatibility with windows programs.

Also note I messed up and changed my code so it now works. :)

Note to self...Some day I should test this instead of just typing from memory...

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

t0ddie, I don't know why you are getting the results that you are. When running your script, I do not have a blank line as the first line. And when I put the numbers in the file, this was my result (Which is what I would expect)

1

22

333

4444

55555

666666

7777777hello

Notice that hello isn't even on a line by itself. There are no newlines pre-pended (Running the script a second time will add hello on a new line, which would also be expected).

Just as a guess, I bet you are accidentally copying a blank line and the text hello into the clipboard. This would cause the behavior that you describe.

There is no bug here in any event. As I said before, your input data is very likely not what you are assuming it is. If you don't want to prepend a newline, ensure that the clipboard data doesn't start with @CR or @LF.

You aren't by chance clicking the line above hello in your editor, then dragging down and right to select it are you? This will cause a newline to be the first character(s) in the clipboard and will produce the result you want.

If this is in fact repeatable as you claim, then add this in place of FileWriteLine():

MsgBox(4096, "", "|" & $bak & "|")

If the data is correct, then it will look like this (Assuming your data is hello:

|hello|

However, if there is a newline at the beginning like I suspect, then it will look like this:

|

hello|

Link to comment
Share on other sites

i tested this outside the full script, and it seemed to be working fine.

but there is still a problem with the same names being copied into my .txt file.

i have worked pretty hard on this script for the most part, by myself. can someone possibly take a look at it and tell me what im doing wrong?

;===============THIS IS THE PART IM HAVING TROUBLE WITH=====

;CHECK LIST FOR DUPLICATES AND WRITE TO FILE IF NEW

$filename="ACCOUNTS.txt"

$fullfile=StringReplace(Fileread($filename, FileGetSize($filename)),@lf,"")

$fullfile=@cr & $fullfile

if StringInstr($fullfile,@cr & $bak & @CR) =0 then filewriteline($filename,$bak)

;===============THIS IS THE PART IM HAVING TROUBLE WITH=====

;THANKS

;Todd

Edited by t0ddie

Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.

Link to comment
Share on other sites

You never actually rewrite the file after you remove the similar lines.

Perform a FileWrite($filename, $fullfile) after $fullfile=@cr & $fullfile. I don't quite understand what you are trying to achieve with this script, but you will need to write it out sometime to the change file.

*** Matt @ MPCS

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...