R – Extra Characters in Trace Function @#$%#$@%

actionscript-3flash

Can someone please tell me where these extra characters are coming from?

This is the output:

"MECHEL OAO ADS"
21.19
21.70
21.88
"+0.84%"
4736975

1

"MECHEL OAO ADS"
21.19
21.70
21.88
"+0.84%"
4736975

1

"MECHEL OAO ADS"
21.19
21.70
21.88
"+0.84%"
4736975

1

Notice the space and then the 1? Those should not be there…

Here's the script, should just plug and play…

var quoteTimer:Timer = new Timer(1000);
quoteTimer.addEventListener(TimerEvent.TIMER, getQuote);
quoteTimer.start();

var url:String = "http://www.hupcapstudios.com/projects/getDow.php";
var stockInfo:String = "s=MTL&f=nol1hp2v";

function getQuote(e:Event):void
{
    var variables:URLVariables = new URLVariables();
    variables.info = stockInfo;
    var urlRequest:URLRequest = new URLRequest(url);
    var stockLoader:URLLoader = new URLLoader();
    urlRequest.method = URLRequestMethod.POST;
    urlRequest.data = variables;
    stockLoader.load(urlRequest);
    stockLoader.addEventListener(Event.COMPLETE, addStock);
}

function addStock(e:Event):void
{
    var stockArray:Array = e.target.data.split(",");
    var assocArray:Array = new Array("Stock: " , "Open: ", "Current: ", "High :", "Percent Change: ", "Volume: ");

    for(var i:int=0;i<assocArray.length;i++)
    {
        trace(stockArray[i]);
    }
}

and the php…

<?php

$stock = require("http://download.finance.yahoo.com/d/quotes.csv?". $_POST['info']);

echo $stock;

?>

When I just run the php (or throw the url in the browser with the post"info" concatenated I don't get that extra space and 1 at the end…

***************** THE SOLUTION *****************

I was getting a "\n" in there, not sure why the 1 was being appended, but here is the code I'm using to make this all usable data (I'm seeking out more than one stock at a time otherwise eliminating the last bit space would have been no problem);

this is the URL to get the traced results of e.target.data:

http://download.finance.yahoo.com/d/quotes.csv?s=MSFT+GE+^N225+F+ET&f=noghl1p2vs

"Microsoft Corpora",N/A,N/A,N/A,26.71,"0.00%",0,"MSFT"

"GEN ELECTRIC CO",N/A,N/A,N/A,16.79,"0.00%",0,"GE"

"NIKKEI 225",10276.4,10216.14,10290.31,10257.56,"+0.18%",0,"^N225"

"FORD MOTOR CO",N/A,N/A,N/A,7.66,"0.00%",0,"F"

"ET",N/A,N/A,N/A,0.00,"N/A",N/A,"ET"

1

In order to make that usable I had to break it down into an array with a couple of loops…

the new addStock() function (with the results sent to a movie clips containing text fields for the data)…

function addStock(e:Event):void
{
    var array:Array = e.target.data.split("\n"); // splits the string into arrays delimited by the "\n"
    array.pop(); // eliminates the last array entry which is just white space
    var goodArray:Array = new Array(); // the array I will store the correctly formatted values

    for(var i:int=0;i<array.length;i++)
    {
        var s:String = array[i].substr(0,array[i].indexOf("\n")-1);
        var nArray:Array = s.split(",");

        for(var con:int = 0; con<nArray.length;con++)
        {
            goodArray.push(nArray[con]);
        }
    }

    var round:int = 0; // the number I need to offset the stocks information.



    for(var t:int = 0; t<stockCardsArray.length;t++)
    {
        var c:* = stockCardsArray[t]; // the array containing my movie clips with the text fields.

        c.stock_name.text = goodArray[0+round].substr(1,goodArray[0+round].length-2);

        c.open.text = "OPEN: " + goodArray[1+round];
        c.low.text = "LOW: " + goodArray[2+round];
        c.high.text = "HIGH: " +goodArray[3+round];
        c.current.text = "CURRENT: " + goodArray[4+round];
        c.percent.text = "PERCENT CHANGE: " + goodArray[5+round];
        c.volume.text = "VOLUME: " + goodArray[6+round];
        c.symbolTab.symbol.text = goodArray[7+round].substr(1,goodArray[0+round].length);

        round +=8;

    }
}

Best Answer

I've no idea why the characters are there, but I can offer a suggestion how to find out. :)

Run the Actionscript code in a debugger, with a breakpoint inside the addStock() handler. At runtime, inspect the contents of the last elements of stockArray[]. I'm willing to bet it's: "4736975\n1\n"

Why would it be that? Probably the PHP script adds the newlines and the 1. If you view the source of the resulting page, instead of viewing it in the browser, you'll see all the linebreaks as they really are. Remember that the browser collapses all whitespace unless specifically told not to.

Related Topic