PDA

View Full Version : how to split a string in log file


dieyana
02-03-2009, 04:47 AM
hiii...

could anyone help me.how i can split this string"i am new to this" using the white space delimiter.

and how should i declare this string before i can paste it in Flex?

thank u so much...

fx.barrett
02-03-2009, 04:51 AM
Huh, my English is not that good :D Could you maybe give a concrete example? How should the output look like and what do you mean by "how should I declare this string before I can paste it in Flex" ? Paste what into Flex ? Why ? and what do you understand by "paste into Flex" because you can "load data into Flex" or "pass data to Flex" but not "paste data"...

MontyCoder
02-03-2009, 05:30 AM
var myStr:String = "i am new to this";
var myArray:Array = myStr.split(" ");
var myNewStr:String=myArray[0]+"-"+myArray[1]+"-"+myArray[2];
trace(myNewStr);
//i-am-new

dieyana
02-04-2009, 01:44 AM
i'm sorry for the inconvenience...:eek:

ok,first: I got this line of codes in flex;
var queryStr:String = '127.0.0.1 - - [15/Sep/2008:10:37:14 -0700];
var params:Array = queryStr.split(" ");

i want to split them into array like this:127.0.0.1 [15/Sep/2008:10:37:14 -0700]
i know that i can use the "white space" as a delimiter in this case,but what i want to know is,where should i put the above codes?whether it should be in the <mx:script> or what??

second:do i have to declare an array before i can use this codes?

thank you so much in advance...;)

MontyCoder
02-04-2009, 05:13 AM
ok if this is ur string
var queryStr:String = '127.0.0.1 - - [15/Sep/2008:10:37:14 -0700]
than what u want on out output...?

dieyana
02-04-2009, 06:29 AM
i want to output this string "127.0.0.1 - - [15/Sep/2008:10:37:14 -0700]" into two columns using the split function.can it be done?

thanks...;)

fx.barrett
02-04-2009, 08:49 AM
Ever considered simply doing this ?

var st:String = "127.0.0.1 - - [15/Sep/2008:10:37:14 -0700]";
var arr:Array = st.split(" - - ");
trace("value1: " + arr[0] + "\n" + "value2: " + arr[1]);

wvxvw
02-04-2009, 09:09 AM
<?xml version="1.0" encoding="utf-8"?>
<!-- SplitExample -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
applicationComplete="applicationCompleteHandler()">
<mx:Script>
<![CDATA[
private function applicationCompleteHandler():void
{
var stringToSplit:String = "127.0.0.1 - - [15/Sep/2008:10:37:14 -0700]";
stringToSplit += " - - 127.0.0.1 - - [16/Sep/2008:10:37:14 -0700]";
stringToSplit += " - - 127.0.0.1 - - [17/Sep/2008:10:37:14 -0700]";
stringToSplit += " - - 127.0.0.1 - - [18/Sep/2008:10:37:14 -0700]";
stringToSplit += " - - 127.0.0.1 - - [19/Sep/2008:10:37:14 -0700] - - ";
var re:RegExp = /(?P<a>[^\2]+?)(\s\-\s\-\s)(?P<b>[^\2]+?)\2/g;
var result:Array;
do
{
result = re.exec(stringToSplit);
if (result)
{
trace(result.a);
trace(result.b);
sourceArray.push({ a: result.a, b: result.b });
}

}
while (result);
dp.refresh();
}
]]>
</mx:Script>
<mx:DataGrid width="100%" height="100%">
<mx:dataProvider>
<mx:ArrayCollection id="dp">
<mx:source>
<mx:Array id="sourceArray"/>
</mx:source>
</mx:ArrayCollection>
</mx:dataProvider>
<mx:columns>
<mx:DataGridColumn dataField="a"/>
<mx:DataGridColumn dataField="b"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>
But that would be much easier if you could pick up another delimiter.

EDIT: haha, oh, sorry, what was I thinking when I wrote that! :) Sure, just use Array.split(), what I've written is surely an overkill :)

fx.barrett
02-04-2009, 09:38 AM
^ Hahah, still, quite nice, nice. ;) I always skipped classes where they were talking about RegExp... but still, RegExp si really great and although I'm not great at it I still use it from time to time.

dieyana
02-04-2009, 11:41 PM
what you have done is really really great!!:)thanks a lot...it's really above my level..i'm just a beginner in exploring Flex.

u have solve my problem..just a little wonder when i use this code below;

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
var st:String = "127.0.0.1 - - [15/Sep/2008:10:37:14 -0700]";
var arr:Array = st.split(" - - ");

]]>
</mx:Script>
<mx:Label text="{arr}" id="myLabel"/>
</mx:WindowedApplication>

the output;

127.0.0.1,[15/Sep/2008:10:37:14 -0700]

why there is a "," there when i don't even define it in code??

wvxvw
02-05-2009, 07:59 AM
This is how Array.toString() works it joins the results with commas. You can alter it like this:
<mx:String id="foo"></mx:String>
<mx:Label text="The delimited string is:{foo} The result is: {arr.join(foo)}" id="myLabel"/>

fx.barrett
02-05-2009, 08:30 AM
Or you can manually build the desired string by adding items from the array together ( just like in the example I posted; maybe you'll run over a situation when you don't want to display the elements of the entire array at once ).