Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Populating HASH from file
Newbie here,

I need to dump a plain text file that has the format of:

key = value (ie. question <equal sign> answer)

into a hash.  I'm writing a simple learning tool to help me learn some
basic korean terminology.

Would someone be kind enough to point me in the right direction.

-Jeff Borders

korean.txt:

One = Hana
Two = Dool
Three = Set
etc.

*********************************

#!/usr/bin/perl

%dictionary=("",,)

@answers=keys(%dictionary);
@questions=values(%dictionary);

open(DICT,"<korean.txt");
while($word = <DICT> ) {
chomp($word);

#  if ($word eq "=") {
#  }
#  print "$word\n";
}
close(DICT);






Report this thread to moderator Post Follow-up to this message
Old Post
Jeff Borders
10-25-04 08:56 PM


Re: Populating HASH from file
On Mon, 25 Oct 2004 08:46:29 -0400, Jeff Borders wrote:

> Newbie here,
>
> I need to dump a plain text file that has the format of:
>
> key = value (ie. question <equal sign> answer)
>
> into a hash.  I'm writing a simple learning tool to help me learn some
> basic korean terminology.
>
> Would someone be kind enough to point me in the right direction.
>
> -Jeff Borders
>
> korean.txt:
>
> One = Hana
> Two = Dool
> Three = Set
> etc.
>
> *********************************
>
> #!/usr/bin/perl

use warnings;
use strict;

> %dictionary=("",,)

my %dictionary;

> open(DICT,"<korean.txt");
> while($word = <DICT> ) {

while (my $word = <DICT> ) {

>   chomp($word);
my ($key, $value) = split /=/,$word;
$dictionary{$key} = $value;
> }
> close(DICT);

Do stuff with dictionary from here...
HTH
Chris.


Report this thread to moderator Post Follow-up to this message
Old Post
Chris Cole
10-25-04 08:56 PM


Re: Populating HASH from file
On Oct 25, Jeff Borders said:

>I need to dump a plain text file that has the format of:
>
>key = value (ie. question <equal sign> answer)
>
>into a hash.  I'm writing a simple learning tool to help me learn some
>basic korean terminology.
>
>Would someone be kind enough to point me in the right direction.

You want to start with an empty hash:

my %dict;

Then, when you're looping over each line of the file:

while (<FILE> ) {

you'll want to remove the ending newline,

chomp;

and split it on the = character (with whitespace on either side):

my ($term, $def) = split /\s*=\s*/;

Then you want to put that pair into the hash:

$dict{$term} = $def;
}

Using the hash, and opening the file, I leave up to you.

--
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %    -- Meister Eckhart


Report this thread to moderator Post Follow-up to this message
Old Post
Jeff 'japhy' Pinyan
10-25-04 08:56 PM


Re: Populating HASH from file
Jeff Borders wrote:
>
> I need to dump a plain text file that has the format of:
>
> key = value (ie. question <equal sign> answer)
>
> into a hash.  I'm writing a simple learning tool to help me learn some
> basic korean terminology.
>
> Would someone be kind enough to point me in the right direction.
>
> -Jeff Borders
>
> korean.txt:
>
> One = Hana
> Two = Dool
> Three = Set
> etc.
>
> *********************************
>
> #!/usr/bin/perl
>
> %dictionary=("",,)
>
> @answers=keys(%dictionary);
> @questions=values(%dictionary);
>
> open(DICT,"<korean.txt");
> while($word = <DICT> ) {
>   chomp($word);
>
> #  if ($word eq "=") {
> #  }
> #  print "$word\n";
> }
> close(DICT);

It sounds like you need something like this:

#!/usr/bin/perl
use warnings;
use strict;

open DICT, '<', 'korean.txt' or die "Cannot open 'korean.txt' $!";

my %dictionary = map /^([^=]+)=(.+)/, <DICT>;

close DICT;




John
--
use Perl;
program
fulfillment

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
10-25-04 08:56 PM


Re: Populating HASH from file
Hi,

Mine is not as elegant as J.Krahn's or Jeff's.
But it's an alternative.
I am just glad to contribute at least something
to the forum I have owed so much...

--
Regards,
Edward WIJAYA
SINGAPORE

-----------------------
use strict;
use warnings;
use Data::Dumper;

my %hash = ();

while (<DATA> ) {
s/\s//g;
/(\w+)=(\w+)/;
$hash{$1} = $2;
}

print Dumper \%hash;

__DATA__
One = Hana
Two = Dool
Three = Set
------------------------------
output:

$VAR1 = {
'Three' => 'Set',
'Two' => 'Dool',
'One' => 'Hana'
};


Report this thread to moderator Post Follow-up to this message
Old Post
Edward WIJAYA
10-25-04 08:56 PM


Re: Populating HASH from file
Edward WIJAYA wrote:
>
> Mine is not as elegant as J.Krahn's or Jeff's.
> But it's an alternative.
> I am just glad to contribute at least something
> to the forum I have owed so much...
>
> -----------------------
> use strict;
> use warnings;
> use Data::Dumper;
>
> my %hash = ();
>
> while (<DATA> ) {
> 	s/\s//g;

You may be removing too many whitespace characters if any of the fields has
embedded whitespace.

> 	/(\w+)=(\w+)/;
> 	$hash{$1} = $2;

You should only use the digit variables ($1, $2, etc.) if the match was
successful or their contents will be left over from the last successful matc
h.

> }


John
--
use Perl;
program
fulfillment

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
10-25-04 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:02 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.