LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2019, 09:38 AM   #1
new_user3085
LQ Newbie
 
Registered: Sep 2016
Posts: 21

Rep: Reputation: Disabled
Reference to a function


Hi All,

Could you look at the below script and help me understand why the output is in reverse order.

Code:
lab@a128:~/mydir> cat ref2fn
#!/usr/bin/perl
# Function definition
sub PrintHash {
    my (%hash) = @_;

    foreach $item (%hash) {
        print "Item: $item\n";
    }
}
%hash = ('name' => 'Tom', 'age' => 19);
%hash1 = ('name' => 'Tommy', 'age' => 39, 'location' => 'Utah');

#Create a reference to above function.
$cref = \&PrintHash; #Uncomment after test
$cref1 = &PrintHash(%hash1);
print "Valud of \$cref is: $cref\n";

#Function call using reference.
&$cref(%hash);  #Uncomment after test
&$cref1(%hash1);  #Uncomment after test
lab@a128:~/mydir> ./ref2fn
Item: location
Item: Utah
Item: name
Item: Tommy
Item: age
Item: 39
Valud of $cref is: CODE(0x7b3f20)
Item: name
Item: Tom
Item: age
Item: 19
Undefined subroutine &main:: called at ./ref2fn line 20.

lab@a128:~/mydir>

I was expecting the below output to print first

Code:
Item: name
Item: Tom
Item: age
Item: 19
Why is this so? Please explain.
Also, it's complaining
Code:
Undefined subroutine &main:: called at ./ref2fn line 20.
in the last line of the output. What is it complaining about?

Thanks in advance!! Appreciate your time.

Last edited by new_user3085; 02-13-2019 at 09:41 AM.
 
Old 02-13-2019, 09:44 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,901

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
probably you missed a backslash here: $cref1 = \&PrintHash(%hash1)
This will cause what you asked.

the order of items in a hash is not specified, it is more or less random.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Latex, Hyperlink to a reference and from reference to where it is used roy1360 Linux - Software 2 03-21-2011 01:37 PM
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
undefined reference to a function included in a library i made!!! keos Programming 5 02-21-2004 04:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration