Jump to content

need help with my first algorithm


Guest
 Share

Recommended Posts

Hello,

I wrote a my first complex(for me) code from 0 (the first time without copying anything!!) that what it is doing is looking in the file - "test.txt" for the sign "$" and If It found then it is opens a msgbox which say to me that it found the letter "$" and also writes me in what line It found it.

I wrote the algorithm to develop the basis for something I want to program.

The problem is - It does not write to me the numbers of the lines which It does not found this letter.

I tried everything possible to make it so it will write the thowse lines reach to result that almost working properly. but it was with lagy.

So here is the code that makes only a half of my action:

#Include
Local $y
$file = "test.txt"
$file_write = "the code(test).txt"
$nl = _FileCountLines($file)
for $x = 1 to $nl step 1
$nx = $x
Call("algo1")
Next
Func algo1()
for $i = 1 to 300 step 1
$line = FileReadLine($file,$nx)
$line = StringTrimRight($line, $i)
if $line = "" Then
$n = $i
$line = FileReadLine($file,$nx)
ExitLoop
EndIf
Next
for $l = 1 to $n step 1
$v = StringTrimRight($line, $n-$l)
$v = StringTrimLeft($v, -1+$l)
If $v = "$" Then
MsgBox(4096,"","Found $ at line: " & $nx)
EndIf
Next
EndFunc

To test it, create a test.txt And write a few lines(Much as you want. There is no limit) of this file.

But some lines will be with this letter.

* Put only one from this letter in each line you do it

The code i did not suitable to work with a line containing more than one from this letter("$").

Or he does (i did not tested it)

and this is not the goal.

Thank for the helpers :)

Edited by Guest
Link to comment
Share on other sites

#include <File.au3>
Local $y
$file = "test.txt"
$file_write = "the code(test).txt"
$nl = _FileCountLines($file)
for $x = 1 to $nl step 1
$nx = $x
Call("algo1")
Next
Func algo1()
for $i = 1 to 300 step 1
$line = FileReadLine($file,$nx)
$line = StringTrimRight($line, $i)
if $line = "" Then
$n = $i
$line = FileReadLine($file,$nx)
ExitLoop
EndIf
Next
for $l = 1 to $n step 1
$v = StringTrimRight($line, $n-$l)
$v = StringTrimLeft($v, -1+$l)
If $v = "$" Then
MsgBox(4096,"","Found $ at line: " & $nx)
Else
ConsoleWrite("Not this line: " & $nx & @CRLF)
EndIf
Next
EndFunc

I added consolewrite cuz msgbox would be endless

Edited by ileandros

I feel nothing.It feels great.

Link to comment
Share on other sites

Not working

How do I use what you added?

This is what almost worked

#include <File.au3>
Local $y
$file = "test.txt"
$file_write = "the code(test).txt"
$nl = _FileCountLines($file)
for $x = 1 to $nl step 1
$nx = $x
Call("algo1")
Next
Func algo1()
for $i = 1 to 300 step 1
$line = FileReadLine($file,$nx)
$line = StringTrimRight($line, $i)
if $line = "" Then
$n = $i
$line = FileReadLine($file,$nx)
ExitLoop
EndIf
Next
for $l = 1 to $n step 1
$v = StringTrimRight($line, $n-$l)
$v = StringTrimLeft($v, -1+$l)
If $v = "$" Then
MsgBox(4096,"","Found $ at line: " & $nx)
EndIf
if $l = $n then
MsgBox(4096,"","not in: " & $nx)
EndIf
Next
EndFunc

It is almost true! And I checked it out ..

But it falls after the second line ..

It's my only idea ..

Edited by Guest
Link to comment
Share on other sites

What do u mean??? This shows you a msgbox with the line and it writtes all the other lines in the console. What did u wanted to do?

I need also see msgbox for line that it does not found.

Link to comment
Share on other sites

#include <File.au3>
Local $y
$file = "test.txt"
$file_write = "the code(test).txt"
$nl = _FileCountLines($file)
for $x = 1 to $nl step 1
$nx = $x
Call("algo1")
Next
Func algo1()
for $i = 1 to 300 step 1
$line = FileReadLine($file,$nx)
$line = StringTrimRight($line, $i)
if $line = "" Then
$n = $i
$line = FileReadLine($file,$nx)
ExitLoop
EndIf
Next
for $l = 1 to $n step 1
$v = StringTrimRight($line, $n-$l)
$v = StringTrimLeft($v, -1+$l)
If $v = "$" Then
MsgBox(4096,"","Found $ at line: " & $nx)
Else
MsgBox(0,"","Didn't found in line: " & $nx)
EndIf
Next
EndFunc

Before trying to write an other algorith try seeing the help files or some tutorials.

ConsoleWrite command write the lines in the console which is the second window under the window you have ur script.

As i already said i didnt add a msgbox command because it would be endless.

Edited by ileandros

I feel nothing.It feels great.

Link to comment
Share on other sites

I know it's not useful .. But that's how I check that things are working.

What you gave me is no different than what I did alone.

The goal is that the msgbox with "Did not found in line" will not Appear again and again on the same line

It should appear once as the other msgbox.

I always reading the help file

Link to comment
Share on other sites

I made it!

It works in a way that even I can not understand although I did it all alone ..

It works in a special way (I do not understand it myself) .. :S

I wrote it through the tests ..

this Is what I wrote:

#include 
Local $y
$f = false
$c = 1
$file = "test.txt"
$file_write = "the code(test).txt"
$nl = _FileCountLines($file)
for $x = 1 to $nl step 1
$nx = $x
Call("algo1")
Next
Func algo1()
for $i = 1 to 300 step 1
$line = FileReadLine($file,$nx)
$line = StringTrimRight($line, $i)
if $line = "" Then
$n = $i
$line = FileReadLine($file,$nx)
ExitLoop
EndIf
Next
for $l = 1 to $n step 1
$v = StringTrimRight($line, $n-$l)
$v = StringTrimLeft($v, -1+$l)
If $v = "$" Then
$f = true
$c = $nx
MsgBox(4096,"","Found $ at line: " & $nx)
EndIf
if $nx > $c Then
$f = false
EndIf
if $l = $n and $f = false then
MsgBox(4096,"","not in: " & $nx)
EndIf
Next
EndFunc

I'll try to understand how it works ..

Question for those who understood how it works, what do you think? can it be dome in better way?

Link to comment
Share on other sites

  • Moderators

gil900,

I made it!

Well done! :thumbsup:

It works in a way that even I can not understand although I did it all alone ..

It works in a special way (I do not understand it myself)

Not so good - you need to understand how code works or else you will never progress. :(

And as you ask, it could be done a little more simply: ;)

#include <File.au3>
; Read the file into an array
Global $aLines
_FileReadToArray("test.txt", $aLines)
; Loop through the array looking for "$"
For $i = 1 To $aLines[0]
    If StringInStr($aLines[$i], "$") Then
        MsgBox(4096, "", "Found $ at line: " & $i)
    Else
        MsgBox(4096, "", "Not in: " & $i)
    EndIf
Next

Can you understand how this script works? Please ask if not. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

My idea was to create a variable $c that represents the last line number that was in the last round in the loop And variable $f that represents something else to use it the next round in the loop ..

The end goal was to create a "if" that occurs only in a particular case.

And it succeeded. This was the main difficulty (know how to create such if)

How is it for someone who don't knows shortcuts?

it is smart?

I did not understand your code

I use Google Translate for the post so forgive me if I wrote a few unintelligible things.

Edited by Guest
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...