Getting an unknown parse error with flot - flot

Can someone please point out what I'm missing here? I'm trying to generate a flot chart with the following function (which is called on success of the $.getJSON function in the page). At the moment, the chart is not loading at all and I get a parse error. If I removed this function, the parse error disappears, so I'm sure it's this function causing the problem but I can't see where!
function plotLineGraph(theData){
var myData = theData['data'];
var myDates = theData['dates'];
var d1 = [
myData[0], myData[1], myData[2], myData[3], myData[4],
myData[5], myData[6], myData[7], myData[8], myData[9],
myData[10], myData[11], myData[12], myData[13], myData[14],
myData[15], myData[16], myData[17], myData[18], myData[19],
myData[20], myData[21], myData[22], myData[23], myData[24],
myData[25], myData[26], myData[27], myData[28], myData[29]
];
$.plot($("#placeholder"), [
{label: "rating", data: d1}
], {
series: {
lines: { show: true },
points: { show: true }
},
xaxis: {
ticks:[
myDates[0], myDates[1], myDates[2], myDates[3], myDates[4],
myDates[5], myDates[6], myDates[7], myDates[8], myDates[9],
myDates[10], myDates[11], myDates[12], myDates[13], myDates[14],
myDates[15], myDates[16], myDates[17], myDates[18], myDates[19],
myDates[20], myDates[21], myDates[22], myDates[23], myDates[24],
myDates[25], myDates[26], myDates[27], myDates[28], myDates[29]
]
},
yaxis: {
ticks: 5,
min: 0,
max: 5
},
grid: {
backgroundColor: { colors: ["#fff", "#eee"] }
}
});
}
EDIT:
The $.getJSON function returns the following JSON:
{
"data": [[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0], [10, 0], [11, 0], [12, 0], [13, 0], [14, 0], [15, 0], [16, 0], [17, 0], [18, 0], [19, 0], [20, 0], [21, 0], [22, 0], [23, 3], [24, 3], [25, 2], [26, 0], [27, 0], [28, 0], [29, 0], [30, 0]],
"dates": [[1, '10/9'], [2, '11/9'], [3, '12/9'], [4, '13/9'], [5, '14/9'], [6, '15/9'], [7, '16/9'], [8, '17/9'], [9, '18/9'], [10, '19/9'], [11, '20/9'], [12, '21/9'], [13, '22/9'], [14, '23/9'], [15, '24/9'], [16, '25/9'], [17, '26/9'], [18, '27/9'], [19, '28/9'], [20, '29/9'], [21, '30/9'], [22, '1/10'], [23, '2/10'], [24, '3/10'], [25, '4/10'], [26, '5/10'], [27, '6/10'], [28, '7/10'], [29, '8/10'], [30, '9/10']]
}
EDIT 2:
Thanks Brendan. The call is here (it is part of a larger function which uses the same syntax to correctly generate a few other pie charts):
$.getJSON('/last-month-daily-average-for-products/'
+ theProductUuid + '/', plotLineGraph);
As for the disassembling/reassembling of the JSON.... No particular reason, no. All I need to do is get the relevant data from the JSON into the correct parts of the flot function, so feel free to correct anything which is inefficient as well as incorrect!!

There's nothing wrong with any of the code you have provided. It works perfectly.
See it in action here: http://jsfiddle.net/ryleyb/eJ2h8/

Related

how yo get the torch.mode 's output indeces , can details

>>> b
tensor([[ 6, 7, 12, 7, 8],
[ 0, 1, 6, 1, 2],
[ 0, 1, 6, 1, 2],
[ 2, 3, 8, 3, 4],
[ 2, 3, 8, 3, 4],
[ 2, 3, 8, 3, 4],
[10, 11, 16, 11, 12],
[-1, 0, 5, 0, 1],
[-2, -1, 4, -1, 0],
[ 2, 3, 8, 3, 4],
[ 1, 2, 7, 2, 3],
[ 1, 2, 7, 2, 3],
[ 2, 3, 8, 3, 4],
[ 5, 6, 11, 6, 7],
[-2, -1, 4, -1, 0],
[-3, -2, 3, -2, -1],
[-5, -4, 1, -4, -3],
[ 1, 2, 7, 2, 3],
[12, 13, 18, 13, 14],
[-3, -2, 3, -2, -1],
[ 2, 3, 8, 3, 4],
[ 3, 4, 9, 4, 5],
[10, 11, 16, 11, 12],
[-6, -5, 0, -5, -4],
[ 9, 10, 15, 10, 11],
[12, 13, 18, 13, 14],
[-3, -2, 3, -2, -1],
[-2, -1, 4, -1, 0],
[-4, -3, 2, -3, -2],
[-1, 0, 5, 0, 1],
[ 2, 3, 8, 3, 4],
[ 4, 5, 10, 5, 6],
[-1, 0, 5, 0, 1],
[ 5, 6, 11, 6, 7],
[ 7, 8, 13, 8, 9],
[ 3, 4, 9, 4, 5],
[ 2, 3, 8, 3, 4],
[ 4, 5, 10, 5, 6],
[-4, -3, 2, -3, -2],
[ 2, 3, 8, 3, 4],
[-1, 0, 5, 0, 1],
[ 2, 3, 8, 3, 4],
[ 4, 5, 10, 5, 6],
[ 9, 10, 15, 10, 11],
[-1, 0, 5, 0, 1],
[-4, -3, 2, -3, -2],
[ 0, 1, 6, 1, 2],
[ 4, 5, 10, 5, 6],
[ 6, 7, 12, 7, 8],
[-2, -1, 4, -1, 0]])
>>> torch.mode(b, 0)
torch.return_types.mode(
values=tensor([2, 3, 8, 3, 4]),
indices=tensor([20, 20, 20, 20, 20]))
i don't know why indeces is all equal to 20
the details of torch.mode description as below
https://pytorch.org/docs/stable/generated/torch.mode.html#torch.mode
torch.mode(input, dim=- 1, keepdim=False, *, out=None)
Returns a namedtuple (values, indices) where values is the mode value of each row of the input tensor in the given dimension dim, i.e. a value which appears most often in that row, and indices is the index location of each mode value found.
By default, dim is the last dimension of the input tensor.
If keepdim is True, the output tensors are of the same size as input except in the dimension dim where they are of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensors having 1 fewer dimension than input.
It is because of the way the tensor b is. The row [2, 3, 8, 3, 4] is repeated a lot, so in each column the modes are respectively [2, 3, 8, 3, 4] and more importantly, the mode indices will be equal precisely because the modes occur together; if you look at the row with index 20 (i.e., the 21st row), it is exactly [2, 3, 8, 3, 4].
I am assuming that you constructed b similar to the example in torch.mode which I believe is a poor choice for an example as it leads to confusion like the one you are having.
Instead, consider the following:
>>> b = torch.randint(4, (5, 7))
>>> b
tensor([[0, 0, 0, 2, 0, 0, 2],
[0, 3, 0, 0, 2, 0, 1],
[2, 2, 2, 0, 0, 0, 3],
[2, 2, 3, 0, 1, 1, 0],
[1, 1, 0, 0, 2, 0, 2]])
>>> torch.mode(b, 0)
torch.return_types.mode(
values=tensor([0, 2, 0, 0, 0, 0, 2]),
indices=tensor([1, 3, 4, 4, 2, 4, 4]))
In the above, b has different modes in each column which are respectively [0, 2, 0, 0, 0, 0, 2] and the indices returned by torch.mode are [1, 3, 4, 4, 2, 4, 4]. This makes sense because, for example, in the first column, 0 is the most common element and there is a 0 at index 1. Similarly, in the second column, 2 is the most common element and there is a 2 at index 3. This is true for all columns. If you want the modes of the rows instead, you would do torch.mode(b, 1).

Get Specific Element from a 2d List Using Condition

Here is a list.
list=[[[5, 1, 50], [7, 10, 52], 2], [[7, 10, 52], [10, 5, 163], 3], [[10, 5, 163], [13, 9, 85], 3], [[13, 9, 85], [14, 3, 176], 1], [[14, 3, 176], [15, 7, 96], 1]]
I want to retrieve an element from the list based on the minimum value which are 2,3,3,1,1 or we can say list[i][j]. From these element I want to find the minimum which is 1 and from that I want to retrieve the element [[[13, 9, 85], [14, 3, 176], 1]]. As here two 1 exist, indexwise I am choosing first one.
Can you tell me how can I do the code?
Sorry for being noob.
Just use find minimum concept with O(N)
list1=[[[5, 1, 50], [7, 10, 52], 2], [[7, 10, 52], [10, 5, 163], 3], [[10, 5, 163], [13, 9, 85], 3], [[13, 9, 85], [14, 3, 176], 1], [[14, 3, 176], [15, 7, 96], 1]]
# Assign first element as a minimum.
min1 = list1[0][2]
minIx = 0;
for i in range(len(list1)):
# If the other element is less than min element
if list1[i][2] < min1:
min1 = list1[i][2] #It will change
minIx = i
print("The smallest element in the list is ",list1[minIx])

Pytorch, retrieving values from a 3D tensor using several indices. Most computationally efficient solution

Related:
Pytorch, retrieving values from a tensor using several indices. Most computationally efficient solution
This is another question about retrieving values from a 3D tensor, using a list of indices.
In this case, I have a 3d tensor, for example
b = [[[4, 20], [1, -1]], [[1, 2], [8, -1]], [[92, 4], [23, -1]]]
tensor_b = torch.tensor(b)
tensor_b
tensor([[[ 4, 20],
[ 1, -1]],
[[ 1, 2],
[ 8, -1]],
[[92, 4],
[23, -1]]])
In this case, I have a list of 3D indices. So
indices = [
[[1, 0, 1], [2, 0, 1]],
[[1, 1, 1], [0, 0, 0]],
[[2, 1, 0], [0, 1, 0]]
]
Each triple is an index for tensor-b. The desired result is
[[2, 4], [-1, 4], [23, 1]]
Potential Approach
Like in the last question, the first solution that comes to mind is a nested for loop, but there is probably a more computationally efficient solution using pytorch function.
And like in the last question, perhaps reshape would be needed to get the desired shape for the last solution.
So a desired solution could be [2, 4, -1, 4, 23, 1], which can come from a flattened list of indices
[ [1, 0, 1], [2, 0, 1], [1, 1, 1], [0, 0, 0], [2, 1, 0], [0, 1, 0] ]
But I am not aware of any pytorch functions so far which allow for a list of 3D indices. I have been looking at gather and index_select.
You can use advanced indexing specifically integer array indexing
tensor_b = torch.tensor([[[4, 20], [1, -1]], [[1, 2], [8, -1]], [[92, 4], [23, -1]]])
indices = torch.tensor([
[[1, 0, 1], [2, 0, 1]],
[[1, 1, 1], [0, 0, 0]],
[[2, 1, 0], [0, 1, 0]]
])
result = tensor_b[indices[:, :, 0], indices[:, :, 1], indices[:, :, 2]]
results in
tensor([[ 2, 4],
[-1, 4],
[23, 1]])

Show 5 days along x axis with panning

I'm trying to show only 5 points along the x axis, with panning enabled - I want the user to pan through the other points beyond 5.
I have disabled the zoom, but my problem seems to be that the more data I add, the points along the x axis starts to try and increase.
So if I load 20 points worth of data, instead of showing the 5 points which the user then should simply pan to the other 15, the x axis "zooms out" to show as many points as possible.
also, How do I set the start position? In that if there are 20 points, and only points are showing, how do I set the view to start at point 10 to 15, so the user pans back to the first 10 points, and pans forward for the remaining 5?
Use these options for your x axis:
xaxis: {
panRange: [0, 20],
min: 5,
max: 10
}
panRange defines the borders of the range to pan and min & max define the start range.
Edit: You can specify an array with ticknames:
var ticks = [
[1, 'one'],
[2, 'two'],
and use it like this:
xaxis: {
...
ticks: ticks,
See the code snippet below for a full example:
$(function() {
var data = [
[1, 2],
[2, 3],
[3, 1],
[4, 4],
[5, 2],
[6, 3],
[7, 3],
[8, 2],
[9, 1],
[10, 1],
[11, 3],
[12, 4],
[13, 2],
[14, 2],
[15, 4],
[16, 3],
[17, 3],
[18, 1],
[19, 4]
];
var ticks = [
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven'],
[8, 'eight'],
[9, 'nine'],
[10, 'ten'],
[11, 'eleven'],
[12, 'twelve'],
[13, 'thirteen'],
[14, 'fourteen'],
[15, 'fifteen'],
[16, 'sixteen'],
[17, 'seventeen'],
[18, 'eighteen'],
[19, 'nineteen']
];
var options = {
series: {
points: {
show: true
},
lines: {
show: true
}
},
xaxis: {
panRange: [0, 20],
min: 5,
max: 10,
ticks: ticks,
tickDecimals: 0
},
yaxis: {
panRange: [0, 5],
min: 0,
max: 5,
tickDecimals: 0
},
zoom: {
interactive: false
},
pan: {
interactive: true
}
};
var plot = $.plot('#placeholder', [data], options);
});
#placeholder {
width: 400px;
height: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://www.flotcharts.org/flot/jquery.flot.js"></script>
<script src="http://www.flotcharts.org/flot/jquery.flot.navigate.js"></script>
<div id="placeholder"></div>

Node.js File Output Incomplete

I've got a script that I'm toying around with to format all generated permutations for a specific scenario so that I can then create an array of strings for the next part. For whatever reason, when I output to my 'output' file, the file stops at ' ["1-12-17-"], ' when it should end on ' ["25-24-23-"], ' The console output ends on the right string but the text file stops short.
Here's the code:
var fs = require('fs');
// 13800
var input = [ [1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 2, 7], [1, 2, 8], [1, 2, 9], [1, 2, 10], [1, 2, 11], [1, 2, 12], [1, 2, 13], [1, 2, 14], [1, 2, 15], [1, 2, 16], [1, 2, 17], [1, 2, 18], [1, 2, 19], [1, 2, 20], [1, 2, 21], [1, 2, 22], [1, 2, 23], [1, 2, 24], [1, 2, 25], [1, 3, 2], [1, 3, 4], [1, 3, 5], [1, 3, 6], [1, 3, 7], [1, 3, 8], [1, 3, 9], [1, 3, 10], [1, 3, 11], [1, 3, 12], [1, 3, 13], [1, 3, 14], [1, 3, 15], [1, 3, 16], [1, 3, 17], [1, 3, 18], [1, 3, 19], [1, 3, 20], [1, 3, 21], [1, 3, 22], [1, 3, 23], [1, 3, 24], [1, 3, 25], [1, 4, 2], [1, 4, 3], [1, 4, 5], [1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 4, 10], [1, 4, 11], [1, 4, 12], [1, 4, 13], [1, 4, 14], [1, 4, 15], [1, 4, 16], [1, 4, 17], [1, 4, 18], [1, 4, 19], [1, 4, 20], [1, 4, 21], [1, 4, 22], [1, 4, 23], [1, 4, 24], [1, 4, 25], [1, 5, 2], [1, 5, 3], [1, 5, 4], [1, 5, 6], [1, 5, 7], [1, 5, 8], [1, 5, 9], [1, 5, 10], [1, 5, 11], [1, 5, 12], [1, 5, 13], [1, 5, 14], [1, 5, 15], [1, 5, 16], [1, 5, 17], [1, 5, 18], [1, 5, 19], [1, 5, 20], [1, 5, 21], [1, 5, 22], [1, 5, 23], [1, 5, 24], [1, 5, 25], [1, 6, 2], [1, 6, 3], [1, 6, 4], [1, 6, 5], [1, 6, 7], [1, 6, 8], [1, 6, 9], [1, 6, 10], [1, 6, 11], [1, 6, 12], [1, 6, 13], [1, 6, 14], [1, 6, 15], [1, 6, 16], [1, 6, 17], [1, 6, 18], [1, 6, 19], [1, 6, 20], [1, 6, 21], [1, 6, 22], [1, 6, 23], [1, 6, 24], [1, 6, 25], [1, 7, 2], [1, 7, 3], [1, 7, 4], [1, 7, 5], [1, 7, 6], [1, 7, 8], [1, 7, 9], [1, 7, 10], [1, 7, 11], [1, 7, 12], [1, 7, 13], [1, 7, 14], [1, 7, 15], [1, 7, 16], [1, 7, 17], [1, 7, 18], [1, 7, 19], [1, 7, 20], [1, 7, 21], [1, 7, 22], [1, 7, 23], [1, 7, 24], [1, 7, 25], [1, 8, 2], [1, 8, 3], [1, 8, 4], [1, 8, 5], [1, 8, 6], [1, 8, 7], [1, 8, 9], [1, 8, 10], [1, 8, 11], [1, 8, 12], [1, 8, 13], [1, 8, 14], [1, 8, 15], [1, 8, 16], [1, 8, 17], [1, 8, 18], [1, 8, 19], [1, 8, 20], [1, 8, 21], [1, 8, 22], [1, 8, 23], [1, 8, 24], [1, 8, 25], [1, 9, 2], [1, 9, 3], [1, 9, 4], [1, 9, 5], [1, 9, 6], [1, 9, 7], [1, 9, 8], [1, 9, 10], [1, 9, 11], [1, 9, 12], [1, 9, 13], [1, 9, 14], [1, 9, 15], [1, 9, 16], [1, 9, 17], [1, 9, 18], [1, 9, 19], [1, 9, 20], [1, 9, 21], [1, 9, 22], [1, 9, 23], [1, 9, 24], [1, 9, 25], [1, 10, 2], [1, 10, 3], [1, 10, 4], [1, 10, 5], [1, 10, 6], [1, 10, 7], [1, 10, 8], [1, 10, 9], [1, 10, 11], [1, 10, 12], [1, 10, 13], [1, 10, 14], [1, 10, 15], [1, 10, 16], [1, 10, 17], [1, 10, 18], [1, 10, 19], [1, 10, 20], [1, 10, 21], [1, 10, 22], [1, 10, 23], [1, 10, 24], [1, 10, 25], [1, 11, 2], [1, 11, 3], [1, 11, 4], [1, 11, 5], [1, 11, 6], [1, 11, 7], [1, 11, 8], [1, 11, 9], [1, 11, 10], [1, 11, 12], [1, 11, 13], [1, 11, 14], [1, 11, 15], [1, 11, 16], [1, 11, 17], [1, 11, 18], [1, 11, 19], [1, 11, 20], [1, 11, 21], [1, 11, 22], [1, 11, 23], [1, 11, 24], [1, 11, 25], [1, 12, 2], [1, 12, 3], [1, 12, 4], [1, 12, 5], [1, 12, 6], [1, 12, 7], [1, 12, 8], [1, 12, 9], [1, 12, 10], [1, 12, 11], [1, 12, 13], [1, 12, 14], [1, 12, 15], [1, 12, 16], [1, 12, 17], [1, 12, 18], [1, 12, 19], [1, 12, 20], [1, 12, 21], [1, 12, 22], [1, 12, 23], [1, 12, 24], [1, 12, 25], [1, 13, 2], [1, 13, 3], [1, 13, 4], [1, 13, 5], [1, 13, 6], [1, 13, 7], [1, 13, 8], [1, 13, 9], [1, 13, 10], [1, 13, 11], [1, 13, 12], [1, 13, 14], [1, 13, 15], [1, 13, 16], [1, 13, 17], [1, 13, 18], [1, 13, 19], [1, 13, 20], [1, 13, 21], [1, 13, 22], [1, 13, 23], [1, 13, 24], [1, 13, 25], [1, 14, 2], [1, 14, 3], [1, 14, 4], [1, 14, 5], [1, 14, 6], [1, 14, 7], [1, 14, 8], [1, 14, 9], [1, 14, 10], [1, 14, 11], [1, 14, 12], [1, 14, 13], [1, 14, 15], [1, 14, 16], [1, 14, 17], [1, 14, 18], [1, 14, 19], [1, 14, 20], [1, 14, 21], [1, 14, 22], [1, 14, 23], [1, 14, 24], [1, 14, 25], [1, 15, 2], [1, 15, 3], [1, 15, 4], [1, 15, 5], [1, 15, 6], [1, 15, 7], [1, 15, 8], [1, 15, 9], [1, 15, 10], [1, 15, 11], [1, 15, 12], [1, 15, 13], [1, 15, 14], [1, 15, 16], [1, 15, 17], [1, 15, 18], [1, 15, 19], [1, 15, 20], [1, 15, 21], [1, 15, 22], [1, 15, 23], [1, 15, 24], [1, 15, 25], [1, 16, 2], [1, 16, 3], [1, 16, 4], [1, 16, 5], [1, 16, 6], [1, 16, 7], [1, 16, 8], [1, 16, 9], [1, 16, 10], [1, 16, 11], [1, 16, 12], [1, 16, 13], [1, 16, 14], [1, 16, 15], [1, 16, 17], [1, 16, 18], [1, 16, 19], [1, 16, 20], [1, 16, 21], [1, 16, 22], [1, 16, 23], [1, 16, 24], [1, 16, 25], [1, 17, 2], [1, 17, 3], [1, 17, 4], [1, 17, 5], [1, 17, 6], [1, 17, 7], [1, 17, 8], [1, 17, 9], [1, 17, 10], [1, 17, 11], [1, 17, 12], [1, 17, 13], [1, 17, 14], [1, 17, 15], [1, 17, 16], [1, 17, 18], [1, 17, 19], [1, 17, 20], [1, 17, 21], [1, 17, 22], [1, 17, 23], [1, 17, 24], [1, 17, 25], [1, 18, 2], [1, 18, 3], [1, 18, 4], [1, 18, 5], [1, 18, 6], [1, 18, 7], [1, 18, 8], [1, 18, 9], [1, 18, 10], [1, 18, 11], [1, 18, 12], [1, 18, 13], [1, 18, 14], [1, 18, 15], [1, 18, 16], [1, 18, 17], [1, 18, 19], [1, 18, 20], [1, 18, 21], [1, 18, 22], [1, 18, 23], [1, 18, 24], [1, 18, 25], [1, 19, 2], [1, 19, 3], [1, 19, 4], [1, 19, 5], [1, 19, 6], [1, 19, 7], [1, 19, 8], [1, 19, 9], [1, 19, 10], [1, 19, 11], [1, 19, 12], [1, 19, 13], [1, 19, 14], [1, 19, 15], [1, 19, 16], [1, 19, 17], [1, 19, 18], [1, 19, 20], [1, 19, 21], [1, 19, 22], [1, 19, 23], [1, 19, 24], [1, 19, 25], [1, 20, 2], [1, 20, 3], [1, 20, 4], [1, 20, 5], [1, 20, 6], [1, 20, 7], [1, 20, 8], [1, 20, 9], [1, 20, 10], [1, 20, 11], [1, 20, 12], [1, 20, 13], [1, 20, 14], [1, 20, 15], [1, 20, 16], [1, 20, 17], [1, 20, 18], [1, 20, 19], [1, 20, 21], [1, 20, 22], [1, 20, 23], [1, 20, 24], [1, 20, 25], [1, 21, 2], [1, 21, 3], [1, 21, 4], [1, 21, 5], [1, 21, 6], [1, 21, 7], [1, 21, 8], [1, 21, 9], [1, 21, 10], [1, 21, 11], [1, 21, 12], [1, 21, 13], [1, 21, 14], [1, 21, 15], [1, 21, 16], [1, 21, 17], [1, 21, 18], [1, 21, 19], [1, 21, 20], [1, 21, 22], [1, 21, 23], [1, 21, 24], [1, 21, 25], [1, 22, 2], [1, 22, 3], [1, 22, 4], [1, 22, 5], [1, 22, 6], [1, 22, 7], [1, 22, 8], [1, 22, 9], [1, 22, 10], [1, 22, 11], [1, 22, 12], [1, 22, 13], [1, 22, 14], [1, 22, 15], [1, 22, 16], [1, 22, 17], [1, 22, 18], [1, 22, 19], [1, 22, 20], [1, 22, 21], [1, 22, 23], [1, 22, 24], [1, 22, 25], [1, 23, 2], [1, 23, 3], [1, 23, 4], [1, 23, 5], [1, 23, 6], [1, 23, 7], [1, 23, 8], [1, 23, 9], [1, 23, 10], [1, 23, 11], [1, 23, 12], [1, 23, 13], [1, 23, 14], [1, 23, 15], [1, 23, 16], [1, 23, 17], [1, 23, 18], [1, 23, 19], [1, 23, 20], [1, 23, 21], [1, 23, 22], [1, 23, 24], [1, 23, 25], [1, 24, 2], [1, 24, 3], [1, 24, 4], [1, 24, 5], [1, 24, 6], [1, 24, 7], [1, 24, 8], [1, 24, 9], [1, 24, 10], [1, 24, 11], [1, 24, 12], [1, 24, 13], [1, 24, 14], [1, 24, 15], [1, 24, 16], [1, 24, 17], [1, 24, 18], [1, 24, 19], [1, 24, 20], [1, 24, 21], [1, 24, 22], [1, 24, 23], [1, 24, 25], [1, 25, 2], [1, 25, 3], [1, 25, 4], [1, 25, 5], [1, 25, 6], [1, 25, 7], [1, 25, 8], [1, 25, 9], [1, 25, 10], [1, 25, 11], [1, 25, 12], [1, 25, 13], [1, 25, 14], [1, 25, 15], [1, 25, 16], [1, 25, 17], [1, 25, 18], [1, 25, 19], [1, 25, 20], [1, 25, 21], [1, 25, 22], [1, 25, 23], [1, 25, 24] ];
// I had to dramatically shorten the data
var temp = [];
//console.log(input.length);
for (var x = 0; x < input.length; x++) {
temp[x] = "[\"" + input[x][0] + "-" + input[x][1] + "-" + input[x][2] + "-\"], ";
if (true) {
fs.appendFile("output", temp[x], function (err) {});
console.log("HERE - " + temp[x]);
}
}
Any ideas? Could the output indicate a limitation of node.js? Or perhaps it's a file line limitation?
Any help would be greatly appreciated.
fs.appendFile is asynchronous, so writes to file might not occur in order you would expect. You can easily fix the problem by using fs.appendFileSync instead of fs.appendFile. Or you could change your code to use asynchronous method in the right way:
var fs = require('fs');
var input = [];
for (var i = 1; i < 25; i++)
for (var j = 1; j < 25; j++)
for (var k = 1; k < 25; k++)
input.push([i, j, k]);
var temp = [];
/* Synchronous method */
for (var x = 0; x < input.length; x++) {
temp[x] = makeStr(input[x]);
fs.appendFileSync("output.txt", temp[x]);
console.log("HERE - " + temp[x]);
}
/* Asynchronous method */
var i = 0,
callback = function (err) {
i++;
if (!err && i < input.length) {
temp.push(makeStr(input[i]));
fs.appendFile("output.txt", temp[i], callback);
}
};
temp.push(makeStr(input[i]));
fs.appendFile("output.txt", temp[i], callback);
function makeStr(item) {
return '["' + item[0] + '-' + item[1] + '-' + item[2] + '"]';
}

Resources