Solved

String stuff

Posted on 2002-06-07
6
146 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

12 Experts available now in Live!

Get 1:1 Help Now