I have a perl code which read csv file. It contains grid data which needs to be updated at the front end.
First, here is the perl code which reads data and formats it so that the data can be pushed to front end for display.
my #array;
for my $column ($csv->column_headers) {
my $json = encode_json([ map { $_->{$column} } #$data ]);
push(#array, "$json;");
}
The final data is the #array which is passed to front end javascript code. The contents of #array is as follows.
["1","2"]; ["dd","ddd"]; ["wow","cool"]; ["HOLD","HOLD"];
This data is actually 4 columns with column header names as Id, Name, Comment and type. All these data are bundled up together in #array and passed to Javascript.
var header=[];
header[0] = #array[0];
}
This code above displays the below output if I do a console.log(header[0]); It means it is displaying the first element of the array. but I want to display the first element's element.
["1", "2"]
whereas it should display below output.
["1"]
In short, I want to know how can I access array elements elements. I tried using below code but it didn't work. Can someone please suggest?
var header=[];
header[0] = #array[0][0];
I am ultimately trying to put this data in grid by using below code.
for (var i=0;i<row_cnt;i++){
var row={};
row["Id"]=Id[i];
row["Name"]=Name[i];
row["Comment"]=Comment[i];
row["type"]= type[i];
data[i]=row;
}
where Id[i] will corresponding to "1" in first loop and "2" in second loop. Similarly it will generate data for other columns. These are then assigned to rows and updated in grid.
As per matts suggestion, I edited the code like this
my $json = encode_json($data);
for my $column ($csv->column_names) {
push(#data_array, "var $column= $json;");
}
Now it displays below values at every cell of the grid.
[object Object]
Building on the answer to your previous question, I think you just need to swap out the loop at the end for this:
my $json = encode_json($data);
print "var data = $json;\n";
Related
I have a collection of $linesheetItems, now I need to loop these $linesheetItems inside a foreach loop and store a seasons array by using line sheet item's season code ($linesheetItem['season']). But according to my current code, it returns an empty array.
Code:
$seasons = [];
foreach($linesheetItems as $linesheetItem) {
$seasons = Season::where('code', $linesheetItem['season'])->get();
}
dd($seasons);
How to achieve this, and what are the modifications should I do to my code?
In your code, you are overriding the $seasons variable each time the loop runs. In order to add an item to an array you have to set $seasons[] = Season::where('code', $linesheetItem['season'])->get();. This will always push a new item into the array. If you want to have custom keys on the array, you can do $seasons['your-key'] = Season::where('code', $linesheetItem['season'])->get();
If I have an array, [Joe, John, Adam, Sam, Bill, Bob] and I want to try to add this to a new row by doing SpreadsheetApp.getActive().getSheetByName('Sheet4').appendRow([array]); , what happens is that the entire list of names goes into 1 cell. Is there a way to break this up so they file away into the same row, but different columns? I need to continue using appendRow however.
I get this:
But I really want to have it look like this:
var my2DArrayFromRng = datasheet.getRange("A:A").getValues();
var a = my2DArrayFromRng.join().split(',').filter(Boolean);
var array = [];
for (d in a) {
array.push(a[d]);
}
SpreadsheetApp.getActive().getSheetByName('Sheet4').appendRow([array.toString()]);
You are converting your array to a string before you post it which is causing your issue.
Do not use the array.toString() method inside append row. Instead just append the array as it is.
SpreadsheetApp.getActive().getSheetByName('Sheet4').appendRow(array);
I'm new to Swift. I can read data (many rows and columns of names and mailing addresses) from csv file format. I have several of these files, so I created a function just to read the files and extract the data into a multidimensional array(s) - names, addresses, city, state, country. I read each of the lines from the file and try to append it to multidimensional array but I get errors - either index out of range or file type mismatch. What's the best way to enable this. See code below.
func getMailing(fileName: String) -> ([[String]])? {
let totalList = 243
var tempList: [String] = []
var arrayList = [[String]]()
guard let path = Bundle.main.url(forResource: fileName, withExtension: "csv") else {
print("File Error")
arrayList = [[""]]
return (arrayList)
}
do {
// get mailing data from file
let content = try String(contentsOf:path, encoding: String.Encoding.utf8)
// separate each line entry
tempList = content.components(separatedBy: "\r\n")
for index in 0...totalList - 1 {
// get each line from list and post into an array
let singleLine = tempList[index].components(separatedBy: ",").dropFirst().prefix(5)
// store each line data into into a multidimensional array for easy retrieval
arrayList[index].append(singleLine)
}
}
return (arrayList)
} catch {
print("File Error")
arrayList = [[""]]
return (arrayList)
}
}
Based on the code you've shown, it looks like you're trying to change the values of two different empty arrays 243 times. You have a loop setup to iterate based on your totalList property, but where you got that value, I have no idea. It would be wise to determine that value programmatically if you can.
You're setting both tempList and arrayList as empty arrays:
var tempList: [String] = []
var arrayList = [[String]]()
But then you're going through a loop and trying to change the value of an entry that doesn't even exist, hence your index out of range error. You need to first add something to both these arrays, because right now they are empty. It's probably crashing the first time through the loop when you try to set singleLine to tempList[index].components(separatedBy: ",").dropFirst().prefix(5), because you're saying tempList[0].components(separatedBy: ",").dropFirst().prefix(5), while there isn't an entry for tempList at index 0 because it's still empty! If you're going to loop through an array, it's always wise to do it based on the count of the array, or at least a quick fix when you need to use an index from two different arrays:
// Get the maximum times you can iterate based on the lowest count from each array
let maxLoop = min(tempList.count - 1, arrayList.count - 1)
for index in 0...maxLoop {
// get each line from list and post into an array
let singleLine = tempList[index].components(separatedBy: ",").dropFirst().prefix(5)
// store each line data into into a multidimensional array for easy retrieval
arrayList[index].append(singleLine)
}
Now that little chunk of code above won't even go through the loop once, because both arrays are still empty. You need to somewhere take your mailing data and parse it so that you can populate tempList and arrayList
I am working on a database project, where I want to create an array as follows:
unsigned char* drillSize[][10] = {
{"0.3678", "23.222", "MN", "89000", ".000236", "678", "ZX", "8563", "LX", "0.678"},
{"0.3678", "23.222", "MN", "89000", ".000236", "678", "ZX", "8563", "LX", "0.678"},
.
.
.
//around 6000 rows }
I have been provided with this data in an Microsoft Word file. If I were to key in the data manually it might take weeks; is there a way to insert commas and inverted commas for each element by some means?
You can try it with regular expressions.
If your data is structured in any way like a csv file you can just import it into your program in some way and then slice it up with basic string functions.
In php I'd
$input = file_get_contents($myfile) //lets say this contains a text: 1,2,3,4,5
$sliced = explode(",",$input);
$myarray = null; //our variable
$output = "\$myarray = new array("; //creating a template for an array as a String
//some logic with the $sliced array if you need
...
$output .= implode(",",$sliced); //we put it back as string
$output .= ");"; //close the array
eval($output); //after this its in php's scope
print_r($myarray);
Basically that's what you need in a more complex form. If the text is not that structured you might need some regular expression library for C, but I'd recommend creating a text in Python, Perl or something which has more support and more flexible then copy the code manually back to C.
I'm trying to put a hash %listvol into an array #fileInfo in Perl.
#fileInfo = ($filename, $data, $index, \%listvol);
%listvol contains a list of volume: key = $vol, value = $vol.
The first $vol values are ABCDEF, then GFFFF, EEEAA - always different.
Then I put the array #fileInfo in the hash %listeAllFile:
$listeAllFile{$nameOfFile} = [#fileInfo];
Later I'm trying to get the hash %listvol without success. I'm using this code:
foreach $key (keys %listeAllFile) {
#tab = #{ $listeAllFile{$key} };
$filename = $tab[0];
%listvol = %{ $tab[3] };
foreach $vol (keys %listvol) {
print "\n vol is $vol for file $filename";
}
The file name is always different, so it is ok. But the value of the variable $vol is always the same, ABCDEF. It seems that I get get each time the same value.
Does anyone have an idea?
While you didn't include code to reproduce your problem, I'm fairly sure that the issue is that you're storing a reference to the same %listvol hash in each array.
When you change the contents of %listvol for the second entry, you're modifying the first entry at the same time. One way to fix that is to use {%listvol} instead of \%listvol. The former makes a shallow copy of the current contents of %listvol, just like [#fileInfo] makes a shallow copy of the current contents of #fileInfo.