Solved

String stuff

Posted on 2002-06-07
6
181 Views
Last Modified: 2010-03-05
Hi,

I'm very new to Perl so, how would you have something to break this up into like three parts:

<variable name> = <something>

Basically, send each part into a variable where you can have furthur analysis such as whether there was an "=" sign in the input string?

Like is it something like:

$in = <STDIN>;
print "Decl: $in";
while(defined($in))
{
    $in = <STDIN>;
    if(defined($in))
    {
        print "Decl: $in";

        if(!($in contains "="))
        {
            print "Result:\tincorrect - missing equals sign";
        }
    }
}
print "\n\n";

Also, I need it to keep looping into the keyboard input (from input redirection) keeps going until end of input.  I have to do it that ghetto way where I take an input in first.  Can anyone think of a better way?

Thanks!
0
Comment
Question by:zoomeroo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 7

Accepted Solution

by:
yoren earned 100 total points
ID: 7063227
Try this:


print "Enter a variable assignment: ";
while (<STDIN>) {
    chomp; # Strip off the line feed

    if (/([^=]+?)\s*=\s*(.*)/) {
        $name = $1;
        $value = $2;
        print "Name: $name, Value: $value\n";
    }
    else {
        print "Result:\tincorrect - missing equals sign\n";
    }

    print "\nEnter another variable assignment: ";
}
0
 

Author Comment

by:zoomeroo
ID: 7063239
Hi,

Wow.  Do you think you could explain "if (/([^=]+?)\s*=\s*(.*)/) {" a little?

Like how it works?
0
 
LVL 7

Expert Comment

by:yoren
ID: 7063257
Yeah it looks crazy, doesn't it. It's a regular expression. There's a whole section of the Perl Frequently Asked Questions devoted to regular expressions, but here's the translation in English:

/ -- begin a regular expression match
(...) -- match the pattern in the ()'s and put the value in the variable $1 for the first set of ()'s, $2 for the second, etc.

[^=]+ -- match 1 or more characters, anything but an equal sign

? -- don't be greedy, meaning let the next pattern take priority. We use this so you don't include any whitespace before the "=" in the variable name.

\s* -- match zero or more whitespace characters

= -- take a guess

\s* -- more whitespace

(.*) -- match anything else and put it in $2.

/ -- end of regular expression

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:zoomeroo
ID: 7063262
Ah, nm I understand
0
 

Author Comment

by:zoomeroo
ID: 7063264
How does it know that it goes in $1 and $2?
0
 
LVL 7

Expert Comment

by:yoren
ID: 7063302
$1 holds the value of the first set of parentheses. $2 holds the second. If there was a third set, it would put it in $3, and so on.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question