Foreach on 2 variables in csh - foreach

Is it possible in csh to do foreach on 2 variables like tcl ?
set test = (a 1 b 2)
foreach a b ($test) then
echo $a
echo $b
endif
this code retrun an error

Related

Reading multiple wildcard paths into a DataFlow PCollection

In my situation, I have a bunch of events that are stored in small files in Storage under a Date folder. My data might look like this:
2022-01-01/file1.json
2022-01-01/file2.json
2022-01-01/file3.json
2022-01-01/file4.json
2022-01-01/file5.json
2022-01-02/file6.json
2022-01-02/file7.json
2022-01-02/file8.json
2022-01-03/file9.json
2022-01-03/file10.json
The DataFlow job will take start and end date as input, and needs to read all files within that date range.
I am working off of this guide: https://pavankumarkattamuri.medium.com/input-source-reading-patterns-in-google-cloud-dataflow-4c1aeade6831
I see there is a way to load a list of files into a PCollection:
def run(argv=None):
# argument parser
# pipeline options, google_cloud_options
file_list = ['gs://bucket_1/folder_1/file.csv', 'gs://bucket_2/data.csv']
p = beam.Pipeline(options=pipeline_options)
p1 = p | "create PCol from list" >> beam.Create(file_list) \
| "read files" >> ReadAllFromText() \
| "transform" >> beam.Map(lambda x: x) \
| "write to GCS" >> WriteToText('gs://bucket_3/output')
result = p.run()
result.wait_until_finish()
I also see there is a way to specify wildcards, but I haven't seen them used together.
Wondering if beam.Create() supports wildcards in the file list? This is my solution:
def run(argv=None):
# argument parser
# pipeline options, google_cloud_options
file_list = ['gs://bucket_1/2022-01-02/*.json', 'gs://2022-01-03/*.json']
p = beam.Pipeline(options=pipeline_options)
p1 = p | "create PCol from list" >> beam.Create(file_list) \
| "read files" >> ReadAllFromText() \
| "transform" >> beam.Map(lambda x: x) \
| "write to GCS" >> WriteToText('gs://bucket_3/output')
result = p.run()
result.wait_until_finish()
Have not tried this yet as I'm not sure if it's the best approach and don't see any examples online of anything similar. Wondering if I'm going in the right direction?
EDIT: This is my revised code after reading the answers:
with beam.Pipeline() as p:
file_list = ['gs://ext-pub-testjeff.appspot.com/2022-01-02/*.json', 'gs://ext-pub-testjeff.appspot.com/2022-01-03/*.json']
for i, file in enumerate(file_list):
p = (p | f"Read Text {i}" >> beam.io.textio.ReadFromText(file, skip_header_lines = 0))
p = (p | "write to GCS" >> WriteToText('gs://ext-pub-testjeff.appspot.com/output'))
EDIT2: Using the original code:
with beam.Pipeline() as p:
file_list = ['gs://ext-pub-testjeff.appspot.com/2022-01-02/*.json', 'gs://ext-pub-testjeff.appspot.com/2022-01-03/*.json']
for i, file in enumerate(file_list):
p = (p |
f"Read Text {i}" >> beam.io.textio.ReadFromText(file, skip_header_lines = 0)
| f"write to GCS {i}" >> WriteToText('gs://ext-pub-testjeff.appspot.com/output'))
You can use the following approach if it's not possible to use a single url with a wildcard :
def run():
# argument parser
# pipeline options, google_cloud_options
with beam.Pipeline(options=pipeline_options) as p:
file_list = ['gs://bucket_1/2022-01-02/*.json', 'gs://2022-01-03/*.json']
for i, file in enumerate(file_list):
(p
| f"Read Text {i}" >> beam.io.textio.ReadFromText(file, skip_header_lines = 0)
| f"transform {i}" >> beam.Map(lambda x: x)
| f"write to GCS {i}" >> WriteToText('gs://bucket_3/output'))
We do a foreach on the file paths with wildcard.
For each element we apply a pipeline with read and write.

What does "local variable" mean in the Forth programming language?

In C, local variables exist inside of a function and contain the values like this:
void main(){
int a = 5;
int b = 9;
}
In the Gforth manual, they describe the local variables like this:
: swap { a b -- b a }
b a ;
1 2 swap .s 2drop
but it seems like a function which is taking two arguments, a and b.
Another tutorial on the Forth language shows a variable like this:
variable a
3 a ! ( ! to store the value )
So, which one is correct?
In Forth, local variables are described by the following syntax (see also 13.6.2.2550 {:):
{: args [ | vals ] [ –– outs ] :}
where each of args, vals and outs represents space-delimited names (the parts in square brackets are optional). These names are interpreted as follows:
args names are for locals that are initialized from the data stack, with the top of the stack being assigned to the rightmost name in args;
vals names are for locals that are uninitialized;
outs names are ignored (they are for documentation purposes only, if any).
Gforth uses { ... } notation for locals as an alternative to the standard one.
So, swap can be defined as:
: swap {: a b :} b a ;
It takes two values from the stack into a and b local variables, and then puts them back on the stack in the reversed order.
An example of use an uninitialized local variable:
: exch ( x2 addr -- x1 ) {: a | x1 :}
a # to x1 a ! x1
;
The optional -- ... part is allowed to mimic a stack diagram, i.e., to unite the declaration of locals and the stack diagram for a word. For example:
: umin {: u2 u1 -- u2|u1 :} u2 u1 u< if u2 else u1 then ;
Without special optimizations, performance of local variables is slightly worse than of a little stack juggling.

How to solve bison reduce/reduce conflict in my code?

I'm new to bison.. I've wrote a grammar rule for if, else if and else statement.. I got reduce reduce conflict though.. Can anyone help ? I've tried everything I've found but as I told I'm new and I don't understand exactly what happens..
Here's my code:
ifinstr: KW_IF expr_decl KW_THEN statements elseifinstr elseinstr KW_END
;
elseifinstr : %empty {$$ = "";}
| elseifinstr KW_ELSE KW_IF expr_decl KW_THEN statement
;
elseinstr : %empty {$$ = "";}
| KW_ELSE statement
;
I've tried this solution too but got shift/reduce conflict instead:
ifinstr: KW_IF expr_decl KW_THEN statements elseifinstr KW_END
;
elseifinstr : %empty {$$ = "";}
| elseifinstr KW_ELSE KW_IF expr_decl KW_THEN statement
| KW_ELSE statement
;
The problem is that the grammar you propose is ambiguous. (The first grammar, I mean. The proposed second solution is a different language.)
Languages which use an "else if" construct in order to cut down on the nesting of "if … end" brackets use a special token:
Ruby Python VB Shell
---------------- ------------------- ----------------- --------------------
if n > 0 if n > 0: If n > 0 Then if ((n>0)); then
puts "Greater" print ("Greater") Print "Greater" echo Greater
elsif n == 0 elif n == 0: ElseIf n = 0 Then elif ((n==0)); then
puts "Equal" print ("Equal") Print "Equal" echo Equal
else else: Else else
puts "Less" print ("Less") Print "Less" echo Less
end End fi
Without the fused "else if", these expressions would be much more cumbersome:
Ruby Python VB Shell
---------------- ------------------- ----------------- --------------------
if n > 0 if n > 0: If n > 0 Then if ((n>0)); then
puts "Greater" print ("Greater") Print "Greater" echo Greater
else else: Else else
if n == 0 if n == 0: If n = 0 Then if ((n==0)); then
puts "Equal" print ("Equal") Print "Equal" echo Equal
else else: Else else
puts "Less" print ("Less") Print "Less" echo Less
end end End fi
end End fi
This constrasts with languages like C, Java, and many others which do not require that "if" statements be terminated, and thus exhibit the "dangling else" shift-reduce conflict, which is always resolved in favour of the shift. In those languages, since the "else" clause simply attaches to the closest unmatched "if" clause, there is no need to provide a special "else-if" token.
Now, you are attempting to combine these two approaches by using a fused "else-if" without fusing it, and that simply leads to the same dangling else problem which the explicit bracketing was attempting to fix. Consider, for example:
if C1 then S1 else if C2 then S2 else if C3 then S3 else S4 end S5 end
Now, which of the following two does this represent?
if C1 then if C1 then
S1 S1
else if C2 then else
S2 if C2 then
else S2
if C3 then else if C3 then
S3 S3
else else
S4 S4
end end
S5 S5
end end
(With more time I might have found a simpler example. The above two interpretations differ in the circumstances in which S5 is executed.)
The simplest solution is to use some fused "else if" token, as in the various language examples above. (Or think up your own :-) )

Need help to understand LPeg and PEGs

The following pattern (from this page) matches only strings with balanced parentheses:
b = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" }
What does 1- in 1 - lpeg.S"()" mean?
function gsub (s, patt, repl)
patt = lpeg.P(patt)
patt = lpeg.Cs((patt / repl + 1)^0)
return lpeg.match(patt, s)
end
What does the +1 in patt / repl + 1 mean?
And I still not quite get the function of prioritized choice operator / very well from this paper
Any help will be appreciated!
The 1 in 1 - lpeg.S"()" means any character. The whole statement can be read as, match any character while not matching a character in the set "()".
The +1 is the same idea, if repl is a string then patt / repl + 1 matches pattern patt and then replaces it's capture with the string repl or skips a character.

why variables can be variable in List Comprehensions?

as we know that variables can't be variable in erlang. but consider this code,why each value of [1,2,3,4] is sequentially pattern matched to N,and don't throw exception??
1> [2*N || N <- [1,2,3,4]].
[2,4,6,8]
Saying that a variable can't be variable isn't quite true. It's more that a variable can only be assigned once. So the following psuedo-code is illegal:
N = 4;
foo(N);
N = N + 1;
foo(N);
However, the following is legal:
fact(0) -> 1,
fact(N) -> N * fact(N-1).
When we call fact(4) N will take the value 4 then 3 then 2 then 1 for each different function call. The code you are showing above is similar. For each item in the list N takes on a different value. But you never assigned the value of N more than once.

Resources