g | x | w | all
Bytes Lang Time Link
042Wolfram Language Mathematica200922T000348Zatt
065PHP201002T160108ZKaddath
141Haskell200925T112315ZKyuuhach
057Pip rl200925T042823ZDLosc
128C# Visual C# Interactive Compiler200924T181817ZGymhgy
072Batch200922T001709ZNeil
128Python 3200921T231854Zcaird co
103Python 3200922T122340ZMiriam
067Python 3.9rc2200922T030854ZSisyphus
069Python200921T231159ZMiriam
088JavaScript200922T070503ZShaggy
172R200922T113701ZCong Che
057Perl 5 p200922T162856ZXcali
081JavaScript ES6200922T052513ZArnauld
02905AB1E200922T122147ZKevin Cr
098Scala200922T134930Zuser
069Red200922T110616ZGalen Iv
137Java 10200922T111546ZKevin Cr
085Retina 0.8.2200922T091326ZNeil
063Ruby200922T033430ZJonah
066J200922T010143Zxash
183SNOBOL4 CSNOBOL4200922T004342ZGiuseppe
022APL Dyalog Unicode200922T000816ZBubbler

Wolfram Language (Mathematica), 55 51 43 42 bytes

($=<||>;Fold[($@#2=#/.$)&]@*Reverse/@#;$)&

Try it online!

-8 thanks to w123

PHP, 65 bytes

eval(preg_filter('/([a-z_]+)/','\$$1',$argn));var_dump($GLOBALS);

Try it online!

Takes input as string with ; as separator, outputs an array.

I'm not sure this is valid, as the rules for the output are not very precise: the result is present at the end, but there are other unnecessery things displayed before... For the first time PHP's $ comes useful, as it allows to use keywords as var names (works with names such as function, echo etc)

Haskell, 177 145 141 bytes

r t=f(?)[](reverse.words.filter(/='=')<$>lines t)
s?(x:y)=f(#)s y where z|Just v<-lookup x s=v|1<2=read x;s#k=(k,z):[x|x<-s,fst x/=k]
f=foldl

Try it online!

Ungolfed:

run :: Read v => String -> [(String, v)]
run input = foldl assign [] (reverse . words . filter (/='=') <$> lines input)

assign :: Read v => [(String, v)] -> [String] -> [(String, v)]
assign scope (first:keys) = foldl acons scope keys
  where value | Just v <- lookup first scope = v
              | otherwise = read first
        acons scope' k = (k, value) : [x | x <- scope', fst x /= k]

Pip -rl, 57 bytes

{YDQaIx~'^.y.,wYXI~$'Fva.sxR:'^.v.,`.+|^$`v.y.n}Mg^sUQx^n

Try it online!

Takes input (from stdin) and produces output (to stdout) as a series of lines, each of the form a b c 5 (for a = b = c = 5). The output will have an extra blank line in it somewhere, which can be eliminated for +1 byte.

Pip is handicapped here by not having a dictionary/hashmap type. Our approach is to build the output as a string, using regex substitutions to update with new assignments. Further explanation available upon request, although I also hope to golf this more. Here's an earlier, pre-golfed version which may be easier to decipher.

C# (Visual C# Interactive Compiler), 128 bytes

x=>{var z=new Dictionary<string,string>();x.ForEach(l=>{var s=l.Pop();l.Any(o=>(z[o]=z.ContainsKey(s)?z[s]:s)=="");});Print(z);}

Try it online!

Batch, 331 317 72 bytes

@setlocal
@for /f "delims==" %%a in ('set')do @set %%a=
@set/a%*
@set

Takes a comma-separated list of assignments on the command line. Explanation:

@setlocal

Don't modify the parent environment.

@for /f "delims==" %%a in ('set')do @set %%a=

Delete all variables, including predefined variables such as PATH. We're only using shell builtins, so we don't need them.

@set/a%*

Evaluate the assignments.

@set

List all of the resulting variables.

Python 3, 159 141 152 128 bytes

def f(s):
	g={}
	for k in s:
		if'='in k:
			*v,l=k.split('=')
			for r in v:
				try:g[r]=int(l)
				except:g[r]=g[l]
	return g

Try it online!

-18 bytes thanks to pxeger

+11 bytes thanks to Shaggy for pointing out a bug

-24 bytes thanks to ovs

Python really isn't my strong suit for golfing :/ Note the use of tabs rather than spaces, so the indentation levels are still a single byte each. Takes input as a list of lines with assignments separated by = (no spaces) and returns a dictionary of variables and values

Python 3, 69 103 bytes

import re
def f(x):g={};exec(re.sub('(^|\n)[^=]+($|\n)','',x).upper(),{},g);return eval(str(g).lower())

Try it online!

+34 bytes to remove no-op lines in the input and avoid undefined variables

Takes advantage of the fact that no python keywords are uppercase, and variable names for this challenge will all be lowercase.

Saves several bytes thanks to a comment on my original (invalid) answer by @ovs:

Note that your original answer could have been 35 bytes with exec(x,{},g), since exec does not add builtins to the locals dictionary. (This is still invalid)

Python 3.9rc2, 67 bytes

def f(x):
 g={}
 for*u,k in x:g|={n:g.get(k,k)for n in u}
 return g

No TIO link, as TIO doesn't support Python 3.9.

Borrows ideas from Artemis' answer, with the following improvements:

Python 3 2, 80 75 69 bytes

-5 bytes thanks to @Sisyphus
-6 bytes thanks to @xnor

g={}
for s in input():
 k=s.pop()
 for n in s:g[n]=g.get(k,k)
print g

Try it online!

Takes input as a list of lists of terms, returns a dict of variable name to value.

Explanation

def f(x,g={}):    # Save a few bytes by defining g as a default argument.
 for s in x:
  k=s.pop(-1)     # Take the last term, which is the value we'll be using.
  for n in s:     # For all *other* values:
   g[n]=g.get(k,k)    # .get(k, k) means "get the value called k, if not found use k raw" (numbers will not be found)
 return g

Note that it never actually differentiates between numbers and variables, just trusts that the input won't try to assign to a number. This means you can actually use it to assign to a number - this input:

[9, 5],
['b', 9],
['c', 'a', 'b'],
['a', 2],
['b', 9]

Will result in this output:

{9: 5, 'b': 5, 'c': 5, 'a': 2}

JavaScript, 52 88 bytes

+36 bytes to handle a single fecking edge case :\

a=>a.map(a=>a.map(k=>o[0+k]=o[0+v]|v,v=a.pop()),o={})&&JSON.stringify(o).split`0`.join``

Try it online!

R, 172 bytes

Takes input as a list of strings, returns a named vector. Just eval in R with aggressive escaping using the A character.

function(i){i=paste(gsub('([a-z_])', 'A\\1',i)[grepl('=',i)],collapse=';')
eval(parse(text=i))
rm("i")
u=ls()
x=sapply(u,function(name)get(name))
names(x)=gsub('A','',u)
x}

Try it online!

Perl 5 -p, 57 bytes

s/[a-z_]+/\$k{'$&'}/g;/=/&&eval}{say"$_=$k{$_}"for keys%k

Try it online!

JavaScript (ES6), 81 bytes

Expects a string in the format described in the challenge. Returns an array of [name, value] pairs.

s=>Object.keys(o={},eval(s.replace(/[_-z]+/g,"o.X$&"))).map(k=>[k.slice(1),o[k]])

Try it online!

How?

We define an object o initially empty and add the prefix "o.X" to all variable names in the input string.

Example:

/* before */ "s = t = u = 6, t = v = 7"
/* after  */ "o.Xs = o.Xt = o.Xu = 6, o.Xt = o.Xv = 7"

We need the leading X to prevent the reserved property __proto__ from being overridden this way.

Provided that the input string is in the expected format -- which is guaranteed by the challenge rules -- the transformed string can be safely eval()'uated. We then iterate on the keys of o to build a list of pairs consisting of 1) the key name without the leading X and 2) the final value associated to the key.

Without the __proto__ issue, this could be done in just 45 bytes without any post-processing:

s=>(eval(s.replace(/[_-z]+/g,"o.$&",o={})),o)

Try it online!

05AB1E, 30 29 bytes

εRćÐþÊiU¯ʒXk_}θθ}δ‚€ˆ}¯.¡н}€θ

Ugh.. :/ Not the right language for the job.

Input as a list of lists.

Try it online or verify all test cases.

Explanation:

ε              # For each list in the (implicit) input-list:
 R             #  Reverse the list
  ć            #  Extract its head; pop and push remainder-list and first item separated
               #  to the stack
   Ð           #  Triplicate this value
    þ          #  Pop one copy, and only leave its digits
     Êi        #  If the top two copies are NOT the same (so it's not an integer):
       U       #   Pop and store the last copy in variable `X`
        ¯      #   Push the global_array
         ʒ     #   Filter it by:
          Xk   #    Where the index of `X`
            _  #    Is 0 (thus the key of the pair)
         }θ    #   After the filter: leave the last pair
           θ   #   Pop and leave its value
      }        #  Close the if-statement
       δ       #  For each value in the remainder-list:
        ‚      #   Pair it with the top value
         €     #  Then for-each pair in this list:
          ˆ    #   Add this pair to the global_array
}¯             # After the outer for-each: push the global_array
  .¡           # Group this list of pairs by:
    н          #  Its first value (the key)
   }€          # After the group-by: map over each group:
     θ         #  And only leave the last pair
               # (after which the top of the stack is output implicitly as result)

Scala, 98 bytes

_./:(Map[String,String]()){case(m,a::b)=>val x=m.getOrElse(a,a);(m/:b.map(_->x))(_+_)case(m,_)=>m}

Try it online!

The statements have to be reversed (List("2","b") for "b=2"). The solutions below can't handle empty input.

Scala, 96 94 bytes

_./:(Map[String,String]()){(m,l)=>val x=m.getOrElse(l.last,l.last);(m/:l.init.map(_->x))(_+_)}

Try it online!

Takes a List[List[String]] and returns a Map[String,String]

Scala, 86 bytes

This is shorter, but the statements are reversed

_./:(Map[String,String]()){case(m,a::b)=>val x=m.getOrElse(a,a);(m/:b.map(_->x))(_+_)}

Try it online!

Red, 74 69 bytes

func[b][context collect[forall b[if set-word? first t: b/1[keep t]]]]

Try it online!

Takes the input as a list of lists, in each of them = replaced with : (Red has set-words rather than assignment operator)

Java 10, 137 bytes

a->{var r=new java.util.TreeMap();for(var p:a)for(int l=p.length-1,i=l;i-->0;)r.put(p[i],p[l]instanceof Long?p[l]:r.get(p[l]));return r;}

Input as a Object-matrix (variables as Strings, values as Longs), output as a sorted HashMap.

Try it online.

Explanation:

a->{                            // Method with Object-matrix parameter & TreeMap return
  var r=new java.util.TreeMap();//  Create the result sorted HashMap
  for(var p:a)                  //  Loop over each Object-list of the input-matrix:
    for(int l=p.length-1,       //   Integer `l`, set to the last index of the list
        i=l;i-->0;)             //   Inner loop `i` in the range (length-1, 0]:
      r.put(                    //    Add to the result TreeMap:
         p[i],                  //     The `i`'th value of the list as key
         p[l]instanceof Long?   //     If the last item is a Long:
          p[l]                  //      Use that last item as value
         :                      //     Else:
          r.get(p[l]));         //      Get the value of this last item from the
                                //      result-Map, and use that as value
  return r;}                    //  Return the resulting TreeMap (sorted HashMap)

Retina 0.8.2, 85 bytes

G`=
+`=(.+(=.+))
$2¶$1
+rm`(^\4=(.+)¶(.+¶)*?.+=)(.+)$
$1$2
+m`^(.+)=.+¶((.+¶)*\1=)
$2

Try it online! Link includes test suite that converts the input from comma separated to newline separated assignments with no spaces. Explanation:

G`=

Ignore statements that have no assignments.

+`=(.+(=.+))
$2¶$1

Split up assignment chains into individual assignments.

+rm`(^\4=(.+)¶(.+¶)*?.+=)(.+)$
$1$2

Substitute the values of variables used on the right-hand side of assignments. The matching is performed right-to-left so that the most recent value is used.

+m`^(.+)=.+¶((.+¶)*\1=)
$2

Remove superseded assignments.

Ruby, 63 bytes

def f(a)
a.reduce({}){|m,x|*r,k=x
r.map{|y|m[y]=m[k]||k}
m}
end

Try it online!

I rarely golf in Ruby (tips appreciated) but I use it for work, and I liked Artemis's clean answer so much that I decided to see what a translation into ruby would look like.

J, 66 bytes

33 bytes for the _ =: 1 special case …

(rplc&('a0';'_')@}.~&_6;".)&>@r0[0!:110@rplc&('_';'a0')[r0=:4!:5@1

Try it online!

How it otherwise works

(_6&}.;".)&>@r0[0!:110[r0=:4!:5@1

It's a mess! m!:n are special functions, that do stuff depending on m and n.

SNOBOL4 (CSNOBOL4), 183 bytes

	T =TABLE()
N	X =INPUT	:F(O)
R	X SPAN(&LCASE '_') . Y (' ' | RPOS(0)) . Z ='T<"' Y '">' Z	:S(R)
	EVAL(X)	:(N)
O	A =CONVERT(T,'ARRAY')
I	I =I + 1
	OUTPUT =A<I,1> ' = ' A<I,2>	:S(I)
END

Try it online!

Takes input separated by newlines with spaces between the =, and returns in the same format.

APL (Dyalog Unicode), 22 bytes

{n⊣⍵{0::0⋄⍵⍎⍺}¨n←⎕NS⍬}

Try it online!

Takes a list of statements in the form of a←b←3, and returns a namespace which is essentially a hashmap of variable names to values. You can't print all the contents of it directly, but you can inspect individual variables like ns.somevar or list all names using ns.⎕NL ¯2.

Oh, and APL doesn't have any alphanumeric-only keywords!

{n⊣⍵{0::0⋄⍵⍎⍺}¨n←⎕NS⍬}  ⍝ ⍵: list of statements
               n←⎕NS⍬   ⍝ Create an empty namespace
   ⍵{        }¨         ⍝ For each statement...
          ⍵⍎⍺           ⍝ Try executing the statement inside the namespace
     0::0⋄              ⍝ ignoring any errors (undefined name)
 n⊣                     ⍝ Return the populated namespace