A way to get variables back from a function... Needed this!

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

A way to get variables back from a function... Needed this!

#1 Post by sunburnt »

I found a way to get variables back out of a function. ( I`m sure others have thought of it...)
Here`s the code, could solve problems with getting variables into gtkdialog from functions:

Code: Select all

guiPOS() {
  G=($(xwininfo -name "$1" |sed '4,9!d'))                          # get size and pos. for GUI
  ((X=G[3]-G[11], Y=G[7]-G[15], W=G[17], H=G[19]))
  ((X<1)) && ((X=0)); ((Y<1)) && ((Y=0)) ; echo "X=$X,Y=$Y,W=$W,H=$H"
}
overPOS() {
   ((`guiPOS $1`))                                                              # get main GUI Left and top
   popX=$(($X+$offX)) ; popY=$(($Y+$offY))                # get X and Y position offsets
}
As long as the function`s returned value is one long string with no spaces it`s OK...
The evaluation parentheses take "," instead of Bashes taking ";" for evaluation separators.
How to "can" this in a library? Another words, make it call just like a function does now.
A fix for Bash`s function, call it like a normal function and get back evaluated variables.
Sort of like having the ". " run in the same shell for functions, I`d call this a big fix.

ken geometrics
Posts: 76
Joined: Fri 23 Jan 2009, 14:59
Location: California

#2 Post by ken geometrics »

Also:
If you have a list of values to return but don't want to force the names they go into, you can do something like this:

:~> CC="987654 65432"
:~> read AA BB <<-XYZZY
> $CC
> XYZZY
:~> echo "$AA"
987654
:~> echo "$BB"
65432


The $CC gets expanded into the here document. This means you can feed its contents into a command as though it came from a file.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#3 Post by sunburnt »

Hi ken geometrics; Your code looks really interesting, I`ve never seen syntax like it...
But I can`t see how it works, could you edit it into a function with it`s call below?

Code: Select all

f() {
# Function code
}
f ARG
# Output code
I assume it`s doing something different than like a function returning a comma delimited string.
echo "$X,$Y,$W,$H"

potong
Posts: 88
Joined: Fri 06 Mar 2009, 04:01

#4 Post by potong »

Try these:

Code: Select all

# unset a b c                                                                                                 
# a="b=bbb c=ccc"
# echo $a
b=bbb c=ccc
# eval $a
# echo $a $b $c
b=bbb c=ccc bbb ccc
# unset a b c
# echo $a $b $c

# a="b=bbb c=ccc"
# echo $a >/tmp/a
# . /tmp/a
# echo $a $b $c
b=bbb c=ccc bbb ccc
# unset a b c
# a="b=bbb c=ccc"
# . <<<$a
bash: .: filename argument required
.: usage: . filename [arguments]
N.B. the here-string doesn't work :(

Potong

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#5 Post by sunburnt »

Hi potong; Variables within variables, yes... Really nifty is commands within variables!
The gtkinfo code I posted returns an info list of variables of all gtkdialogs within a variable.

Post Reply