How to create an array of character lines while reading a whole line from file in windows phone - windows-phone-7.1

fileReader = new StreamReader(new IsolatedStorageFileStream("textfiles\\easy.txt", FileMode.Open, filestorage));
var obj = App.Current as App;
var lines = fileReader.ReadToEnd();
obj.textfile = (string[])fileReader.ReadToEnd();
Getting an error
"cannot convert string to string[]" in obj.textfile =
(string[])fileReader.ReadToEnd();

If you want an array of lines from a single string, you can do
var arrayOfLines = lines.split("\n");

Related

How can I adapt an existing Google Apps Script to import CSV of same name?

I found this code for importing csv files from drive. I do this hourly, and always import the updated (only) copy in drive called "inv.csv", so I'm trying to adapt the code to not require this to be typed in every time
the exiting code is this:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var searchMenuEntries = [ {name: "Search in all files", functionName: "search"}];
var csvMenuEntries = [{name: "Load from CSV file", functionName: "importFromCSV"}];
ss.addMenu("Search Google Drive", searchMenuEntries);
ss.addMenu("CSV", csvMenuEntries);
}
function importFromCSV() {
var fileName = Browser.inputBox("Enter the name of the file in your Google Drive to import (e.g. myFile.csv):");
var searchTerm = "title = '"+fileName+"'";
// search for our file
var files = DriveApp.searchFiles(searchTerm)
var csvFile = "";
// Loop through the results
while (files.hasNext()) {
var file = files.next();
// assuming the first file we find is the one we want
if (file.getName() == fileName) {
// get file as a string
csvFile = file.getBlob().getDataAsString();
break;
}
}
// parseCsv will return a [][] array we can write to a sheet
var csvData = Utilities.parseCsv(csvFile);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// boom data to a sheet
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
What I've tried:
I thought to remove the variable fileName completely, and define searchTerm as a static string, making the first bit
function importFromCSV() {
var fileName = Browser.inputBox("Enter the name of the file in your Google Drive to import (e.g. myFile.csv):");
var searchTerm = "title = 'inv.csv'";
but I get this error:
TypeError: Cannot read property 'length' of undefined
I should have defined fileName, not searchTerm, as the former appeared later in the code.
var fileName = "inv.csv";

Base64 encode a string in Cloud Code

How do I base64 encode a string in cloud code? The value is for a HTTP Basic Auth.
I have tried the following two approaches and I had no success.
var string = 'AQXTTPmj-boT_yDEPQXg9ezIOIM7O:EMx6RLr8jF3S6YYo-X4bZ';
var buffer1 = new Buffer(string, 'base64');
var b3 = buffer1.toString('base64');
console.log(b3);
var string = 'AQXTTPmj-boT_yDEPQXg9ezIOIM7O:EMx6RLr8jF3S6YYo-X4bZ';
var encodeString = Base64.encode(string);
console.log(encodeString);
You send your string to the Buffer constructor and use toString method to convert it to base64 like this:
var string = 'AQXTTPmj-boT_yDEPQXg9ezIOIM7O:EMx6RLr8jF3S6YYo-X4bZ';
var buffer1 = new Buffer(string);
var b3 = buffer1.toString('base64');
console.log(b3);
Also make sure you put var Buffer = require('buffer').Buffer; on top of your main.js file.

Swift: How to add dictionary arrays to an array?

Can I add dictionary arrays (is this the correct term for a dictionary key holding multiple values?) to an array?
var dictionary = [String: [String]]()
var array = [String]()
var data1:String = "55a"
var data2:String = "95a"
var data3:String = "66"
var data4:String = "25"
var data5:String = "88b"
var data6:String = "#"
dictionary["3"] = [data1, data2, data3, data4, data5, data6]
var data7:String = "#"
var data8:String = "#"
var data9:String = "#"
var data10:String = "#"
var data11:String = "#"
var data12:String = "#"
dictionary["2"] = [data7, data8, data9, data10, data11, data12]
var data13:String = "100"
var data14:String = "101"
var data15:String = "102"
var data16:String = "103"
var data17:String = "104"
var data18:String = "105"
dictionary["1"] = [data13, data14, data15, data16, data17, data18]
I tried this:
array.extend([dictionary["1"], dictionary["2"], dictionary["3"]])
but there was an error "Cannot invoke 'extend' with an argument list of type '([[(String)?])"..
How do I add dictionary["1"], ["2"] & ["3"] accordingly into the array?
Your array type declaration is not correct. Please try below one
var array: [[String:[String]] = []
In case you are not interested in the order you might try:
array.extend(flatMap(dictionary.values, {$0}))
If order is important you might build your optionalArrays first:
let optionalArrays = [dictionary["1"], dictionary["2"], dictionary["3"]]
array.extend(flatMap(optionalArrays, {$0 ?? []}))
i.e. your dictionary returns an optional array, this causes the error you reported.
Hope this helps
If you wanted an array of arrays of Strings, you need to change your array's type to be [[String]], as the other answers said.
But, when getting values out of your dictionary, you shouldn't force unwrap! It may work for this example, but in the future you'll likely get into trouble with:
'fatal error: unexpectedly found nil while unwrapping an Optional
value'
You should check to see if a value exists in the dictionary for that key, using optional binding for example:
if let value = dictionary["1"] {
array.append(value)
}
// ...
Or, you could get all the values from your dictionary into an array like so:
let array = Array(dictionary.values)
If you actually did want an array of Strings, you could use flatMap:
let array = flatMap(dictionary.values) { $0 }
Your array variable must be an Array of Array with String elements.
Also don't forget to unwrap the values of the dictionaries by adding !.
Try this:
var dictionary = [String: [String]]()
var array = [[String]]()
var data1:String = "55a"
var data2:String = "95a"
var data3:String = "66"
var data4:String = "25"
var data5:String = "88b"
var data6:String = "#"
dictionary["3"] = [data1, data2, data3, data4, data5, data6]
var data7:String = "#"
var data8:String = "#"
var data9:String = "#"
var data10:String = "#"
var data11:String = "#"
var data12:String = "#"
dictionary["2"] = [data7, data8, data9, data10, data11, data12]
var data13:String = "100"
var data14:String = "101"
var data15:String = "102"
var data16:String = "103"
var data17:String = "104"
var data18:String = "105"
dictionary["1"] = [data13, data14, data15, data16, data17, data18]
array.extend([dictionary["1"]!, dictionary["2"]!, dictionary["3"]!])
Dictionary values are returned as optionals (thus indicating if a value exists for a key) so use the '!' to unwrap the values of each dictionary array (i.e. [dictionary["1"]!)
And as suggested in other answers change your array type as it currently defined as arrays of string rather then an array of dictionaries.

Property not found on String

So i am trying to make a word jumlbe sort of program but i am running into some difficulties with the function for shuffelling the words
So i tried:
function klikk(evt){
var i:int = 0;
var ordet:String = inntastetOrd;
var lengde:int = ordet.length;
var tall:int = 0;
var karakter:String;
for(i=0;i<ordet.length; i++)
{
tall = Math.random() *ordet.length;
karakter = ordet[tall];
ordet[i] = ordet[tall];
ordet[tall] = karakter;
}
But i am getting: "Error #1069: Property "some value" not found on String and there is no default value."
Thanks for help!
If you want to chose a letter in a String, you need to transform this String into Array with the split method.
For example:
var letters:String = "word";
trace(letters[2]); // Error #1069
var a:Array = letters.split(""); // w,o,r,d
trace(a[2]); // r

Actionscript ByteArray (from NFC) to String

I'm reading an NFC tag in my Adobe AIR mobile app. The data is read as a ByteArray, but I'm having difficulty pulling the full text. The sample text on the tag is "http://www.google.com"
Using this method, I get a portion of the String "http://www.goog", but not all of it. I'm assuming because each character is not a single byte:
private static function convertToString(byte_array : ByteArray) : String {
var arr : Array = [];
for (var i : Number = 1 ; i <= byte_array.bytesAvailable; i++) {
arr.push(byte_array.readUTFBytes(i));
}
var finalString : String = "";
for (var t : Number = 0; t < arr.length;t++) {
finalString = finalString + arr[t].toString();
}
return finalString;
}
I've also tried the method below, but it returns null:
bytes.readUTF();
I'm wondering if I need to convert the byteArray to a base64 string and then decode that. It seems like an extra step, but that's how I've done it before when sending data to/from a server using AMFPHP.
Thanks in advance for any input.
You could even simplify this code by simply calling
private static function convertToString(bytes:ByteArray):String {
bytes.position = 0;
var str:String = bytes.readUTFBytes(bytes.length);
return str;
}
This way you will read all contents of the bytearray in one single method call into your destination string.
Figured it out in the code below.
There were 2 errors, plus some cleanup:
private static function convertToString(bytes : ByteArray) : String {
bytes.position = 0;
var str : String = '';
while (bytes.bytesAvailable > 0) {
str += bytes.readUTFBytes(1);
}
return str;
}
the "bytesAvailable" property decreases as you read from the ByteArray. Here, I'm checking if the bytes > 0 instead of the length
the "readUTFBytes" method takes a length parameter (not position). Position is automatically updated as you read from the ByteArray. I'm passing in "1" instead of "i"

Resources