More Translator questions.

Posted by Ranthony on Sun 18 May 2003 02:35 AM — 6 posts, 25,183 views.

#0
Alright, I'm having a bit of difficulty assigning things to a hash. I'll explain a bit of what I'm trying to do before I paste the code in. I've got a while loop that reads in a line from the file, and splits it by the '=' sign, the first element resulting from the split is the drow word, the second is the english. I want to put all of the words into this hash with the english word as the key to each element. Here's what I've gotten so far.

#!/usr/bin/perl
print"\n";
print "\nLoading drow language list.\n";
open(DROWLANGUAGEFILE, "DrowLanguage.txt") || die "Cannot open DrowLanguage.txt";
#@drowlanguage #I can't initialize this here, bah. scope #will be off
while (<DROWLANGUAGEFILE>)
{
#chomp; #TEST CODE
#print "$_\n"; #TEST CODE
# We're going to read each line in and assign the words to an element in the hash.

@tmp= split(/=/,$_); #split the lines using the = sign.
#print"@tmp[0].....@tmp[1]\n"; #TEST CODE
@drowlanguage={@tmp[0],@tmp[1]}; #hoping this #appends to the hash, but it doesn't appear to.
#####below is test code to make sure the hash is
#right

print"Printing \@drowlanguage\n";
for($jack=0; $jack<1000; $jack++)
{
print"$drowlanguage[$jack]";
}


}


My main problems are scope; do I need to declare the hash outside of that block? Second is appending elements to the hash, this can be done right? Sorry if this is elementary, but I'm doing this off of code snippets found online, and I don't have a perl book yet.



EDIT: wow, took out all the formatting. Nasty.
Amended on Sun 18 May 2003 05:48 AM by Ranthony
Australia Forum Administrator #1
Look at "forum codes" when you do a post. Put [code] and [/code] around formatted code and it will come out OK.

As for the code, I am not a Perl expert, although what you have there looks a bit of a mess.

After a bit of experimenting I think you need this to set up the associative array:

$drowlanguage {@tmp [0] } = @tmp [1];

If you do that for each line you will build up the array (which should be in global scope or it will be gone afterwards).

Then to access a particular word, you would do this:

$myword = $drowlanguage { $original_word };
Australia Forum Administrator #2
To test the array you could do this:


foreach $item (%drowlanguage)
  {
  note ($item);
  }
#3
The following works fine for me from shell.

I assume that you are trying to read a file in with the form:

drow1=english1
drow2=english2

and so on.

A couple of notes: your shebang line doesn't make much sense in terms of windows. Also, your die message will never be seen in Mush, you might want to consider doing a $world->send.


#!perl

open(IN, "DrowLanguage.txt") or die "Open failed: $!";

# Load up the hash
while (<IN>)
{
	chomp;
	($drow, $eng) = split(/=/, $_);
	$hDrow{$eng} = $drow;
}

# Prove that we filled it. (Test only)

foreach $a (sort(keys(%hDrow)))
{
	print "English: $a Drow: $hDrow{$a}\n";
}
Amended on Tue 20 May 2003 04:51 PM by Dubthach
Australia Forum Administrator #4
Excellent, now I start to see some of the elegance of Perl. :)

BTW - do you not need to close the file?
#5
Yeah, good call...totally forgot to close the file.

close(IN) or die "Couldn't close: $!";