Using an Int in a string? - basic4android

VERY new user here.
I'm playing with Basic 4 android and having fun.
Originally I wanted to do this
ImageView2.Bitmap = LoadBitmap(File.DirAssets,thumb+".jpg")
which doesn't work
The following code works, but is there a more elegant way - I'm sure there must be.
Thanks in advance.
Steve
Sub Timer1_Tick
'Handle tick events
If thumb <17 Then
thumb=thumb+1
Else
thumb=1
End If
Select thumb
Case 1
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"1.jpg")
Case 2
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"2.jpg")
Case 3
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"3.jpg")
Case 4
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"4.jpg")
Case 5
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"5.jpg")
Case 6
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"6.jpg")
Case 7
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"7.jpg")
Case 8
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"8.jpg")
Case 9
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"9.jpg")
Case 10
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"10.jpg")
Case 11
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"11.jpg")
Case 12
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"12.jpg")
Case 13
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"13.jpg")
Case 14
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"14.jpg")
Case 15
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"15.jpg")
Case 16
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"16.jpg")
Case 17
ImageView2.Bitmap = LoadBitmap(File.DirAssets,"17.jpg")
End Select
End Sub

ImageView2.Bitmap = LoadBitmap(File.DirAssets, thumb & ".jpg")

Related

Is there a way to show where a large number lies in a range of a smaller number?

I'm not quite sure how to word this question, but perhaps an example will help...
Is there an operator which will show the placement of, say, 25, on a number range from 1-7?
For example:
25/7 returns 4
21/7 returns 7
22/7 returns 1
4/7 returns 4
etc.
Example of code:
var dayOfMonth: Int = 28
var aNumber: Int
aNumber = (dayOfMonth ) % 7
func dayOfTheWeek(day: Int) {
switch day {
case 0:
print("Monday")
case 1:
print("Tuesday")
case 2:
print("Wednesday")
case 3:
print("Thursday")
case 4:
print("Friday")
case 5:
print("Saturday")
case 6:
print("Sunday")
default:
print("Error")
}
}
dayOfTheWeek(day: aNumber)
I think you're looking for the modulo operator, spelt % in Swift. With the difference that 21 % 7 is 0, not 7.
m % n could be thought of as "the remainder after dividing m by n"

LinkedStyle for the Word BuiltinStyles

I am extending the capability of the Word report writing from VSTO to consider different languages. Therefore, instead of using the headings like "Heading 1" etc, I have used wdStyleHeading1 etc. I have built a function to assign the style to the heading. The problem is that the line 1 and line 2 below are overwriting each other. If I call the function first, I loose list number and If I call function second, I loose the format. Can you please explain where I am going wrong?
I have imported the necessary references.
Call HeadingListLevel(wrdApp, 1)
wrdApp.Selection.ParagraphFormat.Style = Word.WdBuiltinStyle.wdStyleHeading1
Below is the sub function
Sub HeadingListLevel(wrdApp As Object, HeadingLvl As Integer)
'Dim wrdHeading As String
Dim wrdHeadingNr As String
Dim i As Integer
Dim ListTemp As Word.ListTemplate
wrdHeadingNr = "%" & 1
ListTemp = wrdApp.ListGalleries(Word.WdListGalleryType.wdOutlineNumberGallery).ListTemplates(1)
For i = 1 To HeadingLvl
If i > 1 Then
wrdHeadingNr = wrdHeadingNr & "." & "%" & i
End If
Next i
'wrdHeading = "Heading " & HeadingLvl
With ListTemp.ListLevels(1)
Select Case HeadingLvl
Case 1
.LinkedStyle = Word.WdBuiltinStyle.wdStyleHeading1
Case 2
.LinkedStyle = Word.WdBuiltinStyle.wdStyleHeading2
Case 3
.LinkedStyle = Word.WdBuiltinStyle.wdStyleHeading3
Case 4
.LinkedStyle = Word.WdBuiltinStyle.wdStyleHeading4
Case 5
.LinkedStyle = Word.WdBuiltinStyle.wdStyleHeading5
Case 6
.LinkedStyle = Word.WdBuiltinStyle.wdStyleHeading6
Case 7
.LinkedStyle = Word.WdBuiltinStyle.wdStyleHeading7
End Select
.NumberFormat = wrdHeadingNr
.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic
End With
wrdApp.Selection.Range.ListFormat.ApplyListTemplate(ListTemplate:=ListTemp)
ListTemp = Nothing
End Sub
I discovered the answer myself. Below is the changes to the sub function. It worked well for me.
Sub HeadingListLevel(wrdApp As Object, HeadingLvl As Integer)
'Dim wrdHeading As String
Dim wrdHeadingNr As String
Dim i As Integer
Dim ListTemp As Word.ListTemplate
wrdHeadingNr = "%" & 1
ListTemp = wrdApp.ListGalleries(Word.WdListGalleryType.wdOutlineNumberGallery).ListTemplates(5)
For i = 1 To HeadingLvl
If i > 1 Then
wrdHeadingNr = wrdHeadingNr & "." & "%" & 1
End If
Next i
'wrdHeading = "Heading " & HeadingLvl
With ListTemp.ListLevels(HeadingLvl)
Select Case HeadingLvl
Case 1
.LinkedStyle = wrdApp.ActiveDocument.Styles(Word.WdBuiltinStyle.wdStyleHeading1).NameLocal
Case 2
.LinkedStyle = wrdApp.ActiveDocument.Styles(Word.WdBuiltinStyle.wdStyleHeading2).NameLocal
Case 3
.LinkedStyle = wrdApp.ActiveDocument.Styles(Word.WdBuiltinStyle.wdStyleHeading3).NameLocal
Case 4
.LinkedStyle = wrdApp.ActiveDocument.Styles(Word.WdBuiltinStyle.wdStyleHeading4).NameLocal
Case 5
.LinkedStyle = wrdApp.ActiveDocument.Styles(Word.WdBuiltinStyle.wdStyleHeading5).NameLocal
Case 6
.LinkedStyle = wrdApp.ActiveDocument.Styles(Word.WdBuiltinStyle.wdStyleHeading6).NameLocal
Case 7
.LinkedStyle = wrdApp.ActiveDocument.Styles(Word.WdBuiltinStyle.wdStyleHeading7).NameLocal
End Select
'.LinkedStyle = wrdHeading
.NumberFormat = wrdHeadingNr
.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic
'.StartAt = 1
'.ResetOnHigher = False
End With
wrdApp.Selection.Range.ListFormat.ApplyListTemplate(ListTemplate:=ListTemp)
ListTemp = Nothing
End Sub

Extract Matching Lines after first match

I have text data at the command line that is broken into "records", each with the same value (always 1). In each record, each line is a separate key and value (no this isn't in json unfortunately). A key is sometimes repeated in the record, and sometimes the key name is part of a longer key. For example:
Record = 1
Apple = 1
Ball = 2
Car = 3
RedApple = 4
Ball = 5
Dog = 6
Elf = 7
Fudge = 8
Record = 1
Apple = 2
Ball = 4
Car = 6
RedApple = 8
Ball = 10
Dog = 12
Elf = 14
Fudge = 16
Record = 1
Apple = 3
Ball = 6
Car = 9
RedApple = 12
Ball = 15
Dog = 18
Elf = 21
Fudge = 24
Is there a quick for each record get the lines for a set of keys, returning only the first result per key?
Ex: For each record get keys {Apple, Ball, Dog}
would match the following lines:
Record = 1
Apple = 1
Ball = 2
Dog = 6
Record = 1
Apple = 2
Ball = 4
Dog = 12
...
Basically, the rule is after matching a line with "Record", get the next unique lines with " Apple ", " Ball ", and " Dog " (spacing indicating exact key match) and spit those lines out.
I can write something in perl and it wouldn't be too complex. I don't know awk, so don't know if it's better for something like this.
Is there a quick for each record get the lines for a set of keys, returning only the first result per key?
I don't believe that's actually what you want. I believe you actually want the items labeled Apple, Ball and Dog at the second level, meaning both
Record = 1
Apple = 1
Ball = 2
Car = 3
RedApple = 4
Ball = 5
Dog = 6
Elf = 7
Fudge = 8
and
Record = 1
Apple = 1
Car = 3
RedApple = 4
Ball = 5
Ball = 2
Dog = 6
Elf = 7
Fudge = 8
should produce
Record = 1
Apple = 1
Ball = 2
Dog = 6
If so, you could use
perl -ne'print if /^(?:\S|[ ]{2}(?:Apple|Ball|Dog)[ ]=)/'
or
grep -P '^(?:\S|[ ]{2}(?:Apple|Ball|Dog)[ ]=)'
Output:
Record = 1
Apple = 1
Ball = 2
Dog = 6
Record = 1
Apple = 2
Ball = 4
Dog = 12
Record = 1
Apple = 3
Ball = 6
Dog = 18
See Specifying file to process to Perl one-liner for usage.
If this isn't all you need:
$ grep -E '^(Record| (Apple|Ball|Car))' file
Record = 1
Apple = 1
Ball = 2
Car = 3
Record = 1
Apple = 2
Ball = 4
Car = 6
Record = 1
Apple = 3
Ball = 6
Car = 9
then edit your question to show a more truly representative example. Right now you've accepted an answer that's also based on guessing at your needs and may be more complicated than necessary (while this one may be more simple).
awk to the rescue!
$ awk '/^Record/ {h=$0; a["Apple"]=a["Dog"]=a["Ball"]=0}
$1 in a {if(h) {print h; h=""}
if(!a[$1]++) print}' file
Record = 1
Apple = 1
Ball = 2
Dog = 6
Record = 1
Apple = 2
Ball = 4
Dog = 12
Record = 1
Apple = 3
Ball = 6
Dog = 18
Explanation saves header line and reset the counts. For the lines that has the first field in required keys print header once and print the lines for the first appearance of the key.
If you wanted to extract the second level items only, need to incorporate leading spaces as part of key (to determine the hierarchy). This can be one alternative...
$ awk -F' *= *' '/Record/ {h=$0; a[" Apple"]=a[" Dog"]=a[" Ball"]=0}
$1 in a {if(h) {print h;h=""}; if(!a[$1]++) print}'

How to display only non-zero values in pie-chart?

I am using mpandroid chart to display my data. Here I have 5 xvals(A,B,C,D,E). Every xval has a yval(2.2,0.0,4.4,5.1,0.0,9.0).
I want to only display values on the pie-chart which are non-zero. Is there any way to do this?
So my PieData finally is created like this:
PieData d = new PieData(xVals, ds1);
wherein,
xvals contains:
0 = "A"
1 = "B"
2 = "C"
3 = "D"
4 = "E"
5 = "entry0"
6 = "entry1"
7 = "entry2"
8 = "entry3"
9 = "entry4"
and, ds1 contains:
0 = {Entry#4129} "Entry, xIndex: 0 val (sum): 0.0"
1 = {Entry#4130} "Entry, xIndex: 1 val (sum): 7400.0"
2 = {Entry#4131} "Entry, xIndex: 2 val (sum): 20634.4"
3 = {Entry#4132} "Entry, xIndex: 3 val (sum): 0.0"
4 = {Entry#4133} "Entry, xIndex: 4 val (sum): 60203.52"
I recommend that you simply write your own logic that excludes zero values from being added to the dataset.

How to define a function in Swift without running it

Starting from the Simple C program found here on stackoverflow, I've done it again in Swift and here's the code:
import Foundation
// variables and constants:
var dice1, dice2: UInt32
var score, scoreToWin, diceSum: Int
dice1 = 0
dice2 = 0
diceSum = 0
// functions:
func rollDice() ->Int {
dice1 = arc4random() % 6 + 1
dice2 = arc4random() % 6 + 1
diceSum = Int(dice1 + dice2)
println("\(diceSum)")
return diceSum
}
// main:
score = rollDice()
println("\(dice1) \(dice2) \(score)")
switch score {
case 7, 11:
println("score=\(score)\nYou WIN")
case 2, 3, 12:
println("score=\(score)\nYou LOOSE")
default:
println("You have to roll a \(score) to WIN")
do {
scoreToWin = score
diceSum = rollDice()
if diceSum == 7 { println("You LOOSE") }
else if diceSum == scoreToWin { println("You WIN") }
} while (diceSum != scoreToWin && diceSum != 7)
}
This is a possible output:
6
3 3 6
You have to roll a 6 to WIN
6
You WIN
Program ended with exit 0
I was not expecting the first line of output, because the first line indicate the function rollDice() was run while been defined.
How can I define a function without actually running it?
score = rollDice() would print the 6 you are seeing due to the println("\(diceSum)") you are doing in the rollDice method.
Declaring a method does not run it. Not in swift, nor in any other language I can think of.