Solved

String stuff

Posted on 2002-06-07
6
135 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
  • 3
  • 3
6 Comments
 
LVL 7

Accepted Solution

by:
yoren earned 100 total points
Comment Utility
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
Comment Utility
Hi,

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

Like how it works?
0
 
LVL 7

Expert Comment

by:yoren
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:zoomeroo
Comment Utility
Ah, nm I understand
0
 

Author Comment

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

Expert Comment

by:yoren
Comment Utility
$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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now