I have a long variant array. I was trying to break it with (, _) but I still got the same error.
for example:
arr as variant
arr = array(15,54,10,15,0,0,0,51,12,36,23,15,52,115,132,16,13,18,0,0,0,0,0,0,0,2,0,51,13, _,
,1,25,15,31,81,35,64,31,,0,0,0,0,2,0,5,1,4,3,150,1,91,156,151,51,150,1,0,0,0, _ , ...
maybe the array length is 30 lines.
any help on how to solve this error?
Can't you take your data another way? or do you need to generate random numbers? I think it will take more explanation on what you plan to do to help yourself.
So far the only thing I have found is that your array is filled with a function. I don't know if that will satisfy you as an answer.
Sub main()
Dim arr() As Variant
arr = Array(15, 54)
Call AddArray(arr, Array(15, 54, 10, 15, 0, 0, 0, 51, 12, 36, 23, 15, 52, 115, 132, 16, 13, 18, 0, 0, 0, 0, 0, 0, 0, 2, 0, 51, 13))
Call AddArray(arr, Array(1, 25, 15, 31, 81, 35, 64, 31, , 0, 0, 0, 0, 2, 0, 5, 1, 4, 3, 150, 1, 91, 156, 151, 51, 150, 1, 0, 0, 0))
End Sub
Function AddArray(ByRef arr As Variant, ByRef arrAdd As Variant) As Variant
' "UBound(arr) - LBound(arr) + 1" : Is the calcul for get length of array
LengthArr = UBound(arr) - LBound(arr) + 1
LengthArrAdd = UBound(arrAdd) - LBound(arrAdd) + 1
'Calcul the length of futur array
LengthNewArr = LengthArr + LengthArrAdd - 1
'Redimension the arr
ReDim Preserve arr(LBound(arr) To LengthNewArr)
'Fill the arr with i who start to new espace of array at end
For i = LengthArr To LengthNewArr
arr(i) = arrAdd(i - LengthArr)
Next
'AddArray = arr
End Function
result : arr(0) = 15, arr(1) = 54, ..., ..., arr(59) = 0, arr(60) = 0
Related
hi there i need to parse the byte data of a response from api the data is
[0, 2, 0, 44, 0, 6, 58, 1, 0, 1, 109, 85, 0, 0, 0, 1, 0, 1, 111, 97, 0, 115, 224, 4, 0, 0, 0, 0, 0, 0, 1, 114, 0, 1, 115, 169, 0, 1, 116, 18, 0, 1, 108, 121, 0, 1, 113, 241, 0, 44, 0, 13, 128, 1, 0, 0, 55, 200, 0, 0, 0, 10, 0, 0, 55, 227, 5, 172, 149, 3, 0, 0, 84, 154, 0, 0, 0, 0, 0, 0, 56, 79, 0, 0, 57, 28, 0, 0, 54, 226, 0, 0, 56, 89]
there are certain rules to parse the data according to api provider as below
A The first two bytes ([0 - 2] -- SHORT or int16) represent the number of packets in the message.
B The next two bytes ([2 - 4] -- SHORT or int16) represent the length (number of bytes) of the first packet.
C The next series of bytes ([4 - 4+B]) is the quote packet.
D The next two bytes ([4+B - 4+B+2] -- SHORT or int16) represent the length (number of bytes) of the second packet.
C The next series of bytes ([4+B+2 - 4+B+2+D]) is the next quote packet.
please someone help me how to parse this data according to these rules in dart. i am stuck thanks for help
First, you need to convert the list of integers into a list of bytes:
final byteList = Uint8List.fromList(responseList);
Then you need to create a ByteData from that byte list:
final byteData = ByteData.view(byteList.buffer);
Then you can do your parsing of various bytes, shorts, ints, or longs that you want at various byte offsets. For example:
final packetNum = byteData.getUint16(0);
final firstPacketLength = byteData.getUint16(2);
final firstPacketView = ByteData.sublistView(byteData, 4, 4 + firstPacketLength);
// Do whatever you need for that packet
final secondPacketPos = 4 + firstPacketLength;
final secondPacketLength = byteData.getUint16(secondPacketPos),
final secondPacketView = ByteData.sublistView(byteData, secondPacketPos + 2, 4 + secondPacketLength);
// Do whatever you need for the saecond packet
I have defined 5 arrays.
One with undefined dimensions to store the other 4:
Dim outputArr() As Variant
and the rest as follows:
Dim Arr1(5, 0), Arr2(12, 0), Arr3(5, 0), Arr4(12, 0) As Variant
I assign the elements of the latter as follows:
Arr1(0, 0) = [{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}]
Arr1(1, 0) = [{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}]
Arr1(2, 0) = [{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}]
Arr1(3, 0) = [{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}]
Arr1(4, 0) = [{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}]
Arr1(5, 0) = [{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}]
The above is applied to each array.
When I use
ReDim outputArray(3, 0)
outputArr = [{Arr1, Arr2, Arr3, Arr4}]
I get a 'Type Mismatch' error.
When I do not use Evaluate and assign without ReDim
outputArr = Array(Arr1, Arr2, Arr3, Arr4)
I can see the elements and their values in the Watch window, but when I try to populate Defined Named Ranges with the elements of outputArr I get an empty output
Range("nRange1name").Value = outputArr(0)
Range("nRange2name").Value = outputArr(1)
Range("nRange3name").Value = outputArr(2)
Range("nRange4name").Value = outputArr(3)
How can I work around this?
The use of variants in the OP code introduces unecessary dimensions. I don't understand why two transpose functions are needed but the following code pastes 2d arrays satisfactorily.
Option Explicit
Sub TestArrays()
Dim outputArr As Variant
Dim Arr1 As Variant
Dim Arr2 As Variant
Dim Arr3 As Variant
Dim Arr4 As Variant
Arr1 = Array(Array(1, 2, 3, 4, 5, 6, 7, 8, 9), Array(1, 2, 3, 4, 5, 6, 7, 8, 9))
Arr2 = Array(Array(1, 2, 3, 4, 5, 6, 7, 8, 9), Array(1, 2, 3, 4, 5, 6, 7, 8, 9))
Arr3 = Array(Array(1, 2, 3, 4, 5, 6, 7, 8, 9), Array(1, 2, 3, 4, 5, 6, 7, 8, 9))
Arr4 = Array(Array(1, 2, 3, 4, 5, 6, 7, 8, 9), Array(1, 2, 3, 4, 5, 6, 7, 8, 9))
outputArr = Array(Arr1, Arr2, Arr3, Arr4)
' For Horizontal ranges
Range("A1:H2") = Application.WorksheetFunction.Transpose(Application.WorksheetFunction.Transpose(outputArr(2)))
'For Vertical ranges
Range("A4:B11") = Application.WorksheetFunction.Transpose(outputArr(3))
End Sub
You need to construct an actual 2D array to do something like that.
Dim arr(1 to 6, 1 to 12)
dim r as long, c as long
for r = lbound(arr, 1) to ubound(arr, 1)
for c = lbound(arr, 2) to ubound(arr, 2)
arr(r, c) = 0
next c
next r
Range("A1").Resize(ubound(arr, 1), ubound(arr, 2)).value = arr
Given Scala arrayBuffer:
ArrayBuffer(200, 13, 1, 200, 15, 1, 201, 13, 0, 202, 14, 3, 199, 10, 2, 199, 11, 3, 199, 96, 2)
Expected output:
ArrayBuffer((200, 13, 1), (200, 15, 1), (201, 13, 0), (202, 14, 3), (199, 10, 2), (199, 11, 3), (199, 96, 2))
Is there any simple way of achieving this form of chunking in Scala without for loops? The required chunk_size is 3. And the order of these elements must be the same.
I've tried:
def chunkArray(myArray){
val chunk_size = 3
var index = 0
var arrayLength = arrayToInsert.length
var tempArray = ArrayBuffer[Int](2)
val numChunks = arrayToInsert.length / 3
for (i <- 0 to numChunks-1) {
var myChunk = arrayToInsert.slice(i*chunk_size, (i+1)*chunk_size)
tempArray += (myChunk(0), myChunk(1), myChunk(2))
}
}
Expected result:
((200, 13, 1), (200, 15, 1), (201, 13, 0), (202, 14, 3), (199, 10, 2), (199, 11, 3), (199, 96, 2))
You want to use .grouped(3)
( the collections API examples )
collection.mutable.ArrayBuffer(200, 13, 1, 200, 15, 1, 201, 13, 0, 202, 14, 3, 199, 10, 2, 199, 11, 3, 199, 96, 2).grouped(3).toArray
res2: Array[collection.mutable.ArrayBuffer[Int]] = Array(ArrayBuffer(200, 13, 1), ArrayBuffer(200, 15, 1), ArrayBuffer(201, 13, 0), ArrayBuffer(202, 14, 3), ArrayBuffer(199, 10, 2), ArrayBuffer(199, 11, 3), ArrayBuffer(199, 96, 2))
This will create a Buffer of tuples, which is what the original code appears to attempt.
import collection.mutable.ArrayBuffer
val data =
ArrayBuffer(200, 13, 1, 200, 15, 1, 201, 13, 0 /*etc.*/)
data.grouped(3).collect{case Seq(a,b,c) => (a,b,c)}.toBuffer
//res0: Buffer[(Int, Int, Int)] = ArrayBuffer((200,13,1), (200,15,1), (201,13,0) /*etc.*/)
Note that if the final group is not 3 elements then it will be ignored.
This could also be achieved using sliding:
myArray.sliding(3, 3).toArray
Anyway, .grouped is better suited for this use case as discussed here Scala: sliding(N,N) vs grouped(N)
I have a sequence of Tuples that I need to gzip for storage. Afterwards I want to be able to extract the compressed content, decompress it and then get the Sequence of tuples back.
I use the following code for de/compressing:
def unzip(x: Array[Byte]) : String = {
val inputStream = new GZIPInputStream(new ByteArrayInputStream(x))
val output = scala.io.Source.fromInputStream(inputStream).mkString
return output
}
def gzip(input: Array[Byte]): Array[Byte] = {
val bos = new ByteArrayOutputStream(input.length)
val gzip = new GZIPOutputStream(bos)
gzip.write(input)
gzip.close()
val compressed = bos.toByteArray
bos.close()
compressed
}
As taken from this source https://gist.github.com/owainlewis/1e7d1e68a6818ee4d50e .
Then my routine more or less is the following:
val arr = Seq(("a",1),("b",2))
val arr_bytes = arr.toString.getBytes
val comp = compress(arr_bytes)
val x = unzip(comp)
The output is the following:
arr: Seq[(String, Int)] = List((a,1), (b,2))
arr_bytes: Array[Byte] = Array(76, 105, 115, 116, 40, 40, 97, 44, 49, 41, 44, 32, 40, 98, 44, 50, 41, 41)
comp: Array[Byte] = Array(31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -13, -55, 44, 46, -47, -48, 72, -44, 49, -44, -44, 81, -48, 72, -46, 49, -46, -44, 4, 0, 35, 120, 118, -118, 18, 0, 0, 0)
x: String = List((a,1), (b,2))
The problem is x is now a String that has the format from above (with the word List contained as well).
For example:
x.toList
res174: List[Char] = List(L, i, s, t, (, (, a, ,, 1, ), ,, , (, b, ,, 2, ), ))
My question is, how do I decompress my exact sequence back, or how do I make x into my previous sequence again?
Solved it using the play api json library for storing the content in json objects:
val arr = Json.toJson(Array(Json.obj("name"-> "Bran", "age" -> 13),Json.obj("name"-> "Jon", "age" -> 18)))
val arr_bytes = arr.toString().getBytes
val comp = compress(arr_bytes)
val x= unzip(comp)
val json = Json.parse(x)
I am having problem with most basic Scala operation and it is making me crazy.
val a = Array(1,2,3)
println(a) and result is [I#1e76345
println(a.toString()) and result is [I#1e76345
println(a.toString) and result is [I#1e76345
Can anyone tell me how to print array without writing my own function for doing that because that is silly. Thanks!
mkString will convert collections (including Array) element-by-element to string representations.
println(a.mkString(" "))
is probably what you want.
You can do the normal thing (see either Rex's or Jiri's answer), or you can:
scala> Array("bob","sue")
res0: Array[String] = Array(bob, sue)
Hey, no fair! The REPL printed it out real nice.
scala> res0.toString
res1: String = [Ljava.lang.String;#63c58252
No joy, until:
scala> runtime.ScalaRunTime.stringOf(res0)
res2: String = Array(bob, sue)
scala> runtime.ScalaRunTime.replStringOf(res0, res0.length)
res3: String =
"Array(bob, sue)
"
scala> runtime.ScalaRunTime.replStringOf(res0, 1)
res4: String =
"Array(bob)
"
I wonder if there's a width setting in the REPL. Update: there isn't. It's fixed at
val maxStringElements = 1000 // no need to mkString billions of elements
But I won't try billions:
scala> Array.tabulate(100)(identity)
res5: Array[Int] = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99)
scala> import runtime.ScalaRunTime.replStringOf
import runtime.ScalaRunTime.replStringOf
scala> replStringOf(res5, 10)
res6: String =
"Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
"
scala> res5.take(10).mkString(", ")
res7: String = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Wait, let's make that:
scala> res5.take(10).mkString("Array(", ", ", ")")
res8: String = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
This might be obvious:
scala> var vs = List("1")
vs: List[String] = List(1)
scala> vs = null
vs: List[String] = null
scala> vs.mkString
java.lang.NullPointerException
So instead:
scala> import runtime.ScalaRunTime.stringOf
import runtime.ScalaRunTime.stringOf
scala> stringOf(vs)
res16: String = null
Also, an array doesn't need to be deep to benefit from its stringPrefix:
scala> println(res0.deep.toString)
Array(bob, sue)
Whichever method you prefer, you can wrap it up:
implicit class MkLines(val t: TraversableOnce[_]) extends AnyVal {
def mkLines: String = t.mkString("", EOL, EOL)
def mkLines(header: String, indented: Boolean = false, embraced: Boolean = false): String = {
val space = "\u0020"
val sep = if (indented) EOL + space * 2 else EOL
val (lbrace, rbrace) = if (embraced) (space + "{", EOL + "}") else ("", "")
t.mkString(header + lbrace + sep, sep, rbrace + EOL)
}
}
But arrays will need a special conversion because you don't get the ArrayOps:
implicit class MkArrayLines(val a: Array[_]) extends AnyVal {
def asTO: TraversableOnce[_] = a
def mkLines: String = asTO.mkLines
def mkLines(header: String = "Array", indented: Boolean = false, embraced: Boolean = false): String =
asTO.mkLines(header, indented, embraced)
}
scala> Console println Array("bob","sue","zeke").mkLines(indented = true)
Array
bob
sue
zeke
Here are two methods.
One is to use foreach:
val a = Array(1,2,3)
a.foreach(println)
The other is to use mkString:
val a = Array(1,2,3)
println(a.mkString(""))
If you use list instead, toString() method prints the actual elenents (not the hashCode)
var a = List(1,2,3)
println(a)
or
var a = Array(1,2,3)
println(a.toList)
For a simple Array of Ints like this, we can convert to a Scala List (scala.collection.immutable.List) and then use List.toString():
var xs = Array(3,5,9,10,2,1)
println(xs.toList.toString)
// => List(3, 5, 9, 10, 2, 1)
println(xs.toList)
// => List(3, 5, 9, 10, 2, 1)
If you can convert to a List earlier and do all your operations with Lists, then you'll probably end up writing more idiomatic Scala, written in a functional style.
Note that using List.fromArray is deprecated (and has been removed in 2.12.2) .
The method deep in ArrayLike recursively converts multidimensional arrays to WrappedArray, and overwrites a long prefix "WrappedArray" with "Array".
def deep: scala.collection.IndexedSeq[Any] = new scala.collection.AbstractSeq[Any] with scala.collection.IndexedSeq[Any] {
def length = self.length
def apply(idx: Int): Any = self.apply(idx) match {
case x: AnyRef if x.getClass.isArray => WrappedArray.make(x).deep
case x => x
}
override def stringPrefix = "Array"
}
Usage:
scala> val arr = Array(Array(1,2,3),Array(4,5,6))
arr: Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6))
scala> println(arr.deep)
Array(Array(1, 2, 3), Array(4, 5, 6))
Rather than manually specifying all the parameters for mkString yourself (which is a bit more verbose if you want to add start and end markers in addition to the delimiter) you can take advantage of the WrappedArray class, which uses mkString internally. Unlike converting the array to a List or some other data structure, the WrappedArray class just wraps an array reference, it's created in effectively constant time.
scala> val a = Array.range(1, 10)
a: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> println(a)
[I#64a2e69d
scala> println(x: Seq[_]) // implicit
WrappedArray(a, b, c, d)
scala> println(a.toSeq) // explicit
WrappedArray(1, 2, 3, 4, 5, 6, 7, 8, 9)