Raised This Month: $32 Target: $400
 8% 

Solved Searching in a text file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
WHOOHOO
Junior Member
Join Date: Aug 2016
Old 08-21-2020 , 14:31   Searching in a text file
Reply With Quote #1

Okay, I already read a lot of articles about working with files in pawn and I'm sure it's out there somewhere but I'm frustrated of trying and failing at this so can someone help me please?
I have a text file and each line is consists of a map name, and a number, So it would be something like this:
Code:
de_dust2 123
de_inferno 444
de_nuke 12333
de_mirage 951
and the list goes on and on
I basically want to search for the map name and grab that number, Not sure how can I do that.

Last edited by WHOOHOO; 08-30-2020 at 08:24. Reason: I figured it by myself and I changed the title so if anyone had the same problem they could find it.
WHOOHOO is offline
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 08-21-2020 , 18:10   Re: Searching in a text file
Reply With Quote #2

Use a trie to get the entry ( the map name ), then all goes from itself: entry = value ( number of certain map ). You can use an iterator to find the entry, when the entry is equal with your search, stop the function and so on.
__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]
Shadows Adi is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-21-2020 , 22:48   Re: Searching in a text file
Reply With Quote #3

What part are you having issues with? You should provide the code that you tried so we can help you with your code.

There are several ways to do this and the best method will depend on how you are going to use the data. If you are looking up only for the current map and using that value for that map, you generally don't need to store the entire file in a variable. In this case, you would just loop through the lines of the file until your first argument matches the current map name and then you would grab the value that goes with it.

If all of the data is needed at any time, you should store all the data in variables in the plugin and then you'd look through that data to get what you need. There are several ways to do this, again depending on how you plan to use the data. Some are more complex than others.

Post your code and maybe explain how you're going to use the data so that we can help you out.

Quote:
Originally Posted by Shadows Adi View Post
Use a trie to get the entry ( the map name ), then all goes from itself: entry = value ( number of certain map ). You can use an iterator to find the entry, when the entry is equal with your search, stop the function and so on.
That is confusing and thus not really helpful, maybe worse than helpful.
__________________
fysiks is offline
WHOOHOO
Junior Member
Join Date: Aug 2016
Old 08-23-2020 , 08:48   Re: Searching in a text file
Reply With Quote #4

Quote:
Originally Posted by fysiks View Post
What part are you having issues with? You should provide the code that you tried so we can help you with your code.

There are several ways to do this and the best method will depend on how you are going to use the data. If you are looking up only for the current map and using that value for that map, you generally don't need to store the entire file in a variable. In this case, you would just loop through the lines of the file until your first argument matches the current map name and then you would grab the value that goes with it.

If all of the data is needed at any time, you should store all the data in variables in the plugin and then you'd look through that data to get what you need. There are several ways to do this, again depending on how you plan to use the data. Some are more complex than others.

Post your code and maybe explain how you're going to use the data so that we can help you out.



That is confusing and thus not really helpful, maybe worse than helpful.
Oh, I'm sorry for the confusion.
I'm trying to find the world record for the current map, It's for a kreedz server (I have them all stored in a file, the format is almost the same but I'll copy the whole thing so you can see for yourself)
And no, a trie would not be helpful since I don't need information about other maps, I just need to save the decimal time of the current map's world record in a variable so I'd be able to do stuff with it, for example show how much a player's record is slower than the WR.
And about my code, I failed to come up with anything that worked, I failed to figure out how that "Looping thru the data and searching for the first argument that matches up with current map" works.

Code:
8b1_brickngrass 229.39 Chasquido ar 642
8b1_hellinashop 127.44 topoviygus ru 665
ae_strafers_heaven 126.74 topoviygus ru 604
ak_rh_warehouse_ns 333.39 shooting-star cz 2
av_degyptianez 158.71 promax rs 539
bhop_its_2caves 324.36 fykseN cz 761
bkm_problock 146.9 topoviygus ru 588
The only part of my current code that is working is the part that I get the map name.
WHOOHOO is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-23-2020 , 14:45   Re: Searching in a text file
Reply With Quote #5

Well, in the first post you said "I already read a lot of articles about working with files in pawn" but I no longer believe that this is actually a true statement since you have not shown any effort to read a file.

I actually found one tutorial that does nearly exactly what you're wanting to do. The thread is here and you should look for the filereadtest() function that uses the "new file commands" (fopen, fgets, fclose).

If you are unable to get it working after implementing your code based on this example, attach your .sma file so that we can help you make it work.
__________________

Last edited by fysiks; 08-23-2020 at 14:58.
fysiks is offline
WHOOHOO
Junior Member
Join Date: Aug 2016
Old 08-27-2020 , 12:57   Re: /wr Plugin (Formerly: Searching in a text file)
Reply With Quote #6

Hey, I figured it out by myself and it seems like it's working so I just changed the post title so if anybody had the same problem they could read it and I hope that it could help them.

Code:
#include <amxmodx>
#include <amxmisc>
#include <ColorChat>

#define PREFIX "Kreedz"

new filename[256]
new currentmap[64], WRTime[32], WRHolder[32]
new bool:IsNAOrNot = true

public plugin_init() {
	register_plugin("Slash WR", "0.0.1", "WHOOHOO")
	format(filename,255,"addons/amxmodx/data/demos.txt",filename)
	get_mapname(currentmap, 64)
	register_clcmd("say /wr", "ShowWR")
	set_task(1.0, "ParseWR")
}

public ShowWR(id) {
	if(!IsNAOrNot) {
		new Float:WRTimer, WRimin, WRisec, WRims
		WRTimer = str_to_float(WRTime)
		WRimin = floatround(WRTimer / 60.0, floatround_floor)
		WRisec = floatround(WRTimer - WRimin * 60.0,floatround_floor)
		WRims = floatround( ( WRTimer - ( WRimin * 60.0 + WRisec ) ) * 100.0, floatround_floor )
		ColorChat(id,RED,"^04[%s] ^04World record on ^03%s ^4is ^03%i:%i.%i ^04by ^03%s.", PREFIX, currentmap, WRimin, WRisec, WRims, WRHolder)
	} else {
		ColorChat(id, RED, "^04[%s] There is ^03no world record ^04submitted for this map.", PREFIX)
	}
	
	return PLUGIN_HANDLED
}

public ParseWR() {
	new filepointer = fopen(filename,"r")
	if(filepointer) {
		new readdata[128], parsedcurrentmap[62]
		new parsedwrtime[32],parsedname[32],parsedcountry[32], parsedmapid[32]
		while(fgets(filepointer,readdata,127)) {   
			parse(readdata,parsedcurrentmap, 61, parsedwrtime,31, parsedname, 31, parsedcountry, 31, parsedmapid, 31)
			if(equal(parsedcurrentmap,currentmap)) {
				IsNAOrNot = false
				format(WRTime, 31, "%s", parsedwrtime)
				format(WRHolder,31, "%s", parsedname)
				break
			}
		}
		fclose(filepointer)
	}
	return PLUGIN_HANDLED
}
Oh, by the way I'm not sure if this is the smartest way to achieve this, Or if my code is credible at all, Just posting it so it may help someone.
I'd appreciate if someone could check it and see if I made any mistakes.
Attached Files
File Type: sma Get Plugin or Get Source (wr.sma - 51 views - 1.8 KB)
WHOOHOO is offline
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 08-27-2020 , 14:29   Re: /wr Plugin (Formerly: Searching in a text file)
Reply With Quote #7

Code:
format(filename,255,"addons/amxmodx/data/demos.txt",filename)
Don't use this way, it's very slow.
Code:
static szDataDir[64] /* Define array as static because you don't use it so much, so a plus in optimizing. */ static filename[64] /* Don't waste resources */ .... get_datadir(szDataDir, charsmax(szDataDir)); formatex(filename, charsmax(filename), "%s/demos.txt", szDataDir);

Code:
format(WRTime, 31, "%s", parsedwrtime) format(WRHolder,31, "%s", parsedname) -->> formatex(WRTime, 31, "%s", parsedwrtime) /* Using formatex() because you don't need copy-back and it's faster than format() ( 5 x faster if I'm not wrong ) */ formatex(WRHolder,31, "%s", parsedname)
__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]

Last edited by Shadows Adi; 08-27-2020 at 14:30.
Shadows Adi is offline
thEsp
BANNED
Join Date: Aug 2017
Old 08-27-2020 , 16:18   Re: /wr Plugin (Formerly: Searching in a text file)
Reply With Quote #8

Code:
/* Define array as static because you don't use it so much, so a plus in optimizing. */
1. That's not true at all.
2. This has been discussed for hundreds of times already.
thEsp is offline
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 08-27-2020 , 16:36   Re: /wr Plugin (Formerly: Searching in a text file)
Reply With Quote #9

Quote:
Originally Posted by thEsp View Post
1. That's not true at all.
https://wiki.alliedmods.net/Optimizi...atic_vs_Global
Code:
A variable declared with the keyword "static" instead of "new" operates in the same way a global does (it is created only once) but the variable is local to the function; this means the code is much easier to read, while drastically improving speed just like a global variable.
Quote:
Originally Posted by thEsp View Post
2. This has been discussed for hundreds of times already.
I know this has been discussed and the answers were same as the above one.
__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]

Last edited by Shadows Adi; 08-27-2020 at 16:37.
Shadows Adi is offline
thEsp
BANNED
Join Date: Aug 2017
Old 08-27-2020 , 17:10   Re: /wr Plugin (Formerly: Searching in a text file)
Reply With Quote #10

A "dynamic" variable will be freed at the end of the function whereas a static one when the script finishes running. There is no optimization whatsoever in this case, you're just keeping it uselessly.
thEsp is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 15:57.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode