Skip navigation
Currently Being Moderated

Getting Variables from data string

Sep 18, 2012 6:43 AM

Hi,

 

I'm posting and getting back data from an ASP page on a site.

 

The data returns using the following function:

 

 

var dataS3:String;
function handleComplete3(event:Event):void
{
     var loader3:URLLoader = URLLoader(event.target);
     dataS3 = loader3.data;
     trace(dataS3); 
}

 

The trace (showing the data received) looks like: score1=0.5244005132792953&score2=-0.27105341041929653

 

How do I get these 2 scores (score1 and score2), which appear to be part of a data string, into separate variables that I can then use as numeric values in later frames (round/add/subtract/multiply/divide them)? Is there a way to parse out the 2 variables for use in later frame maths? Would I declare var DataS3 differently or keep it as a string, then somehow convert the returning string to separate numbers or integers?

 

Any help appreciated.

 
Replies
  • Currently Being Moderated
    Sep 18, 2012 7:10 AM   in reply to saratogacoach

    Search Google using terms like "AS3 text file variables" and you should find what you need.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 18, 2012 10:07 AM   in reply to saratogacoach

    Typically you use objects you can loop on for this. You can also type cast as you're doing only when you need to rather than to create a variable just to use it.

     

    Your example re-written using an array and casting can be:

     

    function handleComplete3(event:Event):void

    {

         // make a bunch of var=value pairs

         var urlVars:Array = String(event.target.data).split("&");

     

         // S5

         trace(String(urlVars[0]).split("=")[1]); // prints string

         // S6

         trace(String(urlVars[1]).split("=")[1]); // prints string

     

         // now you want conversion, just convert in-line to numbers instead of assigning new vars

     

         // S7

         trace(Math.round(Number(String(urlVars[0]).split("=")[1]) * 1000) / 1000); // prints number

         // S8

         trace(Math.round(Number(String(urlVars[1]).split("=")[1]) * 1000) / 1000); // prints number

    }

     

    The basic point is you can use the data you receive directly and unless it's in a very intensively called function you can just perform multiple operation at the same time without saving out to extra variables.

     

    You're allowed to daisy chain and cast types as you go. For example I didn't NEED to split the string by an &ampersand into a temporary array, I did it for ease of reading. To print your S7 I could have did this single line of code:

     

    function handleComplete3(event:Event):void

    {

        // traces your S7

        trace(Number(Math.round(Number(String(String(event.target.data).split ("&")[0]).split("=")[1]) * 1000) / 1000) );

    }

     

    There's no difference between the S7's being printed. You assigned a bunch of variables and cast them one by one. I anonymously casted them daisy-chain style without assigning any variables working directly on the data. I just wanted to point out the daisy chaining power of ActionScript. You can convert things as you go and condense code and dynamic variable usage quite a bit.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 18, 2012 11:29 AM   in reply to saratogacoach

    No shortage of tutorials if you google "AS3 bar chart tutorial". Some animated, some reading from XML, etc etc. Pick your flavor. If you have any other questions just open a new topic and fire away. Good luck!

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 19, 2012 7:53 AM   in reply to saratogacoach

    In those tutorials the XML is providing the data for the bar charts, just the numeric value. You'd just remove all that XML code and supply your own numeric values. Otherwise the bar chart code would be the same.

     

    e.g.

     

    someBar.scaleY = int(_xml.Somenode[i].someValue * someMathValue / etc);

     

    to

     

    someBar.scaleY = myValue;

     

    You're still adjusting the same property of the bar on the chart, the size of it (whether it be height, scaleY, etc), you just erase the XML code and supply your own value instead. The tutorial should specify what the value expects which will either be a height in pixels or a scale value between -1 and 1. It's up to you to perform the math to convert the number correctly. Feel free to post any questions on that process.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 19, 2012 8:30 AM   in reply to saratogacoach

    You're welcome and good luck!

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points