Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:04:54+00:00 2026-06-02T10:04:54+00:00

I realize the question name doesn’t really tell much but I didn’t really know

  • 0

I realize the question name doesn’t really tell much but I didn’t really know how to explain it shortly, so here’s the long version.

First, here’s my current code:

#! /usr/bin/perl

use strict;
use warnings;

my $input;
while (<>) {
        $input .= $_;
}
$input =~ s/ |\n//g;

print "\n";

What I want to do is make a calculator, e.g. when a user does echo "8 * 5 + 21-15" | calculate it will calculate it correctly. So here’s my thought progress. First I take the string as a whole and strip it of all whitespace characters. Then I wanted to index() it for the occurence of *, +, / or -. Then I wanted to add all the characters before any of those operators to a string and then (int) the string and then do the same to the part after the operator and then do the operation between them. But I don’t actually have much of a clue on how to do this. Also, I’m very new to Perl (3 days experience) so please go slowly on me if possible.

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:04:56+00:00Added an answer on June 2, 2026 at 10:04 am

    If you can accept that your calculator won’t be able to handle parenthesis, use a regular expression to parse the string for you:

    #!/usr/bin/env perl
    use strict;
    use warnings;
    
    my @tokens = <STDIN> =~ /(\d+|\+|-|\*|\/)/g;
    print "$_\n" for @tokens;
    

    this will provide you with an array of tokens that you can work on, so

    echo "8 * 5 + 21-15" | script.pl
    

    will print

    8
    *
    5
    +
    21
    -
    15
    

    Now it’s up to you to write some code that does the right calculations on the tokens. It isn’t too hard if you don’t try parsing parens, but if you do, you’ll need to write a recursive parser, which is much harder.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize this question is pretty basic, but I'm really stuck. I have a
I realize this is kind of a long question, but I saw no other
I realize this question has probably been asked numerous times, but I have not
I realize that this question has been asked 100times but none that I have
While I realize that this question has been asked once or twice ago but
I realize this is more of a hardware question, but this is also very
I realize this is probably a hopelessly newbie question, but what is the difference
I realize this is a basic question but I have searched online, been to
I know this is a basic question, but I can't find other StackOverflow posts
I've been using SO for a long time but finally had a question I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.