Reading particular word by its position in vbscript - vbscript

I have following lines in a file:
Displaying 5 of 17 -- All country members
Displaying 0 of 0 -- All country members
Displaying 15 of 417 -- All country members
Displaying 134 of 4890 -- All country members
In my script condition, i may get any line from the above.
I want to fetch only the bold number.
Is there any way to fetch the 4th word of a line?

You could use Split() to get an array of the " " separated chunks of the line and access the fourth element by accessing it with its (zero-based) index 3.
demo:
>> WScript.Echo Split("Displaying 134 of 4890 -- All country members")(3)
>>
4890
>>

Related

SAS Macros in Datalines

I have a two part question about creating datasets in SAS that calls upon macro variables
Part 1
I'm trying to create a dataset that has one character variable called variable with a length of 100, and 3 observations.
%let first_value=10;
%let second_value=20;
%let third_value=30;
data temp;
infile cards truncover;
input variable $100.;
cards;
First Value: &first_value
Second Value: &second_value
Third Value: &third_value
;
run;
My output dataset doesn't show the macro variables, just the exact text I entered in the datalines. I would love help on syntax of how to concatenate character input with a macro variable. Also I'm curious why sometimes you need a separate length statement for character variables before the input statement when other times you can just specify the length in the input statement like above.
Part 2
Next, I'm trying to create a dataset that has one observation with 4 variables, 3 of which are macro variables.
data temp2;
infile cards dlm=" "
input variable $ first_var second_var third_var
cards;
Observation 1 Filler &first_value &second_value &third_value
;
run;
The 4 spaces in the delimiter statement and between variables in the datalines are actually tabs in my code.
Thanks!
Your examples do not seem to be worth using macro variables.
But if you really need to resolve macro expressions in variable values then use the RESOLVE() function. The RESOLVE() will evaluate all macro code in the text, not just the macro variable references in your example. So any macro function calls and calls to actual macros will be resolved and the generated text returned as the result of the function.
newvar=resolve(oldvar);
So your examples become:
data temp;
infile cards truncover;
input variable $100.;
variable = resolve(variable);
cards;
First Value: &first_value
Second Value: &second_value
Third Value: &third_value
;
data temp2;
infile cards dlm="|" ;
input #;
_infile_=resolve(_infile_);
input variable :$100. first_var second_var third_var ;
cards;
Observation 1 Filler|&first_value|&second_value|&third_value
;
But on the second one be careful as the _INFILE_ variable for CARDS images are fixed multiples of 80 bytes so if the resolved macro expressions make the string longer than the next 80 byte boundary you will lose the extra text.
511 %let xx=%sysfunc(repeat(----+----0,8));
512
513 data test;
514 infile cards truncover;
515 input #;
516 _infile_=resolve(_infile_);
517 input variable $100. ;
518 length=lengthn(variable);
519 put length= variable=;
520 cards;
length=5 variable=short
length=80 variable=long ----+----0----+----0----+----0----+----0----+----0----+----0----+----0----+
NOTE: The data set WORK.TEST has 2 observations and 2 variables.
So use input from an actual file instead. That way the limit is instead the 32,767 byte limit for a character variable.
%let xx=%sysfunc(repeat(----+----0,8));
options parmcards=text;
filename text temp;
parmcards;
short
long &xx
;
531
532
533 data test;
534 infile text truncover;
535 input #;
536 _infile_=resolve(_infile_);
537 input variable $100. ;
538 length=lengthn(variable);
539 put length= variable=;
540 run;
NOTE: The infile TEXT is:
Filename=C:\...\#LN00053,
RECFM=V,LRECL=32767,File Size (bytes)=17,
Last Modified=08Jul2022:23:42:10,
Create Time=08Jul2022:23:42:10
length=5 variable=short
length=95 variable=long ----+----0----+----0----+----0----+----0----+----0----+----0----+----0----+----0----+----0
NOTE: 2 records were read from the infile TEXT.
The minimum record length was 5.
The maximum record length was 8.
NOTE: The data set WORK.TEST has 2 observations and 2 variables.

How to use two different excel files in same syntax procedure?

I have an excel file with information about variables (excel1) and another one with information about lists (excel2).
In order to create a syntax to generate a new syntax to create VARIABLE and VALUES LABELS, I used solution proposed by #eli.k here.
But with this solution I have to have a dataset with lists so I could use it instead of writing it “by hand” (copy/paste) (here). One problem came with L2, which has 195 entries so the new create variable would need to be bigger that 20.000 characters (is this possible in SPSS?), appearing all in one line.
What I want to know is if it’s possible to use excel2 automatically in code, line by line.
Using the following code:
GET DATA
/TYPE=XLSX
/FILE=" D:\excel1.xlsx "
/SHEET=name 'Folha1'
/CELLRANGE=FULL
/READNAMES=ON
/DATATYPEMIN PERCENTAGE=95.0.
STRING cmd1 cmd2 (a200).
SORT CASES by List.
MATCH FILES /FILE=* /FIRST=first /LAST=last /BY List. /* marking first and last lines.
DO IF first.
COMPUTE cmd1="VARIABLE LABELS".
COMPUTE cmd2="VALUE LABELS".
END IF.
IF not first cmd1=concat(rtrim(cmd1), " "). /* "/" only appears from the second varname.
COMPUTE cmd1=concat(rtrim(cmd1), " ", Var_label).
COMPUTE cmd2=concat(rtrim(cmd2), " ", Var).
DO IF last.
COMPUTE cmd1=concat(rtrim(cmd1), ".").
COMPUTE cmd2=concat(rtrim(cmd2), " ",' 1 "Afghanistan" 2 "Albania" (…) 195 "Zimbabwe".').
END IF.
EXECUTE.
SELECT IF ('List' 'L2').
ADD FILES /file=* /rename cmd1=cmd /file=* /rename cmd2=cmd.
EXECUTE.
I would like to know if there is a way to replace ' 1 "Afghanistan" 2 "Albania" (…) 195 "Zimbabwe".'' by some function/procedure to grab information from excel2 concerning L2, and showing it line by line:
(…)
VARIABLE LABELS V2 "Country"
/ V3 "Country Mother"
/ V4 "Country Father".
VALUE LABELS V2
V3
V4
1 "Afghanistan"
2 "Albania"
(…)
195 "Zimbabwe".
Thanks for helping me!
This issue is pretty complex and would usually be beyond the scope of Stack-Overflow Q&A but here's my answer anyway:
First I recreate the parts of your example data concerning the value labels only:
data list list/var list (2a5).
begin data
"v1" "L1"
"v2" "L2"
"v3" "L2"
"v4" "L2"
end data.
dataset name xl1.
data list list/list (a5) nb (f5) nb_txt (a20).
begin data
"L1" 1 "Female"
"L1" 2 "Male"
"L2" 1 "Afghanistan"
"L2" 2 "Albania"
"L2" 43 "Israel"
"L2" 195 "Zimbabwe"
end data.
dataset name xl2.
data list list/v1 v2 v3 v4 (4f3).
begin data
1 1 2 3
2 2 2 43
1 2 1 195
end data.
dataset name gen.
Now to work:
The first part is to create a macro for each list of variable labels. since some of the lists are long, I use ADD Value labels separately for each value.
dataset activate xl2.
string cmd (a200) cmdFin (a20).
sort cases by list nb.
match files /file=* /by list /first=first /last=last.
compute cmd=concat("add value labels !1 ", string(nb,f6), " '", rtrim(nb_txt), "' .").
if first cmd=concat("define dolist_", list, " (!pos=!cmdend) ", rtrim(cmd)).
if last cmdFin=" !enddefine .".
write outfile="path\create value label macros.sps"/cmd/cmdfin.
exe.
insert file="path\create value label macros.sps".
After inserting the generated syntax a macro has been defined for each of the value lists. Now we create an additional syntax that will run the related macro for each of the variable names in the list:
dataset activate xl1.
string cmd (a200).
compute cmd=concat("dolist_", list, " ", var, " .").
write outfile="path\run value label macros.sps"/cmd.
exe.
Now we can actually try out the generated macros on our original data:
dataset activate gen.
insert file="path\run value label macros.sps".

String iteration programming

I'd like to write a function able to generate an array of string with iteration in Go programming language.
Enter a unique name for the signal path in the field titled Description. Since the field features an iterator, multiple, consecutively counted signal paths can be set-up by using curly brackets, for example "Source {1-16:2} {Left, Right} >"
The result is an array of string:
Source 01 Left >
Source 01 Right >
Source 02 Left >
Source 02 Right >
...
Source 16 Left >
Source 16 Right >
I could have an undefined curly brackets iterator in the string.
The first curly brackets {1-16:2} indicate 1-16 : increment value
from 1 to 16 :2 is the padding that means two digits
The second curly brackets [Left, Right} indicate a defined list of
value.
I don't have the code yet.
Today I have an UI interface where I have some optional fields to fill.
signal path : "Source" as the unique identifier (as string) (Mandatory)
counter : "16" increment value from 1 till 16 (as integer) (optional)
Source 1
Source 2
Source 3
...
Source 16
If I would like to create a signal path like this:
Source 01 Left >
Source 01 Right >
I need to do it manually. If I have 1000 signal path to create it will take hours with errors and if the counter > 0 then increment value will be added at the end of the signal path and no padding.
Now I am looking some help if any libraries could analyze my string iteration "Source {1-16:2} {Left, Right}" and generate the string as expected.
Best regards,
Youssef

Awk scripting to obtain links for a chosen column 1 elements for an input file

I have a data set as below:
2,5
159,5
2,100
2,858
3,100
3,114
3,171
3,858
5,100
858,2
2,2456
4500,2
2456,3
If I choose an element from column 1 such as 2. I need to get the corresponding elements of the chosen element from column 2.
I have used :
awk -F, '$1=="2" {print $2}' Sample.txt
This returns the corresponding column 2 elements of the element 2 which is as below:
5
100
858
2456
I would like the next iteration to perform a check on 5 and return the column 2 elements. In this case, 5 should return 100 but it is already shown by 2 so I don't need 100. The same check for 100 and so forth till 2456. For 2456 it should return 3 which is the corresponding column 2 element and is unique. I would want that iteration to continue the same for 3 and return the unique corresponding column 2 unique elements until there are no column 2 elements to return.
Final output should look like :
5
100
858
2456
3
114
171
# 3 is got as a column 2 element of 2456 and 114,171 are got as column 2 element of 3. Since, 114 and 171 don't have any further unique column 2 elements (Refer the sample data set above). The iteration stops. Can this be recursively achieved as I am able to do it only for the first chosen element.
The command you have can be changed to:
awk -F, '$1==2 {print $2}' Sample.txt >> tmp.txt
5
100
858
2456

ASP Left function issue

I have this code and something odd happening when I'm running it.
I have field number like 101512 up to 101520. I've used LEFT function to get rid of last two digits and keep the 1015. When i runn loop function for the first one it gives me 1015 but for the rest it gives me 101 an it elminates the last digit like this:
d = Split(Request("field"),",")
For i = 1 To UBound(d)
Responce.Write(Left(d(i),4))
Next
Results
1015
101
101
101
...
Does anybody have any idea what is going on?
My guess is that Request("field") may be returning a string like the following:
101520, 101521, 101522
Note the space after each comma. Thus when you apply Left() and print the value to your HTML output you don't notice the space but you only see three digits as the space counted as the first digit
One thing to try to see if this is the case is to change the code to the following:
Left(Trim(d(i)), 4)
That way any spaces around the value are removed before Left() is applied.
Correct way to iterate over "multi value" request item is actually:
For i = 0 To Request("field").Count-1
Response.Write(Request("field").Item(i) & "<br />")
Next
This will iterate the actual values without using split at all..

Resources