zoomeroo
asked on
String stuff
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!
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
/ -- 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
ASKER
Ah, nm I understand
ASKER
How does it know that it goes in $1 and $2?
$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.
ASKER
Wow. Do you think you could explain "if (/([^=]+?)\s*=\s*(.*)/) {" a little?
Like how it works?