Register
It is currently Wed Dec 13, 2017 12:42 pm

Calling functions through array elements


All times are UTC - 6 hours


Post new topic Reply to topic  [ 13 posts ] 
Author Message
 PostPosted: Mon Feb 08, 2016 9:58 pm   

Joined: Fri Nov 27, 2015 12:24 am
Posts: 11
Alright, so I have an array, and one of its elements is the same as the title of a function that I have. Is there any way to call this function through the array?


Top
 Profile  
 PostPosted: Mon Feb 08, 2016 10:32 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 273
Sure there is! It works just like if you were to set a command to a variable (ex: cat=cat; file=file.txt; $cat $file)

Here's an example that I hope is what you're looking for:

Code:
([email protected])-> cat test.sh
function hello {
    echo "Hello World"
}

array=(foo bye hello bar)
${array[2]}

([email protected])-> /bin/bash ./test.sh
Hello World


I hope this helps!


Top
 Profile YIM  
 PostPosted: Tue Feb 09, 2016 2:04 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
Do not hold code in vars or arrays!!

Rethink your logic, if you think this can be a solution.
It never is.

Never ever hold code in vars or arrays!!!


Top
 Profile  
 PostPosted: Fri Feb 12, 2016 12:53 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 638
hi,

I usually wouldn't, but in this case, I'd do the same as jeo suggests, as only functions' names are in the array, and no complicated code.

can you elaborate your objection, uhelp, please ?


Top
 Profile  
 PostPosted: Fri Feb 12, 2016 4:37 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
If there are only function names in an array, it is just an unneeded level of indirection.
Whatever can be done with this approach can be done without an array as well.

What's the point having func names in an array?
To decide which element of an array should be called, one needs the logic anyway.
So why not calling em directly?

Indirection of vars and arrays does can be used with values pretty well.
But not with function names.
And definitely not with a kind of self- modifying code.


Top
 Profile  
 PostPosted: Fri Feb 12, 2016 10:38 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 638
and what if collected data have in its structure a number /name that defines/suggests what treatment must be done

Code:
1;321;data to be treated the namesDoNotMatter_X's way;...
2;45;data to be treated the function_A's way;...
3;321,data to be treated the namesDoNotMatter_X's way;...
4;56;data to be treated the function_B's way;...
5;321;data to be treated the namesDoNotMatter_X's way;...

and functions
Code:
function_A() { :treat like A;}
function_B() { :treat like B;}
namesDoNotMatter_X() { :treat like nDNM_X;}

wouldn't having an array be practical
Code:
funcList=( [45]=function_A [56]=function_B [321]=namesDoNotMatter_X )
and parsing data
Code:
while IFS=';' read order funcNmb data; do ${funcList[funcNmb]} "$data"; done < dataFile

how would you do ?


Last edited by Watael on Fri Feb 12, 2016 2:00 pm, edited 2 times in total.

Top
 Profile  
 PostPosted: Fri Feb 12, 2016 10:46 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
How do you handle the case where data contain such a triggering sequence of chars but treatment shouldn't be triggered by it?

If you rely on things linke that, it is just bad style.
You have to check it anyway.

Of course one can built up an example where it seems reasonable to do so.
I consider it bad style anyway.


Top
 Profile  
 PostPosted: Fri Feb 12, 2016 1:59 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 638
Quote:
How do you handle the case where data contain such a triggering sequence of chars but treatment shouldn't be triggered by it?
i forgot to define the IFS :( corrected ! :)
data may contain anything, only funcName (corrected too) is used


Top
 Profile  
 PostPosted: Fri Feb 12, 2016 4:47 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
This does not change the play.
In my opinion it is still bad practice.

If one ensures that data contain proper function calls, it still does not rule out my argument.
Write the script properly and there is no need to ensure function names appear within data shell proof.
This introduces a second place to take care off.

Of course you can do write scripts like that.
But they tend to break.
I prefer writing good code avoiding doubt-able paradigms.


Top
 Profile  
 PostPosted: Fri Feb 12, 2016 11:14 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 638
no matter what's been said previousvly, how would you do from this
Code:
1;321;data to be treated the namesDoNotMatter_X's way;...
2;45;data to be treated the function_A's way;because field #2 is 45
3;321,data to be treated the namesDoNotMatter_X's way;because field #2 is 321
4;56;data to be treated the function_B's way;because field #2 is 56
5;321;data to be treated the namesDoNotMatter_X's way;because field #2 is 321
simple case/esac testing second field ?
any other way?


Top
 Profile  
 PostPosted: Sat Feb 13, 2016 1:19 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 693
Location: Des Moines, Iowa
I actually have just completed a similar script to what your describing above, and I used case/esac to filter 59 different options and do something based on that. I personally like case/esac simply because it's very clear to me what it's doing when running through the code. The whole use a variable from an array as code never occured to me, however I find it pretty cool that you can.

Just because you can, doesn't mean you should, and just because it's not the "normal" way, or "approved" way, definately does not mean you can't do it that way if you want.... just hope the guy that maintains your code after your done isn't a vicious psycopath that knows where you live. :) :)


Top
 Profile WWW  
 PostPosted: Sat Feb 13, 2016 3:45 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
uhelp wrote:
Of course you can do write scripts like that...
I prefer writing good code avoiding doubt-able paradigms.


Top
 Profile  
 PostPosted: Sat Feb 13, 2016 6:19 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 638
there is no doubt in my mind, or when I read some of my scripts using this method, yet.

to each his own (?)

if a violent psychopath finds me, we'll have stories to tell each other. :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 20 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP