Code Comments
Programming Forum and web based access to our favorite programming groups.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);
Post Follow-up to this messageOn 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.
Post Follow-up to this messageOn 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
Post Follow-up to this messageJeff 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
Post Follow-up to this messageHi,
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'
};
Post Follow-up to this messageEdward 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.