Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Perlscript
➜ More Translator questions.
More Translator questions.
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Ranthony
(12 posts) Bio
|
Date
| Sun 18 May 2003 02:35 AM (UTC) Amended on Sun 18 May 2003 05:48 AM (UTC) by Ranthony
|
Message
| 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. | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #1 on Sun 18 May 2003 07:14 AM (UTC) |
Message
| 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 };
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #2 on Sun 18 May 2003 07:16 AM (UTC) |
Message
| To test the array you could do this:
foreach $item (%drowlanguage)
{
note ($item);
}
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Dubthach
(47 posts) Bio
|
Date
| Reply #3 on Tue 20 May 2003 04:50 PM (UTC) Amended on Tue 20 May 2003 04:51 PM (UTC) by Dubthach
|
Message
| 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";
}
| Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #4 on Tue 20 May 2003 08:22 PM (UTC) |
Message
| Excellent, now I start to see some of the elegance of Perl. :)
BTW - do you not need to close the file? |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Dubthach
(47 posts) Bio
|
Date
| Reply #5 on Tue 20 May 2003 08:31 PM (UTC) |
Message
| Yeah, good call...totally forgot to close the file.
close(IN) or die "Couldn't close: $!"; | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
19,563 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top