Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Question about hashes of hashes

Reply
Thread Tools

Question about hashes of hashes

 
 
djacober
Guest
Posts: n/a
 
      07-24-2005
Hi all

I tried to pass a hash of hashes as a reference to a subroutine but
everytime I try to make an operation with to the hash I get the
following mistake. I found a lot of information on the web but it's
still not clear to me what I can do about it

Here's the error message I get:

djacober@tux ~/perl $ perl -w detect_eyes.pl > eyes.out
Can't use string ("1") as a HASH ref while "strict refs" in use at
detect_eyes.pl line 126.

Here's a the snippet of my code that causes the headache:

sub detect_eye {

my $ref_color = @_;
foreach my $line (@board_number) {
VALUE: foreach my $alpha (@board_alpha) {

## this line doesn't work !!!!
126: print "$line,$alpha : $ref_color->{$line}{$alpha} \n";


Here's how I call the subroutine:

sub main {
read_board();
detect_eye(\%white);
}


Can any one help?

Thanks for your support
Daniel
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-24-2005
djacober wrote:
>
> djacober@tux ~/perl $ perl -w detect_eyes.pl > eyes.out
> Can't use string ("1") as a HASH ref while "strict refs" in use at
> detect_eyes.pl line 126.


<random code snippets snipped>

> Can any one help?


If you make it easy to help, I'm sure someone can.

Please post a short but _complete_ program that people can copy and run
and that exhibits the problem you're having!

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-24-2005
djacober wrote:
> Gunnar Hjalmarsson wrote:
>> djacober wrote:
>>> Can any one help?

>>
>> If you make it easy to help, I'm sure someone can.
>>
>> Please post a short but _complete_ program that people can copy and
>> run and that exhibits the problem you're having!

>
> Sorry I didn't want to overload with code:
>
> Here's the complete code


Well, I didn't ask for the _complete_ code either.

Now, Sven-Thorsten found the mistake anyway, but for next time, please
study the posting guidelines for this group to better understand what I
was after.

http://mail.augustmail.com/~tadmc/cl...uidelines.html

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
djacober
Guest
Posts: n/a
 
      07-24-2005
Gunnar Hjalmarsson wrote:
> djacober wrote:
>
>>
>> djacober@tux ~/perl $ perl -w detect_eyes.pl > eyes.out
>> Can't use string ("1") as a HASH ref while "strict refs" in use at
>> detect_eyes.pl line 126.

>
>
> <random code snippets snipped>
>
>> Can any one help?

>
>
> If you make it easy to help, I'm sure someone can.
>
> Please post a short but _complete_ program that people can copy and run
> and that exhibits the problem you're having!
>


Sorry I didn't want to overload with code:

Here's the complete code

Regards Daniel

#!/usr/bin/perl
#
################################################## ###############################
#
# detect_eyes.pl : perl program to find the double eyes in a go game
#
# Filename : detect_eyes.pl
#
# Version : 1.0
#
# Author : D. Jacober, DSAI 2001 8. Semester KI
# Date : 05.07.2005
# Changes :
#
# Input : Text file on the command line with a go board
# Output : Displays double eyes on the go board
#
################################################## ###############################

use strict;
use warnings;
use Time::localtime;
use IO::Handle;
###use Sort::Fields;

# Variablen deklarieren
my $infile = shift || "example.txt";
my $outfile = shift || "double_eyes.txt";
my %board = ();
my %white = ();
my %black = ();
my %empty = ();
my @open = ();
my @closed = ();
my @current = ();
my @board_number = (1,2,3,4,5);
my @board_alpha = ("A","B","C","D","E");
my $max_num = 5;
my $min_alpha = "A";
my $max_alpha = "F";

################################################## ###############################
# sub to read input file
################################################## ###############################

sub has_neighbour {
my ($line,$alpha) = @_;
my $value = $white{$line}{$alpha};
my $next_alpha=chr(ord($alpha)+1);
my $before_alpha=chr(ord($alpha)-1);
my $has_neighbour = 0;
my @new_member = ();

LINE: foreach my $num ($line-1,$line,$line+1) {
next LINE if ( ($num < 1) || ($num > $max_num) );

ALPHA: foreach my $char ($before_alpha,$alpha,$next_alpha) {
next ALPHA if ( ($num == $line) && ($alpha eq $char) );
next ALPHA if ( ($char lt $min_alpha) || ($char gt $max_alpha) );

if ( exists($white{$num}{$char}) && $value eq $white{$num}{$char}) {

foreach my $in_open (@open) {
next LINE if ("$num $char" eq $in_open);
}

foreach my $in_current (@current) {
next LINE if ("$num $char" eq $in_current);
}

print "inserting $num $char in open\n";
$new_member[$has_neighbour++] = "$num $char";
}

}
}

push(@new_member, @open);
@open = @new_member;
return $has_neighbour;
}

sub read_board {
my @alpha_coordinate;

open(INFILE,$infile) or die " cannot open $infile ";

while(<INFILE>) {
s/^\s//g;
chomp;
if (s/^\///) {
s/^\s//g;
@alpha_coordinate = split(/ /);
} elsif (s/^(\d+)//) {
my $line_no = $1;
s/^\s//g;
my @line = split / /;
my $n = 0;
foreach my $field (@line) {

#full board with all figures
$board{$line_no}{$alpha_coordinate[$n]} = $field;

#separate hashes for each color type (black, white and empty)
if ($field =~ /0/) {
$white{$line_no}{$alpha_coordinate[$n]} = $field;
} elsif ($field =~ /X/) {
$black{$line_no}{$alpha_coordinate[$n]} = $field;
} else {
$empty{$line_no}{$alpha_coordinate[$n]} = $field;
}

$n++;
}
} else { die "wrong input line \n"; }
}
close(INFILE);

}

sub detect_eye {

my $ref_color = @_;
foreach my $line (@board_number) {
VALUE: foreach my $alpha (@board_alpha) {
print "$line,$alpha : $ref_color->{$line}{$alpha} \n";

if (exists($white{$line}{$alpha}) ) {

if (has_neighbour($line,$alpha) > 1) {unshift(@current, "$line
$alpha");}

while ( $#open != -1 ) {
print "@open \n";
print "@current \n";
my ($num, $char) = split(/ /,shift(@open));

if (has_neighbour($num,$char) ) {unshift(@current, "$num
$char");}

}
exit;
}

}
}
}

sub main {
read_board();
detect_eye(\%white);
}

main();


 
Reply With Quote
 
djacober
Guest
Posts: n/a
 
      07-24-2005
Sven-Thorsten Fahrbach wrote:
> On Sun, 24 Jul 2005 22:39:31 +0200
> djacober <(E-Mail Removed)> wrote:
>
>
>>Hi all
>>
>>I tried to pass a hash of hashes as a reference to a subroutine but
>>everytime I try to make an operation with to the hash I get the
>>following mistake. I found a lot of information on the web but it's
>>still not clear to me what I can do about it
>>
>>Here's the error message I get:
>>
>>djacober@tux ~/perl $ perl -w detect_eyes.pl > eyes.out
>>Can't use string ("1") as a HASH ref while "strict refs" in use at
>>detect_eyes.pl line 126.
>>
>>Here's a the snippet of my code that causes the headache:
>>
>>sub detect_eye {
>>
>> my $ref_color = @_;

>
>
> Wrong context. This doesn't mean 'give me the first element of @_' (which is what you probably had in mind) but 'give me the number of elements in the list'. You pass the hashref \%white to the sub, so @_ contains one element, i.e. 1 is being assigned to $ref_color, hence the error message 'Can't use string ("1")...'. What you want to do is either:
> 1 - The C way : my $ref_color = $_[0]; or
> 2 - The Perl way: my $ref_color = shift;
> shift() operates on @_ if no additional arguments are given and you should use it instead of accessing individual array elements by their subscripts, which is slower.
> perldsc is also a good reference if you run into trouble with hashes of hashes or other data structures.


Yes you're a Genious so simple but took me some hours of my life seeking
in the docs

Thanks a lot

Regards Daniel
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-24-2005
Sven-Thorsten Fahrbach wrote:
> djacober wrote:
>>
>>sub detect_eye {
>>
>> my $ref_color = @_;

>
> What you want to do is either:
> 1 - The C way : my $ref_color = $_[0]; or
> 2 - The Perl way: my $ref_color = shift;


or

3 - Another Perl way: my ($ref_color) = @_;

TIMTOWDI

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Sven-Thorsten Fahrbach
Guest
Posts: n/a
 
      07-24-2005
On Sun, 24 Jul 2005 22:39:31 +0200
djacober <(E-Mail Removed)> wrote:

> Hi all
>
> I tried to pass a hash of hashes as a reference to a subroutine but
> everytime I try to make an operation with to the hash I get the
> following mistake. I found a lot of information on the web but it's
> still not clear to me what I can do about it
>
> Here's the error message I get:
>
> djacober@tux ~/perl $ perl -w detect_eyes.pl > eyes.out
> Can't use string ("1") as a HASH ref while "strict refs" in use at
> detect_eyes.pl line 126.
>
> Here's a the snippet of my code that causes the headache:
>
> sub detect_eye {
>
> my $ref_color = @_;


Wrong context. This doesn't mean 'give me the first element of @_' (which is what you probably had in mind) but 'give me the number of elements in the list'. You pass the hashref \%white to the sub, so @_ contains one element, i.e. 1 is being assigned to $ref_color, hence the error message 'Can't use string ("1")...'. What you want to do is either:
1 - The C way : my $ref_color = $_[0]; or
2 - The Perl way: my $ref_color = shift;
shift() operates on @_ if no additional arguments are given and you should use it instead of accessing individual array elements by their subscripts, which is slower.
perldsc is also a good reference if you run into trouble with hashes of hashes or other data structures.
 
Reply With Quote
 
Sven-Thorsten Fahrbach
Guest
Posts: n/a
 
      07-24-2005
On Sun, 24 Jul 2005 22:57:44 +0200
djacober <(E-Mail Removed)> wrote:

> Yes you're a Genious so simple but took me some hours of my life seeking
> in the docs
>
> Thanks a lot
>
> Regards Daniel


This won't be the last time you encounter something like that . It's often the most simple things that take longest to fix.
Happy coding.

SveTho
 
Reply With Quote
 
Tom
Guest
Posts: n/a
 
      07-24-2005

"Sven-Thorsten Fahrbach" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sun, 24 Jul 2005 22:39:31 +0200
> djacober <(E-Mail Removed)> wrote:
>
> > Hi all
> >
> > I tried to pass a hash of hashes as a reference to a subroutine but
> > everytime I try to make an operation with to the hash I get the
> > following mistake. I found a lot of information on the web but it's
> > still not clear to me what I can do about it
> >
> > Here's the error message I get:
> >
> > djacober@tux ~/perl $ perl -w detect_eyes.pl > eyes.out
> > Can't use string ("1") as a HASH ref while "strict refs" in use at
> > detect_eyes.pl line 126.
> >
> > Here's a the snippet of my code that causes the headache:
> >
> > sub detect_eye {
> >
> > my $ref_color = @_;

>
> Wrong context. This doesn't mean 'give me the first element of @_' (which

is what you probably had in mind) but 'give me the number of elements in the
list'. You pass the hashref \%white to the sub, so @_ contains one element,
i.e. 1 is being assigned to $ref_color, hence the error message 'Can't use
string ("1")...'. What you want to do is either:
> 1 - The C way : my $ref_color = $_[0]; or
> 2 - The Perl way: my $ref_color = shift;
> shift() operates on @_ if no additional arguments are given and you should

use it instead of accessing individual array elements by their subscripts,
which is slower.
> perldsc is also a good reference if you run into trouble with hashes of

hashes or other data structures.

I am still having trouble with why "1" is not X0001
or some such.


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      07-25-2005
Tom <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> "Sven-Thorsten Fahrbach" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > On Sun, 24 Jul 2005 22:39:31 +0200
> > djacober <(E-Mail Removed)> wrote:


[...]

> > > my $ref_color = @_;

> >
> > Wrong context. This doesn't mean 'give me the first element of @_' (which

> is what you probably had in mind) but 'give me the number of elements in the
> list'. You pass the hashref \%white to the sub, so @_ contains one element,
> i.e. 1 is being assigned to $ref_color, hence the error message 'Can't use
> string ("1")...'. What you want to do is either:


[...]

> I am still having trouble with why "1" is not X0001
> or some such.


$ref_color becomes the number of elements in the array @_. In the given
case the number is 1. It is not X0001 and will never be.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
On Hashes - How the hashes printing works? Neela megha shyam Chivukula Ruby 4 05-28-2009 10:56 AM
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
using hashes as keys in hashes Steven Arnold Ruby 3 11-23-2005 03:25 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments