Basic Perl - pass by ref


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Basic Perl - pass by ref
# 1  
Old 07-08-2011
Basic Perl - pass by ref

Code:
 
#!/usr/bin/perl
 
sub addToHash{
  my(%hash) = @_;
 $hash{ 'A' } = 'value';
 $hash{ 'B' } = 'value';
 $hash{ 'C' } = 'value';
 print("Hashmap size is " .keys(%hash) . "\n");
}
 
my %myhash = ();
addToHash(\%myhash);
print("Hashmap size is " .keys(%myhash) . "\n");


I want this to print out "Hashmap size is 4" twice but it doesnt seem to be passing by refrence. What am I doing wrong?
# 2  
Old 07-08-2011
It will become clear once you start to use warnings;
Code:
Reference found where even-sized list expected at hash.pl line 5.

You're passing in an reference, but your sub is expecting a regular hash (which is really an even sized array). Try these lines instead:
Code:
my ( $href ) = @_;
my %hash = %$hash;

# 3  
Old 07-08-2011
Code:
 
#!/usr/bin/perl

sub addToHash{
 my ($href) = @_;
 $href{ 'A' } = 'value';
 $href{ 'B' } = 'value';
 $href{ 'C' } = 'value';
 print("Hashmap size is " .keys(%href) . "\n");
}
 
my %myhash = ();
addToHash(\%myhash);
print("Hashmap size is " .keys(%myhash) . "\n");

Still just prints out

Hashmap size is 3
Hashmap size is 0
# 4  
Old 07-08-2011
Read my post above again, especially the part about adding lines (plural). my ($href) = @_; creates a reference to the hash you're passing. The very next line, $href{ 'A' } = 'value';, creates a new hash (%href) that is completely distinct from the reference you've passed in, aside from the name.

Serious recommendation: always use these 3 lines as the start of your Perl scripts, they will help you catch a lot of errors:
Code:
#!/usr/bin/perl -w
use strict;
use warnings;

# 5  
Old 07-08-2011
Code:
#!/usr/bin/perl

use warnings;
use strict;

sub addToHash {
 my $href = $_[0];
 $href->{ 'A' } = 'value';
 $href->{ 'B' } = 'value';
 $href->{ 'C' } = 'value';
 print "Hashmap size is ". keys( %$href ) . "\n";
}

my %myhash = ();
addToHash \%myhash;
print "Hashmap size is " . keys( %myhash ) . "\n";

# 6  
Old 07-08-2011
Code:
$
$
$ cat -n testref.pl
     1  #!perl
     2
     3  sub addToHash{
     4    $href = shift;
     5    ${$href}{'A'} = 'xxx';
     6    ${$href}{'B'} = 'yyy';
     7    ${$href}{'C'} = 'zzz';
     8    print("Hashmap size is " .keys(%{$href}) . "\n");
     9  }
    10
    11  my %myhash = ();
    12  addToHash(\%myhash);
    13  print("Hashmap size is " .keys(%myhash) . "\n");
    14
$
$
$ perl testref.pl
Hashmap size is 3
Hashmap size is 3
$
$
$

tyler_durden
# 7  
Old 07-08-2011
Thanks I was just missing the "->" symbol
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Basic Question on perl use POSIX [SOLVED]

Hi guys, I think this is a basic question. I'm not very familiar with this. I'm trying to round a number up and round a number down. From what I have read this can be done using POSIX. I have tried to to use this, but i'm getting errors: sub findGridBounds($$$%) { use POSIX; ... (0 Replies)
Discussion started by: WongSifu
0 Replies

2. Shell Programming and Scripting

Perl basic code 3

Hi , I am having csv contains data 2011-08-23 11:11:00.074+0000: Info: Duplicate Order is not processed,Original Order Tuple ($category, $type) = split(',', $_ , 2); what is the split function work here?? Thanks Please start using code tags, thanks. (5 Replies)
Discussion started by: pspriyanka
5 Replies

3. Shell Programming and Scripting

perl ref to hash with refs in it (how to get what's being referenced).

I have a reference to a hash that contains some references. I was just wondering if there was a more simplistic Way of dereferencing the contained references without having to assign them to another reference like this: my $href = shift; #some hash my $temp = $href->{element}; print... (3 Replies)
Discussion started by: mrwatkin
3 Replies

4. Shell Programming and Scripting

perl basic code

Hi, foreach $cat (sort{$cats{$b} <=> $cats{$a}} keys %cats) can anyone explain me above code?? i am new to perl Thanks (1 Reply)
Discussion started by: pspriyanka
1 Replies

5. Shell Programming and Scripting

perl basic code

Hi, can anybody explain me below code $cats{$category}++ Thanks (2 Replies)
Discussion started by: pspriyanka
2 Replies

6. Programming

Basic perl code- Date format

Hi friends, Please see the below code carefully. ======================================================= # Get batch date and Ord range open OR,$ARGV; while (<OR>) { # find the batch date next if length $_ < 3; # BLANK LINE # last if $. > 120; # sample should be good enough... (2 Replies)
Discussion started by: pspriyanka
2 Replies

7. Programming

Basic perl code

hi, can anybody explain me the below code. i am new to perl ========================================== $o_dups{$1} = 1 if /^\w+\t.{19}\t(+),/; ========================================== regards, priyanka (2 Replies)
Discussion started by: pspriyanka
2 Replies

8. Shell Programming and Scripting

perl basic multiple pattern matching

Hi everyone, and thank you for your help with this. I am VERY new with perl so all of your help is appreciated. I have tried google but as I don't know the proper terms to search for and could be daunting for a newbie scripter... I know this is very easy for most of you! Thanks! I have a... (4 Replies)
Discussion started by: sinusoid
4 Replies

9. Shell Programming and Scripting

Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable Something like:perl -p -e 's/$shell_srch/$shell_replace/g' input.txt I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.) I have searched and found... (4 Replies)
Discussion started by: alex_5161
4 Replies

10. UNIX for Dummies Questions & Answers

basic perl question

Hi, I was jus goin through a ebook on perl....it says u get binary installation for both unix and windows.....doesnt perl come already bundeled with unix ?cause i never installed any perl from binary........but i am able to execute perl programs...... Thanks and Regards Vivek.S (1 Reply)
Discussion started by: vivekshankar
1 Replies
Login or Register to Ask a Question