Solved

String stuff

Posted on 2002-06-07
6
157 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Validating the data using Oracle DBD module. 5 67
pattern matching in perl 2 103
Perl 101 11 76
syslog unix file 20 73
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

772 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