Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Beginner here - basic help

by xr6turbo (Initiate)
on Mar 26, 2015 at 13:09 UTC ( [id://1121388]=perlquestion: print w/replies, xml ) Need Help??

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

Hey Monks, Just starting off in perl (and programming in general) and need to do an exercise but a bit confused on the process.

Trying to create a script that will ask the user to enter a number between 1 and 5 and for it to print a colour represented e.g. if they enter 1, Blue comes up or 3, red gets printed. I've done a bit of research and I know I need to create an array (@) but not too clear on how to do so.

After that I need to assign an emotion to the colour e.g. blue = calm, red = angry. I'd also like to know how to incorporate modulus into this so on even/odd number inputs I can print it out (think ill need an if/else statement for this?)

At the end I'd like it to look like this:

Please enter a number between 1 and 5:

1

Blue. Even. Calm

So far I've got the code below and have no idea how to finish or even progress, any ideas?

print "Please enter a number between 1 and 5 (inclusive) below:"; my $number = <STDIN>; chomp($number); my @colours = ("red", "green", "blue", "purple", "black"); my %table = ( 1 => "angry" 2 => "sick" 3 => "calm" 4 => "worried" 5 => "sad" );

Replies are listed 'Best First'.
Re: Beginner here - basic help
by choroba (Cardinal) on Mar 26, 2015 at 13:28 UTC
    You don't need a hash (%table), you can use an array in the same way as for @colours. Just remember that array indices start at 0, not 1.

    You can also verify that the input is a number in the given range and ask again if it isn't.

    The modulo operator in Perl is %:

    #! /usr/bin/perl use warnings; use strict; print "Please enter a number between 1 and 5 (inclusive): "; my $number = 0; while ($number !~ /^[1-5]$/) { $number = <STDIN>; chomp $number; } my @colours = qw( _ Red Green Blue Purple Black ); my @emotions = qw( _ Angry Sick Calm Worried Sad ); my $even_odd; if (0 == $number % 2) { $even_odd = 'Even'; } else { $even_odd = 'Odd'; } print join '. ', $colours[$number], $even_odd, $emotions[$number], "\n +";
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Beginner here - basic help
by toolic (Bishop) on Mar 26, 2015 at 13:29 UTC
    A hash of hashes structure will work:
    use warnings; use strict; my %table = ( 1 => { emotion => 'angry' , color => 'red' }, 2 => { emotion => 'sick' , color => 'green' }, 3 => { emotion => 'calm' , color => 'blue' }, 4 => { emotion => 'worried' , color => 'purple' }, 5 => { emotion => 'sad' , color => 'black' }, ); for my $number (2, 5) { my $even = ($number % 2) ? 'odd' : 'even'; print "$number $table{$number}{emotion} $table{$number}{color} $ev +en\n"; } __END__ 2 sick green even 5 sad black odd

      Or an array of arrays:

      my @data = ( [ qw( angry red ) ], [ qw( sick green ) ], [ qw( calm blue ) ], [ qw( worried purple ) ], [ qw( sad black ) ], ); for (1, 4) { print join( ' ', $_, @{$data[$_]}, ($_%2) ? 'odd' : 'even' ), $/; } __END__ 1 sick green odd 4 sad black even

      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)
      
Re: Beginner here - basic help
by BillKSmith (Monsignor) on Mar 26, 2015 at 16:56 UTC
    Combining chobera and jeffa's solutions and correcting for array index starting at zero.
    use strict; use warnings; my @array = ( ["error" ], [qw( Red Angry )], [qw( Green Sick )], [qw( Blue Calm )], [qw( Purple Worried )], [qw( Black Sad )], ); my $response=0; while ($response !~ /^[1-5]$/) { print "Enter an integer ( 1 through 5 inclusive): "; $response = <>; chomp $response; } print $array[$response][0], ' ', ($response % 2) ? "Odd" : "Even", ' ', $array[$response][1], "\n";
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1121388]
Approved by Athanasius
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found