mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Jan 2012
Posts: 305
Epic Offline OP
Pan-dimensional mouse
OP Offline
Pan-dimensional mouse
Joined: Jan 2012
Posts: 305
I propose to add a new switch "-i" for the "$read()" identifier, which, when reading lines of a file, will preserve all types of indentation, similar to a space, in the result response.

Example:

  1. 5th line of the file:    line   reading   test
  2. Command input: //echo -a $read(file.txt, i, 5)
  3. Return result:    line   reading   test



The need for this innovation arose after discussing the problem in another thread: #Post271814

Note: Most likely, something similar will need to be done for the variables in which such data will be stored.


🌐 http://forum.epicnet.ru 📜 irc.epicnet.ru 6667 #Code | mIRC scripts, help, discuss, examples
Joined: Sep 2003
Posts: 35
O
Ook Offline
Ameglian cow
Offline
Ameglian cow
O
Joined: Sep 2003
Posts: 35
This can be done with $fread()

Code
; $1 = filename, $2 = linenumber
alias readline {
  ; check file exists
  if (!$isfile($1)) return
  ; check for a valid line number.
  if ($2 !isnum 1-) return
  ; create a unique name
  var %name = $+(txt,$ticks)
  ; open text file
  .fopen %name $qt($1)
  ; check for errors
  if ($fopen(%name).err) goto error
  ; jump to line we want.
  .fseek -l %name $2
  ; check for errors
  if ($fopen(%name).err) goto error
  ; check for end of file
  if ($fopen(%name).eof) goto error
  ; get text line
  set -ln %txt $fread(%name)
  ; clean up
  :error
  .fclose %name
  ; return requested line. Keeping spaces etc..
  returnex %txt
}
alias indent_test {
  var %f = versions.txt, %line = 96
  ; line 96 is: [space][space][space]options.
  ; which is 11 characters
  echo Length should be: 11
  echo read: $len($read(%f,nt,%line))
  echo fopen: $len($readline(%f,%line))
  set -ln %ok $readline(%f,%line)
  echo fopen2: $len(%ok)
}

Joined: Jan 2012
Posts: 305
Epic Offline OP
Pan-dimensional mouse
OP Offline
Pan-dimensional mouse
Joined: Jan 2012
Posts: 305
Thank you for the valuable advice and interesting example. I will definitely try to use this method to solve problem with saving and displaying indents in lines.


🌐 http://forum.epicnet.ru 📜 irc.epicnet.ru 6667 #Code | mIRC scripts, help, discuss, examples
Joined: Jul 2006
Posts: 4,155
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,155
Note that only the ascii 32 space is affected, 'all types of indentation' is really just the space ascii 32.
Variables don't need to be supporting anything because %variables can already store leading, consecutive, and trailing spaces.
As demonstrated by Ook with the file handling solution, only /returnex is needed to return a string containing leading/consecutive/trailing spaces.

There's a small issue in the code above though @Ook, /set by default will store any form of trailing/consecutive/leading spaces... except for a single trailing space, but it has since been fixed via the -p switch.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel

Link Copied to Clipboard