Getting a specific line from a file in Haxe

Showing 1-9 of 9 messages
Getting a specific line from a file in Haxe Andy Green 11/12/12 7:34 PM
In Haxe, is it possible to obtain a specific line from a file (using one of the standard classes, such as Sys)?

static function getMethodName(lineNumber, fileName){
//Is there a method that does this?
}
Re: [haxe] Getting a specific line from a file in Haxe JLM 11/12/12 8:41 PM
You can with trace...
And also with macros but unsure of details.
Re: Getting a specific line from a file in Haxe David Peek 11/12/12 9:28 PM
static function readLine(path:String, line:Int):String
{
if (line < 1) throw "Can't read line " + line;
var out = sys.io.File.read(path);
for (i in 0...line - 1) out.readLine();
var line = out.readLine();
out.close();
return line;
}
Re: [haxe] Re: Getting a specific line from a file in Haxe Mihail Ivanchev 11/13/12 12:02 AM
A bit hacky and slow, but quick:

File.getContent(path).split("\n")[lineNumber - 1];

I am using this code in exactly the same case as you, processing CTAGs info and generating code stubs.

Regards,
Mihail
Re: [haxe] Getting a specific line from a file in Haxe Andy Green 11/13/12 6:37 AM
Is it really possible to print the lines from a file using the trace method alone? I don't see anything in the documentation that suggests that.
Re: [haxe] Getting a specific line from a file in Haxe Mihail Ivanchev 11/13/12 6:40 AM
It's not possible.
Re: [haxe] Getting a specific line from a file in Haxe JLM 11/13/12 7:07 AM
Sorry Mihail your wrong, it is possible I have done it lots of times, it was in the first haxe book, but never really thought about it.
Just double checked with js target... 

Read the page carefully ( especially the last part ) and then add this to your Main class....


    static function traceComment( comment : String, ?pos : haxe.PosInfos )
    {
        haxe.Log.trace( comment + " :: Line no " + pos.lineNumber );
    }


Then test...


96        Main.traceComment( 'this is line 96' );

It will trace out...

"this is line 96 :: Line no 96"

Best 

Justin


On 13 Nov 2012, at 14:40, Mihail Ivanchev wrote:

It's not possible.


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
 
 
 

Re: [haxe] Getting a specific line from a file in Haxe Mihail Ivanchev 11/13/12 7:15 AM
Oh yeah, Justin, it's possible to print the line of the trace in the source file, of course, you're 100% correct, but it's not possible to load an external file, navigate to a line, and print that line:

trace("myfile.txt", 75);
Re: [haxe] Getting a specific line from a file in Haxe JLM 11/13/12 7:33 AM
Oh sorry missed the meaning of the question... oops.

Well you need to load it in and have a callback for loaded, and put the characters in a string then something like fileString.split('\n').[75];   or \r\n or \r.

I have parsed CSV files in haxe flash lots of times.