views:

93

answers:

3

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&amp;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;

    }
}
+1  A: 

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.

doihaveto
This is the correct answer. It's not the php that's delivering the "\n" it's the .csv that yahoo returns.
Jascha
is csv adding a `1` too?
Amarghosh
Jascha
grrrr...sorry, that didn't come through right again
Jascha
ok, I added the url to the question post, just above the trace results. (you have to copy and paste as the link truncated at the ^ character)
Jascha
A: 

I think I got it... I'll give the check mark to the ASCII whiz that can answer this for sure...

I'm assuming that the returned data has "\n" in it (not visible in the returned data)

I'm assuming this because I searched the return string for it and then returned a sub string minus the string data after the "\n"

var s:String = (e.target.data);
trace(s.substr(0,s.indexOf("\n")-1));

-J

Jascha
which means flash is indeed receiving a newline followed by '1'. So the problem is somewhere else.
Amarghosh
A: 

Add trace(e.target.data); in the beginning of addStock method to see the data send by php.

Although yahoo.finance.com link gave a correct csv file, pasting the url used by the URLLoader (http://www.hupcapstudios.com/projects/getDow.php?s=MTL&amp;f=nol1hp2v) on to the browser gave me

Missing Symbols List.
1

Just thought that might ring a bell to you.

Amarghosh
Jascha