Newbie: Has anyone used Tie::Hash::TwoWay
G'day everyone I'm confused. I'm trying to make use of Tie::Hash::TwoWay to give me access to a dictionary of word <=> misspelling. Has anyone got any idea how I would use TwoWay for this? Nothing I do seems to work. my $secondary = $dict->{0}; while ( ($k, $v) = ( each %$secondary ) ) { print $k . "\t" . $v . "\n"; } my $primary = $dict->{1}; while ( ($k, $v) = ( each %$primary ) ) { print $k . "\t" . $v . "\n"; } Kind regards, Bruce. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Newbie: Has anyone used Tie::Hash::TwoWay
axtens wrote: G'day everyone I'm confused. I'm trying to make use of Tie::Hash::TwoWay to give me access to a dictionary of word <=> misspelling. Has anyone got any idea how I would use TwoWay for this? Nothing I do seems to work. my $secondary = $dict->{0}; while ( ($k, $v) = ( each %$secondary ) ) { print $k . "\t" . $v . "\n"; } my $primary = $dict->{1}; while ( ($k, $v) = ( each %$primary ) ) { print $k . "\t" . $v . "\n"; } Your code looks ok. What is it doing wrong? Or what isn't it doing that it should be? Can you give us an example of your data please? I can't see how you're expecting to use this module for mispellings. If you use a properly-spelled word as the hash key it will return a list of misspellings. If you use a misspelled word as a key it will offer you a list of corresponding properly-spelled ones. Is that what you want? Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Newbie: Has anyone used Tie::Hash::TwoWay
axtens wrote: G'day everyone I'm confused. I'm trying to make use of Tie::Hash::TwoWay to give me access to a dictionary of word <=> misspelling. Has anyone got any idea how I would use TwoWay for this? Nothing I do seems to work. my $secondary = $dict->{0}; while ( ($k, $v) = ( each %$secondary ) ) { print $k . "\t" . $v . "\n"; } my $primary = $dict->{1}; while ( ($k, $v) = ( each %$primary ) ) { print $k . "\t" . $v . "\n"; } After sending my previous post it occurs to me that you may not realise that the values of the tied hash are themselves hash references. Try a loop like this: while (my ($k, $v) = each %$secondary) { printf "%s => (%s)\n", $k, join ' ', keys %$v; } is that closer to what you expect? Oh, and please use strict; use warnings; at the start of your code. Thanks. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Newbie: Has anyone used Tie::Hash::TwoWay
On Feb 1, 10:00 am, [EMAIL PROTECTED] (Rob Dixon) wrote: > axtenswrote: > > G'day everyone Thanks for that. I did, however, give up on using TwoWay and used an idea a colleague had given me, as below: sub Misspellings_Setup { @wordsList = split /\n/, <<'__WORDLIST__' abandonned abandoned aberation aberration abilities abilties . . . __WORDLIST__ ; foreach (@wordsList) { ($left, $right) = split /\t/, $_; @leftList = split /,\s* /, $left; @rightList = split /,\s*/, $right; foreach $l (@leftList) { foreach $r (@rightList) { if ( exists $misDict{"!-$l"} ) { $misDict{"!-$l"} .= $r . "^"; } else { $misDict{"!-$l"} = $r . "^"; } if ( exists $misDict{"?-$r"} ) { $misDict{"?-$r"} .= $l . "^"; } else { $misDict{"?-$r"} = $l . "^"; } } } } } misDict is an "our" declared elswhere. Getting something out of the hash is as below. sub Misspellings_Suggest { #receives string #returns string my $res; my $string = shift; if ( ! %misDict ) { Misspellings_Setup(); } if ( exists( $misDict{"!-$string"} ) ) { $res = $misDict{"!-$string"}; } else { $res = $string; } $res =~ s/\^/FS/ge; return $res; } Kind regards, Bruce. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Newbie: Has anyone used Tie::Hash::TwoWay
axtens wrote: On Feb 1, 10:00 am, [EMAIL PROTECTED] (Rob Dixon) wrote: axtenswrote: G'day everyone Thanks for that. I did, however, give up on using TwoWay and used an idea a colleague had given me, as below: sub Misspellings_Setup { @wordsList = split /\n/, <<'__WORDLIST__' abandonned abandoned aberation aberration abilities abilties . . . __WORDLIST__ ; Why not just create @wordsList as an AoA so you don't have to do all that splitting: my @wordsList = ( [ 'abandonned', 'abandoned' ], [ 'aberation', 'aberration' ], [ 'abilities', 'abilties' ], ... ); foreach (@wordsList) { ($left, $right) = split /\t/, $_; @leftList = split /,\s* /, $left; @rightList = split /,\s*/, $right; I didn't see any commas in your data so what is this supposed to be doing? foreach $l (@leftList) { foreach $r (@rightList) { if ( exists $misDict{"!-$l"} ) { $misDict{"!-$l"} .= $r . "^"; } else { $misDict{"!-$l"} = $r . "^"; } if ( exists $misDict{"?-$r"} ) { $misDict{"?-$r"} .= $l . "^"; } else { $misDict{"?-$r"} = $l . "^"; } You don't have to test for the key existence as perl will autovivify the values. } } } } misDict is an "our" declared elswhere. Getting something out of the hash is as below. sub Misspellings_Suggest { #receives string #returns string my $res; my $string = shift; if ( ! %misDict ) { Misspellings_Setup(); } if ( exists( $misDict{"!-$string"} ) ) { $res = $misDict{"!-$string"}; } else { $res = $string; } $res =~ s/\^/FS/ge; The /e option evaluates the "FS" string as perl code but it is not perl code so why use the /e option? return $res; } John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order.-- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Newbie: Has anyone used Tie::Hash::TwoWay
On Feb 4, 12:44 pm, [EMAIL PROTECTED] (John W. Krahn) wrote: > axtenswrote: > > On Feb 1, 10:00 am, [EMAIL PROTECTED] (Rob Dixon) wrote: > >> axtenswrote: > >>> G'day everyone > John As it happens, there are commas in the data, and they can be on either side of the the tab. What's more, I wanted to be able to handle the possibility of comma and comma+space. I certainly didn't think of the [] notation, which is a good idea. (Am showing my beginner status quite well here.) As for the /e with the FS, the FS is actually a constant for chr(28) which we're using internally as ... can you believe it ... a field separator. (How many years has it been in the low end of the ASCII sequence, never being used as what it was originally designed for?) Anyway, thanks muchly for your input. Kind regards, Bruce. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Newbie: Has anyone used Tie::Hash::TwoWay
John W. Krahn wrote: axtens wrote: On Feb 4, 12:44 pm, [EMAIL PROTECTED] (John W. Krahn) wrote: axtens wrote: $res =~ s/\^/FS/ge; The /e option evaluates the "FS" string as perl code but it is not perl code so why use the /e option? As for the /e with the FS, the FS is actually a constant for chr(28) which we're using internally as ... can you believe it ... a field separator. According to my ascii man page: man ascii [ SNIP ] 034 281CFS (file separator) You probably want RS: 036 301ERS (record separator) (How many years has it been in the low end of the ASCII sequence, never being used as what it was originally designed for?) If FS is actually code then you should show it as code: $res =~ s/\^/ FS() /ge; Or better would be to just use a scalar: my $FS = "\x1C"; ... $res =~ s/\^/$FS/g; But then why not just use FS() in the first place: foreach $l ( @leftList ) { foreach $r ( @rightList ) { $misDict{ "!-$l" } .= $r . FS(); $misDict{ "?-$r" } .= $l . FS(); } } Or you could just use Perl's $; variable which is already set to that value. John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order.-- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Newbie: Has anyone used Tie::Hash::TwoWay
axtens wrote: On Feb 4, 12:44 pm, [EMAIL PROTECTED] (John W. Krahn) wrote: axtens wrote: $res =~ s/\^/FS/ge; The /e option evaluates the "FS" string as perl code but it is not perl code so why use the /e option? As for the /e with the FS, the FS is actually a constant for chr(28) which we're using internally as ... can you believe it ... a field separator. According to my ascii man page: man ascii [ SNIP ] 034 281CFS (file separator) You probably want RS: 036 301ERS (record separator) (How many years has it been in the low end of the ASCII sequence, never being used as what it was originally designed for?) If FS is actually code then you should show it as code: $res =~ s/\^/ FS() /ge; Or better would be to just use a scalar: my $FS = "\x1C"; ... $res =~ s/\^/$FS/g; But then why not just use FS() in the first place: foreach $l ( @leftList ) { foreach $r ( @rightList ) { $misDict{ "!-$l" } .= $r . FS(); $misDict{ "?-$r" } .= $l . FS(); } } John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order.-- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Newbie: Has anyone used Tie::Hash::TwoWay
On Feb 4, 9:11 pm, [EMAIL PROTECTED] (John W. Krahn) wrote: > John W. Krahn wrote: > >axtenswrote: > >> On Feb 4, 12:44 pm, [EMAIL PROTECTED] (John W. Krahn) wrote: John, You're blowing me away with all this kindness. Thanks. Bruce. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/