Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,208 members, 7,953,767 topics. Date: Friday, 20 September 2024 at 04:56 AM |
Nairaland Forum / Science/Technology / Programming / Sorting Out A Simple Text Dictionary With PHP (6211 Views)
Sorting Out A Simple Text Dictionary With PHP by dhtml(m): 11:13pm On Jul 20, 2014 |
I was trying to make a facebook post some minutes back, and here was the content of the post. Now, i was faced with a real dilema, this stuff needs to be sorted out. I was about doing copy and paste, and then i remember I was a programmer, so i decided to give PHP the dirty work gbewiri - olee, thief 1 Like 1 Share |
Re: Sorting Out A Simple Text Dictionary With PHP by dhtml(m): 11:15pm On Jul 20, 2014 |
Ah well, after about 17 lines of PHP code approximately 2 - 3 minutes later, here is what i got: Not only was it sorted neatly, it also put the starting alphabet for each of the groups. [Aa] |
Re: Sorting Out A Simple Text Dictionary With PHP by dhtml(m): 11:16pm On Jul 20, 2014 |
The code in question: <?php |
Re: Sorting Out A Simple Text Dictionary With PHP by IamGodzilla: 11:21pm On Jul 20, 2014 |
nice! 1 Like |
Re: Sorting Out A Simple Text Dictionary With PHP by dhtml(m): 11:26pm On Jul 20, 2014 |
Thanks bro! 1 Like |
Re: Sorting Out A Simple Text Dictionary With PHP by Olyboy16(m): 1:51am On Jul 21, 2014 |
Hmm...so these are dhtml's php codes..i c..nice work |
Re: Sorting Out A Simple Text Dictionary With PHP by dhtml(m): 2:38am On Jul 21, 2014 |
Olyboy16: Hmm...so these are dhtml's php codes..i c..nice workYou got me laughing, are you trying to say their is something strange about my method of coding? I know i need to improve, thanks for the compliment though. |
Re: Sorting Out A Simple Text Dictionary With PHP by Nobody: 6:58am On Jul 21, 2014 |
In The quiet words of a beginner: "Nice one Bro".. |
Re: Sorting Out A Simple Text Dictionary With PHP by dhtml(m): 8:10am On Jul 21, 2014 |
Djangocode: In The quiet words of a beginner: "Nice one Bro"..Thanks bro. |
Re: Sorting Out A Simple Text Dictionary With PHP by logoscope(m): 12:31pm On Jul 21, 2014 |
Wow. cool. |
Re: Sorting Out A Simple Text Dictionary With PHP by boomtube: 12:45pm On Jul 21, 2014 |
logoscope: Wow. cool.Were you expecting like maybe a longer code? |
Re: Sorting Out A Simple Text Dictionary With PHP by Olyboy16(m): 12:47pm On Jul 21, 2014 |
dhtml: you deciphered my post already.. Now thats some real intelligence!! |
Re: Sorting Out A Simple Text Dictionary With PHP by boomtube: 1:23pm On Jul 21, 2014 |
Olyboy16:Thanks, i appreciate that. . .dont mind the pirated ID |
Re: Sorting Out A Simple Text Dictionary With PHP by logoscope(m): 7:23am On Jul 22, 2014 |
boomtube:Nah, not really. I was impressed with the way he used PHP to solve a simple problem. |
Re: Sorting Out A Simple Text Dictionary With PHP by asalimpo(m): 10:40am On Jul 22, 2014 |
Your code highlights design flaws of d php language. 1.) ambiguous data type syntax. Scalars and compounds (list,arrays) indistiguishable! $y = (get-file-content,"yoruba.txt" wat data type is $y? (cant say by lookng at the variable declaration. $lines = explode('\n',$y) $l = explode('-',$line') $lines is a compound type but u cant tell at declaration time?! Same Q applies to $l? If $l is an array wat datatype is $lines? arr[$l[0]] ok.? What kinda syntax is ths? count($l) this is for determg size of array, i guess. But i thought php was OO compliant. This kinda procedural abomination should have been deprecated from d language a long time ago.!! With facilities like this, php coders can spitout sloppy code ,part OO part procedural). |
Re: Sorting Out A Simple Text Dictionary With PHP by dhtmlextreme(m): 8:53pm On Jul 22, 2014 |
@asalimpo, this code has been tested in latest version of PHP and has no errors (in fact it should work in all versions of php 3,4,5). If you do not understand the PHP rules that are involved, well, I can attempt to explain. PHP/Javascript are different from C and Java where you need to declare a variable before use, and you dont have to stick to a particular type $y='Seun';100% correct php code. $y=trim(file_get_contents( => $y is created and assigned value at this point by the result of the function trim which is a string variable (standard php rule). As long as i do not attempt to use a variable such as y before declaration, i should never get an error in php Following the same principles: $lines = explode('\n',$y) => explode will split a string into an array, so $lines is created as an array and assigned the result $l = explode('-',$line') -> this will create an array of a particular line of text, the array has a key/value (word/meaning) - both key and value are strings. $l[0] will give the key of the array as explained above. arr[$l[0]] - will give the value of the array since array(key) is used count($l) will give the size of the array as you already guessed. As for your procedural abominations, i dont know what you are talking about. I write standard codes as a PHP expert (both oop and procedural when required), but this is a very simple code as far as i am concerned. 2 Likes |
Re: Sorting Out A Simple Text Dictionary With PHP by babatope88(m): 11:52pm On Jul 22, 2014 |
Oga plenty 4 here sha |
Re: Sorting Out A Simple Text Dictionary With PHP by Olyboy16(m): 2:56pm On Jul 23, 2014 |
asalimpo: Your code highlights design flaws of d php language. "they must know me too. I sabi dis tin smal".. *yimu* |
Re: Sorting Out A Simple Text Dictionary With PHP by dhtmlextreme(m): 8:15pm On Jul 23, 2014 |
Olyboy16: "they must know me too. I sabi dis tin smal".. *yimu*You can get banned for YIMUing on this board o, berra be careful. |
Re: Sorting Out A Simple Text Dictionary With PHP by Olyboy16(m): 9:58pm On Jul 23, 2014 |
[quote author=] You can get banned for YIMUing on this board o, berra be careful. [/quote] Are you serious or jst kidding? |
Re: Sorting Out A Simple Text Dictionary With PHP by asalimpo(m): 11:03pm On Jul 23, 2014 |
]@asalimpo, this code has been tested in latest version of PHP and has no errors (in fact it should work in all versions of php 3,4,5). If you do not understand the PHP rules that are involved, well, I can attempt to explain.the first issue i raised cud b ignored cuz php follows d trend of other dynamic languages (python,ruby,smalltalk,groovy) .the fields dont display type information . This is also d case with static languages too. Though, type information is specified at creation time in static languages. E.g string[] countries = ...//some code [] conveys type information about d field 'countries' . Som1 readg d code knows its an array , but later on in the code base he will need help to get this info. Tht's y most languages hav ide's. Countries = method1(); here, coder doesnt know what kind of field 'countries' is. Is it a scala,array,list,etc. Ide help needed. But since this is d issue with 99.9 percnt of most modern langs, its accepted custom. But, perl, a language tht inspires php appends type information to fields so tht at a glance a programmer can tell wat type (container type) of field he's dealg with. $ for scalas, @ for arrays and % for hashmaps. 2. ---- php allows the mixg of procedural and oop design styles in d same code base. oop is d more structured and disciplined approach. Ruby is purely oop, yet its dynamic. E.g count(list) - is a free floatg function, so is explode(string) etc. Programmers invoke these in built functions, get d results and go on their way. But because these functions work well as standalone entire code blocks cud b constructed purely from them, without any need for oop. Which creates a breed of quick and monolithic coders. The only modularity existg in situations like these is at d source file level. The files can b distributed. As far as php keeps makg structural codg an alternatv to oop , it will keep breedg quick coders who go straight to d point , without botherg oop concepts but who'll keep churng hard to maintain huge piles of monolithc code. Php shud come clean and break away from backward compatibility and make php a completely modular language. Put count() in a list class. Put explode() in a String /or StringUtility class. Make modularity at the design level instead of at d source file level. 1 Like |
Re: Sorting Out A Simple Text Dictionary With PHP by dhtmlextreme(m): 6:46am On Jul 24, 2014 |
Ah well, you are right in a way |
Re: Sorting Out A Simple Text Dictionary With PHP by cyrielo(m): 10:20am On Jul 24, 2014 |
asalimpo: Your code highlights design flaws of d php language.PHP is a dynamic and a flexible language, php converts data type of a variable based on your command, you can compare string against boolean or boolean against int php automatically figure what you really want to do at runtime. and it is also multi paradigm. you can choose to write pure OOP code or procedural |
Re: Sorting Out A Simple Text Dictionary With PHP by dhtmlextreme(m): 3:53pm On Jul 24, 2014 |
^^^that is a PHP expert speaking and talking |
Re: Sorting Out A Simple Text Dictionary With PHP by asalimpo(m): 3:43pm On Jul 25, 2014 |
cyrielo: the point you miss dear sir,is tht standardzation is a necessary evil. Mainstream programmg languages and software engineerg needs some form of standardzation, but standardzation usually stiffles innovation especially wen (as is often) the case an inferior technology gets standardzed. E.g in d days of video cassetes, vhs was d standard,though,betamax was technologically superior. In electrcity, DC, was d heavily touted standard but AC was better for electricity distribution. It took a lot of bitter fightg for AC to b adopted. Today, AC ,is used to get d power to ur house where it is stepped down to DC for ur electroncs to use. Light bulbs are built to a standard so tht u dnt care who d manufacturer is,u just use it outta d box to replace a dead bulb, usb interface has replaced former serial ports for device to computer connectvty. So watabout software? In software its long being discovered ,painfully, tht multi paradigm without a controllg paradigm is detrimental. C++ operated ths philosophy and d wrld is still hurtg so bad from it. In c++, speed was d essense, u cud do it in oop, structural,generically,or low level . You cud mix up paradigms as u like-the result. complexity explosion, hard to track bugs, memory leaks tht cost corporations $1b annually (as at wen java was born). Yeah c++ was fast, but it was disastrous. Lets not even talk about d perils of multiple inheritance and direct memory manipulation (wat language is d favrite for virus writers?) Java (inspired by Ada) discarded the no-holds bared philosophy of c++ for a Big brother philosophy. No, you cant run code outside a class, here , put ur bags in d locker, (variables in classes), and put d books in d bag (packages). The toilet seats go down after use (threadg). Evrythg goes in a class, and every student must register with 3 names.. (lotsa a rules). It was too restrctve. But almost 20 yrs after, the rules of java and making oop d dominant paradigm controlling its other paradigms has sustained it this far. Java now has 3 paradigms in it, oop,generic,and functional (as of java8). Yet all paradigms are expressed thru oop. Php has 2 paradigms oop,structural and it is repeatg the mistake c++ made. Givg programmers license to mix as they please,enforcg no structure. Result: it's so easy (reminds you of javascript right?!). Compare its contemporaries, ruby,python,clojure. Ruby enforces one paradigm- oop evrythg is an object. Python enfrces one paradigm-oop clojure enforces one paradigm- functional one paradigm means one option for structurg code wich means greater modularity. This is where php is failg. It has embraced oop but oop is just an add on. You can continue weavg more unstructred spaghetti wraps called code and pushg it to the server faster thn other more disciplined language practitioners can. But to d disciplined eye all he'll see is bad language design . |
Re: Sorting Out A Simple Text Dictionary With PHP by cyrielo(m): 4:48pm On Jul 25, 2014 |
^^^^ if you want some standardization ever heard of Model View Controller (MVC) code design depends on the programmer not just the language! And yeah i could hack away one main java class and build full application in procedural style without creating any additional class ! well every language has its pros and cons. and what do you mean by c++ being disastrous please tell me more |
Re: Sorting Out A Simple Text Dictionary With PHP by asalimpo(m): 2:03am On Jul 26, 2014 |
cyrielo: ^^^^ if you want some standardization ever heard of Model View Controller (MVC) code design depends on the programmer not just the language! And yeah i could hack away one main java class and build full application in procedural style without creating any additional class ! well every language has its pros and cons. and what do you mean by c++ being disastrous please tell me moresure you could hack one big monolithic method - procedural style - in java. but you're still forced to conform to the OOP paradigm. the method is in a class. and the class is the driver class. no getting around it. You can also create many unnecessary classes in the name of everything is an object mantra. c++ is disastrous cuz c++ enforces no paradigm. It assumes the dev knows what he's doing. because no paradigm is enforced, you can write structural code all the way, use global variables , go low level as you like. Some c coders crossing over to cpp , ditch the oop aspect of c++ and code in c++ completely in procedural style. No modularity. everything is just one procedure calling anoda procedure. Messy code. php follows in the footsteps of c++. |
(1) (Reply)
Ekiti Government To Train Youths In Programming / How Can I Practice Programming on My PC? / Different Ways To Earn $1000 Online Monthly
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 75 |