d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Javascript
Closed New Topic New Poll
Member
Posts: 12,356
Joined: Mar 24 2010
Gold: 500.00
Oct 29 2014 03:59am
Hello, i would like to get help from you guys.

For now i used autoit for making scripts that help me with my work.

I would liek to train some Javascript also.

I would like to make a script that will open file on my local computer, modify line of text and save it.

that would be base for my script but i would like to learn using it.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 29 2014 07:16am
Quote (blejdowy @ Oct 29 2014 04:59am)

I would liek to train some Javascript also.

I would like to make a script that will open file on my local computer, modify line of text and save it.


Do you know what Javascript is?
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Oct 29 2014 08:07am
JavaScript is normally for web based crap, you are looking a down to earth language. Since you're already using language that shall not be named, want to manipulate data, I think Python is the obvious choice. Here is something that demonstrates some terrible 5 minute hack you could do in python, I commented it so grandma can read it, though you probably won't need it.

Code
#!/usr/bin/env python3

"""

File.txt:
-----------------------------------------------------------------------------------------------
Ayy
Lmao -> ['Ayy\n', 'Lmao\n', 'Whatever']
Whatever
v
-to- heh()
v
Ayy
Lmao <- ['Ayy\n', 'Lmao\n', 'Lel']
Lel
-----------------------------------------------------------------------------------------------
"""

def heh(ayy):
""" do stuff to your data ;)"""
for off, dat in enumerate(ayy): # 0, line, 1, line, 2, line, etc.
if "Whatever" in ayy[off]: # find relevant line based on condition
ayy[off] = "Lel" # modify position in list with new data
return ayy



with open('file.txt', 'r') as file: # context manager for I/O - 'r' supports read/
data = list(file) # put each line into list - can also use file.read() and file.readline()
print(data)
wow = heh(data) # apply function `heh()` to reference `data` -> heh(data)
print('--After Change(s)--')
print(wow)
yah, yahornah = 'y', input('All good, y/n?')
if yahornah == yah:
with open('file.txt', 'w') as lel: # 'w' for write
for each in wow:
lel.write(each) # write out each in list to file


https://docs.python.org/3/tutorial/inputoutput.html
https://docs.python.org/3.0/library/fileinput.html

Should have used the second one

This post was edited by killg0re on Oct 29 2014 08:14am
Member
Posts: 770
Joined: Aug 28 2014
Gold: Locked
Oct 29 2014 11:05am
easy program ?
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 29 2014 01:57pm
Quote (Stealthmenace @ Oct 29 2014 12:05pm)
easy program ?


Wtf? You have 10 posts total. And this is one of them. I would probably gouge my eyes out if I read the other 9.

Of course its an easy program. Every single thing posted to this subforum is an easy program. Wanna know why? Because aside from the 4 or 5 people who are regulars here, everything that is posted is either by highschool stufents or freshmen in college trying to get help on their intro to programming homework assignments. If you want to see real-world applicable problems and solutions go look at something like stackoverflow.

Do you feel good about yourself knowing you can write a simple program that uses basic file io? Did it make you feel superior announcing to the world you can do this?
Go Back To Programming & Development Topic List
Closed New Topic New Poll