Hoopy frood
Joined: Oct 2004
Posts: 8,061 |
Why in the world wouldn't you use a better method of saving the data? An INI file or hash table would cut the script down to only 10 or so lines. Anyhow, here.
alias findinfo {
if ($1 !isnum) { echo -a ERROR: Invalid format. | return }
var %Section = $read(Data.txt,w,$chr(91) $+ $1 $+ $chr(93))
if (%Section == $null) { echo -a ERROR: That section number is not available. | return }
var %Data = $read(Data.txt,$calc($readn + 1))
while (: isin %Data) {
var % $+ $gettok(%Data,1,58) $gettok(%Data,2-,58)
var %Data = $read(Data.txt,$calc($readn + 1))
}
if ($2) {
var %cnt = 2
while (%cnt <= $0) {
var %Output = %Output $(% $+ $($ $+ %cnt,2),2)
inc %cnt
}
return %Output
}
else {
return $1 $+ $iif(%Nickname,$chr(44) $+ $v1) $+ $iif(%Address,$chr(44) $+ $v1) $+ $iif(%Channel,$chr(44) $+ $v1)
}
}
alias writeinfo {
if (!$1) { echo -a ERROR: Invalid format. | return }
var %cnt = 1
if ($exists(Data.txt)) {
filter -ffcu Data.txt Data.tmp [*
while ($read(Data.tmp,w,$chr(91) $+ %cnt $+ $chr(93))) {
inc %cnt
}
.remove Data.tmp
}
write Data.txt $+([, %cnt ,])
write Data.txt Nickname: $1
if ($2) { write Data.txt Address: $2 }
if ($3) { write Data.txt Channel: $3 }
}
alias delinfo {
if ($1 !isnum) { echo -a Invalid format. | return }
var %Section = $read(Data.txt,w,$chr(91) $+ $1 $+ $chr(93))
if (%Section == $null) { echo -a ERROR: That section number is not available. | return }
if (!$2) {
write -dl $readn Data.txt
while (: isin $read(Data.txt,$readn)) {
write -dl $readn Data.txt
}
}
else {
var %Data = $read(Data.txt,$calc($readn + 1)), %line = $readn
while (: isin %Data) {
if ($istok($2-,$gettok(%Data,1,58),32)) {
write -dl %line Data.txt
}
else inc %line
var %Data = $read(Data.txt,%line)
}
}
}
alias replaceinfo {
if ($1 !isnum) { echo -a Invalid format. | return }
var %Section = $read(Data.txt,w,$chr(91) $+ $1 $+ $chr(93))
if (%Section == $null) { echo -a ERROR: That section number is not available. | return }
var %Data = $read(Data.txt,$calc($readn + 1))
while (: isin %Data) {
if ($2 == $gettok(%Data,1,58)) {
write -l $readn Data.txt $2 $+ : $3-
}
var %Data = $read(Data.txt,$calc($readn + 1))
}
}
Fully tested. USES: N = An item number Something = Nickname,Address,Channel,Anything else Type = The name of the type of information (i.e. Nickname) TypeData = The data for the specific Type (basically the same as Something) $findinfo(N,Something) Displays the information for Something on item N$findinfo(N,Something,Something) Displays the information for Something and Something on item NNote that you can put the "Something's" in any order you want to depending on how you want them to appear.$writeinfo(Nickname,Address,Channel) Writes the information to the file in the first unused section #.You don't have to include everything... $writeinfo(Nickname) will work... but you do need to put those into the correct order and cannot skip something... $writeinfo(Nickname,Channel) is invalid.
If you want to be able to write the items in any order, with or without certain data, and/or add additional types of data, this can be adjusted to use a format of $writeinfo(Type,TypeData,Type,TypeData,Type,TypeData,Type,TypeData) ... basically, include the type before the data for that type. If you need that, just ask.$delinfo(N) Delete all information for section N$delinfo(N,Something) Delete only Something from section N$replaceinfo(N,Type,TypeData) Replace the existing data for Type with the new TypeDataYou cannot replace multiple types with the same $replaceinfo. You'll have to call it multiple times if you want to replace more than one Type's data at a time.Any questions, just ask.
|