Solved

String stuff

Posted on 2002-06-07
6
174 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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.…
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…

752 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