Write a program in Perl using hash












0















I want to print 2 days back after getting input from user.



Example:



enter a day :
Input : Wednesday

Output : monday


I tried it using hashing with array but can't find result.



%hash=('mon',1,'tue',2,'wed',3);
@arr=keys %hash;









share|improve this question





























    0















    I want to print 2 days back after getting input from user.



    Example:



    enter a day :
    Input : Wednesday

    Output : monday


    I tried it using hashing with array but can't find result.



    %hash=('mon',1,'tue',2,'wed',3);
    @arr=keys %hash;









    share|improve this question



























      0












      0








      0








      I want to print 2 days back after getting input from user.



      Example:



      enter a day :
      Input : Wednesday

      Output : monday


      I tried it using hashing with array but can't find result.



      %hash=('mon',1,'tue',2,'wed',3);
      @arr=keys %hash;









      share|improve this question
















      I want to print 2 days back after getting input from user.



      Example:



      enter a day :
      Input : Wednesday

      Output : monday


      I tried it using hashing with array but can't find result.



      %hash=('mon',1,'tue',2,'wed',3);
      @arr=keys %hash;






      perl






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 3:31









      jwodder

      33.1k35283




      33.1k35283










      asked Nov 21 '18 at 3:24









      Vishal GuptaVishal Gupta

      245




      245
























          2 Answers
          2






          active

          oldest

          votes


















          4














          Your attempt is backwards. The strings by which you want to search should be the keys of the hash.



          my @days = qw( mon tue wed );
          my %index_of_day = map { $days[$_] => $_, $_ => $_ } 0..$#days;

          defined( my $input = <> )
          or die("Premature EOFn");

          chomp($input);

          my $old_index_of_day = $index_of_day{$input}
          or die("Unrecognized day $inputn");

          my $new_index_of_day = $old_index_of_day - 2;
          $new_index_of_day += @days while $new_index_of_day < 0;

          my $output = $days[$new_index_of_day];





          share|improve this answer


























          • My input is 1 or 2 or 0 or mon however error message popups unrecognized day... What is actual input?

            – ssr1012
            Nov 21 '18 at 4:30











          • Either check if you have a number, or add the number to the map. I did the latter.

            – ikegami
            Nov 21 '18 at 4:38











          • thanks it works fine

            – ssr1012
            Nov 21 '18 at 4:56



















          -1














          Using interactive perl-one liner. Note that it is case sensitive and doesn't print anything if it is not matching the keys of the %hash.



          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: fff


          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: Mon
          Sat

          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: Tue
          Sun

          $





          share|improve this answer


























          • this is working for (Mon, Tue,Wed) only but not for other days.

            – Vishal Gupta
            Nov 21 '18 at 4:29











          • it works.. please check case sensitivity as stored in the hash..and any extra spaces

            – stack0114106
            Nov 21 '18 at 4:32











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404829%2fwrite-a-program-in-perl-using-hash%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          Your attempt is backwards. The strings by which you want to search should be the keys of the hash.



          my @days = qw( mon tue wed );
          my %index_of_day = map { $days[$_] => $_, $_ => $_ } 0..$#days;

          defined( my $input = <> )
          or die("Premature EOFn");

          chomp($input);

          my $old_index_of_day = $index_of_day{$input}
          or die("Unrecognized day $inputn");

          my $new_index_of_day = $old_index_of_day - 2;
          $new_index_of_day += @days while $new_index_of_day < 0;

          my $output = $days[$new_index_of_day];





          share|improve this answer


























          • My input is 1 or 2 or 0 or mon however error message popups unrecognized day... What is actual input?

            – ssr1012
            Nov 21 '18 at 4:30











          • Either check if you have a number, or add the number to the map. I did the latter.

            – ikegami
            Nov 21 '18 at 4:38











          • thanks it works fine

            – ssr1012
            Nov 21 '18 at 4:56
















          4














          Your attempt is backwards. The strings by which you want to search should be the keys of the hash.



          my @days = qw( mon tue wed );
          my %index_of_day = map { $days[$_] => $_, $_ => $_ } 0..$#days;

          defined( my $input = <> )
          or die("Premature EOFn");

          chomp($input);

          my $old_index_of_day = $index_of_day{$input}
          or die("Unrecognized day $inputn");

          my $new_index_of_day = $old_index_of_day - 2;
          $new_index_of_day += @days while $new_index_of_day < 0;

          my $output = $days[$new_index_of_day];





          share|improve this answer


























          • My input is 1 or 2 or 0 or mon however error message popups unrecognized day... What is actual input?

            – ssr1012
            Nov 21 '18 at 4:30











          • Either check if you have a number, or add the number to the map. I did the latter.

            – ikegami
            Nov 21 '18 at 4:38











          • thanks it works fine

            – ssr1012
            Nov 21 '18 at 4:56














          4












          4








          4







          Your attempt is backwards. The strings by which you want to search should be the keys of the hash.



          my @days = qw( mon tue wed );
          my %index_of_day = map { $days[$_] => $_, $_ => $_ } 0..$#days;

          defined( my $input = <> )
          or die("Premature EOFn");

          chomp($input);

          my $old_index_of_day = $index_of_day{$input}
          or die("Unrecognized day $inputn");

          my $new_index_of_day = $old_index_of_day - 2;
          $new_index_of_day += @days while $new_index_of_day < 0;

          my $output = $days[$new_index_of_day];





          share|improve this answer















          Your attempt is backwards. The strings by which you want to search should be the keys of the hash.



          my @days = qw( mon tue wed );
          my %index_of_day = map { $days[$_] => $_, $_ => $_ } 0..$#days;

          defined( my $input = <> )
          or die("Premature EOFn");

          chomp($input);

          my $old_index_of_day = $index_of_day{$input}
          or die("Unrecognized day $inputn");

          my $new_index_of_day = $old_index_of_day - 2;
          $new_index_of_day += @days while $new_index_of_day < 0;

          my $output = $days[$new_index_of_day];






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 5:42

























          answered Nov 21 '18 at 3:55









          ikegamiikegami

          262k11176396




          262k11176396













          • My input is 1 or 2 or 0 or mon however error message popups unrecognized day... What is actual input?

            – ssr1012
            Nov 21 '18 at 4:30











          • Either check if you have a number, or add the number to the map. I did the latter.

            – ikegami
            Nov 21 '18 at 4:38











          • thanks it works fine

            – ssr1012
            Nov 21 '18 at 4:56



















          • My input is 1 or 2 or 0 or mon however error message popups unrecognized day... What is actual input?

            – ssr1012
            Nov 21 '18 at 4:30











          • Either check if you have a number, or add the number to the map. I did the latter.

            – ikegami
            Nov 21 '18 at 4:38











          • thanks it works fine

            – ssr1012
            Nov 21 '18 at 4:56

















          My input is 1 or 2 or 0 or mon however error message popups unrecognized day... What is actual input?

          – ssr1012
          Nov 21 '18 at 4:30





          My input is 1 or 2 or 0 or mon however error message popups unrecognized day... What is actual input?

          – ssr1012
          Nov 21 '18 at 4:30













          Either check if you have a number, or add the number to the map. I did the latter.

          – ikegami
          Nov 21 '18 at 4:38





          Either check if you have a number, or add the number to the map. I did the latter.

          – ikegami
          Nov 21 '18 at 4:38













          thanks it works fine

          – ssr1012
          Nov 21 '18 at 4:56





          thanks it works fine

          – ssr1012
          Nov 21 '18 at 4:56













          -1














          Using interactive perl-one liner. Note that it is case sensitive and doesn't print anything if it is not matching the keys of the %hash.



          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: fff


          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: Mon
          Sat

          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: Tue
          Sun

          $





          share|improve this answer


























          • this is working for (Mon, Tue,Wed) only but not for other days.

            – Vishal Gupta
            Nov 21 '18 at 4:29











          • it works.. please check case sensitivity as stored in the hash..and any extra spaces

            – stack0114106
            Nov 21 '18 at 4:32
















          -1














          Using interactive perl-one liner. Note that it is case sensitive and doesn't print anything if it is not matching the keys of the %hash.



          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: fff


          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: Mon
          Sat

          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: Tue
          Sun

          $





          share|improve this answer


























          • this is working for (Mon, Tue,Wed) only but not for other days.

            – Vishal Gupta
            Nov 21 '18 at 4:29











          • it works.. please check case sensitivity as stored in the hash..and any extra spaces

            – stack0114106
            Nov 21 '18 at 4:32














          -1












          -1








          -1







          Using interactive perl-one liner. Note that it is case sensitive and doesn't print anything if it is not matching the keys of the %hash.



          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: fff


          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: Mon
          Sat

          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: Tue
          Sun

          $





          share|improve this answer















          Using interactive perl-one liner. Note that it is case sensitive and doesn't print anything if it is not matching the keys of the %hash.



          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: fff


          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: Mon
          Sat

          $ perl -ne 'BEGIN{printf("%s","Enter the input: "); my $inp=<STDIN>; chomp($inp); %hash=('Mon',1,'Tue',2,'Wed',3,'Thu',4,'Fri',5,'Sat',6,'Sun',7); $x=$hash{$inp}-2; $x
          +=7 if $x<1; exit if not exists $hash{$inp}; foreach my $y (keys %hash) { print "$y" if $hash{$y}==$x } ; exit } '
          Enter the input: Tue
          Sun

          $






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 3:58

























          answered Nov 21 '18 at 3:53









          stack0114106stack0114106

          2,8131417




          2,8131417













          • this is working for (Mon, Tue,Wed) only but not for other days.

            – Vishal Gupta
            Nov 21 '18 at 4:29











          • it works.. please check case sensitivity as stored in the hash..and any extra spaces

            – stack0114106
            Nov 21 '18 at 4:32



















          • this is working for (Mon, Tue,Wed) only but not for other days.

            – Vishal Gupta
            Nov 21 '18 at 4:29











          • it works.. please check case sensitivity as stored in the hash..and any extra spaces

            – stack0114106
            Nov 21 '18 at 4:32

















          this is working for (Mon, Tue,Wed) only but not for other days.

          – Vishal Gupta
          Nov 21 '18 at 4:29





          this is working for (Mon, Tue,Wed) only but not for other days.

          – Vishal Gupta
          Nov 21 '18 at 4:29













          it works.. please check case sensitivity as stored in the hash..and any extra spaces

          – stack0114106
          Nov 21 '18 at 4:32





          it works.. please check case sensitivity as stored in the hash..and any extra spaces

          – stack0114106
          Nov 21 '18 at 4:32


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404829%2fwrite-a-program-in-perl-using-hash%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?