Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Write an user answer inside an array

by Eth443 (Initiate)
on May 04, 2015 at 23:22 UTC ( [id://1125643]=perlquestion: print w/replies, xml ) Need Help??

Eth443 has asked for the wisdom of the Perl Monks concerning the following question:

Hi...again! The idea => The array is like the last one

@bad_answer=( "Bad", "Bad to be honest", "Not good", "Not good enough" );

Now I would like to know if it's possible when the user put with <STDIN> something prove if it is in the array (i know how to do this) and if it isn't save it for ever as part of the initial array.Prob. my explication is quite short, if there's any dude, ask

my $useranswer=<STDIN>; if ($useranswer~~@bad_answer){print "ok";} #shotversion else{SAVE THE DATA INSIDE THE ARRAY} @bad_answer=( "Bad", "Bad to be honest", "Not good", "Not good enough", "USER INPUT"#I'll have 3 different arrays );

Replies are listed 'Best First'.
Re: Write an user answer inside an array
by AnomalousMonk (Archbishop) on May 04, 2015 at 23:31 UTC

    I haven't been following along in the CB so this may not be apropos, but maybe something like:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @answers = ('Bad', 'Good', 'Not good enough'); ;; my %known = map { canonical($_) => 1 } @answers; ;; UI: { chomp(my $user_answer = <STDIN>); last UI if canonical($user_answer) eq canonical('q'); ;; if ($known{canonical($user_answer)}) { print qq{'$user_answer' known}; } else { print qq{'$user_answer' is UNKNOWN}; $known{ canonical($user_answer) } = 1; } ;; redo UI; } ;; dd \%known; ;; sub canonical { return lc $_[0]; } " foo 'foo' is UNKNOWN foo 'foo' known Junk 'Junk' is UNKNOWN junk 'junk' known total crap 'total crap' is UNKNOWN Total Crap 'Total Crap' known q { "bad" => 1, "foo" => 1, "good" => 1, "junk" => 1, "not good enough" => 1, "total crap" => 1, }


    Give a man a fish:  <%-(-(-(-<

Re: Write an user answer inside an array
by jeffa (Bishop) on May 04, 2015 at 23:32 UTC

    As discussed in the CB, you'll need to store the contents outside of RAM, such as a file. The following code will bootstrap the file if necessary and query the user for feelings and how they feel. It uses FreezeThaw to store persistent data.

    #!/usr/bin/env perl use strict; use warnings; use File::Slurp; use FreezeThaw qw( freeze thaw ); my $DB_FILE = 'questions.frozen'; unless (-e $DB_FILE) { open FH, '>', $DB_FILE; close FH; } my $data; eval { ($data) = thaw( read_file( $DB_FILE ) ) }; $data = {} if $@; print "How do you feel?"; chomp( my $feeling = <> ); if ($data->{$feeling}) { print "$feeling is a $data->{$feeling} feeling\n"; } else { print "I don't know about $feeling, is it good or bad?"; chomp( my $answer = <> ); $data->{$feeling} = $answer; } open FH, '>', $DB_FILE; print FH freeze $data; close FH;

    I did not include error checking and i opted to use a hash instead of an array. Hope this helps!

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      Yey, thanks I'm quite noobie as you know, can you comment a little bit? xD I hate just copy and paste the code if I cannot understand it..

        Can you start the commenting? Comment as much as possible about every line of the program

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1125643]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-23 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found