Skip to content

Hello, I made CurrencyIn.pm module for you. #64

Merged
merged 6 commits into from Apr 21, 2012

5 participants

@Alchymista

When user type:
currency in {country}
then result is found and returned from hash table (now it is included inside module)

I know it requires some upgrades but this is my first day with Perl =)
so anyone feel free to do updates in it...

Module is based on this request:
https://duckduckhack.uservoice.com/forums/5168-plugins/suggestions/2678913-currency-in-x-tells-you-the-currency-in-x-countr

Pavel

@rpicard
rpicard commented Apr 12, 2012

Cool Goodie! You should put the hash in the Perl file though. See here for an example of that.

@Alchymista

Hello, thanks for comment.

No problem, I can put that hash inside Perl file. I am just unsure if it should be embed or loaded from external file.
Because I was thinking that those information (parsed from wikipedia) may be updated regularly (e.g. once a year =) and it is easier to do when in external file (parser is already coded in Python)
But I am open for suggestion. If it is really no problem then I can put it in the Perl file (it is easier solution anyway =).

Pavel

@rpicard
rpicard commented Apr 12, 2012

I'd just put it in the Goodie but someone else might disagree.

@Alchymista

OK, seem you thing it is better to put that hash into Perl file... so I am starting to work on it =)

@Alchymista

Hash table with countries is now included in Perl file and it works fine on my computer so hopefully someone will accept my pull request :)

@rpicard
rpicard commented Apr 12, 2012

Cool. Just a couple of other suggestions.

You might want to move the hash outside of the subroutine so it isn't re-generated every time it's called.

Also, what if the user types "currency of" instead of "currency in"? You might want to use =~ with a regex to find the country they want instead of using substr(). That way, you could just search the whole thing for a pattern like: /(in\s*.*|of\s*.*|for\s*.*)/ (probably not the right pattern to use but you get the idea) and not care about what came before it, e.g. what is the currency used in America or what currency will I need for Russia? (note: the latter case shows that you might also consider stripping the match of punctuation before checking the hash).

If you did decide to do that you'd probably want to change the trigger to any.

@Alchymista

Great, thank you for suggestions =)

I get some sleep first and will take a look on it, tomorrow.

@yegg
DuckDuckGo member
yegg commented Apr 15, 2012

I think you are right -- it would be best to read in the output file so it could be updated easily at a later point. Also, you can add the python generation file right in the same share directory so it is easy to regenerate.

You should be able to read in the hash like this:

my %currency = share('currency.txt')->slurp

That would read one line in as a key, and the next as a value. Check out how the passphrase goodie for an example (though it uses an array).

@Alchymista

So far I have two issues with "my %currency = share('currency.txt')->slurp" way of reading hash file:

1) it reads values with the end of the line for keys and values. So when I search for '$currency{"slovakia"}' then no results, because I must search for "slovakia\n" with end of the line included...

2) I have problem to pass value as array when share function reads '["Euro (EUR)", "Dolar (USD)"]' then it keeps that line as string not an array =(

It will be much appreciated if anyone can help =)

Pavel

btw: 'currency.txt' at the moment looks like this:
abkhazia
["Russian ruble (RUB)"]
afghanistan
["Afghan afghani (AFN)"]
zimbabwe
["Botswana pula (BWP)","British pound (GBP)","Euro (EUR)","South African rand (ZAR)","United States dollar (USD)"]
...

@rpicard
rpicard commented Apr 17, 2012

@Alchymista In response to your second point you could make it a string where the values are separated by something like a semicolon so it looks like Zimbabwe => "Botswana pula (BWP):Euro (EUR):Dollar (USD)" and use my @arr = split(/:/, $currency{"simbabwe"}) to split it into an array.

@Getty
Getty commented Apr 17, 2012

You should consider switch this to YAML https://metacpan.org/module/YAML

@Alchymista

Thank you @rpicard
Not sure how I could forgot about so simple solution (probably because I wanted to store it like array so much...)

@Getty ... thanks for introducing YAML module, but I am quite new to Perl (all together I spent like three days with it) so for the moment I stick with my current solution (it is working =) ...
But if you think that with YAML it will be really, really cool then feel free to take all my code or redo whole CurrencyIn module if you..
(for me this is sort of exercise because I wanted to try what it is about "to be involved in opensource" =)

@Getty
Getty commented Apr 17, 2012

you can definitly do that yourself :) its a good mission/training :) thanks for the code! :)

@rpicard
rpicard commented Apr 18, 2012

@Alchymista I was just testing out your Goodie and have a few things for you to look into.

  • There is a new line in the response. I think chomp() might work but there is some stuff in that page about using it with slurp so you might need to use $var =~ s/\n//g; or something similar to strip the new line.
  • Maybe make the response more like a sentence, i.e. instead of saying Currency in Andorra is Euro (EUR) return The currency in Andorra is the Euro (EUR)..

It's coming along really well!

@Alchymista

Thank you for help and support :)

@rpicard
rpicard commented Apr 19, 2012

No problem! Thanks for putting so much work into it.

@majuscule majuscule merged commit 4d01ce2 into duckduckgo:master Apr 21, 2012
@majuscule

Merged :-) Thanks again for contributing to DuckDuckGo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.