Very beginner question
I feel strange putting such a simple question on this list, but then again, that is why I did not join the experts list. Any how, if I have a file called try.txt with the following line: (name = john) if I am trying to just extract john for the value $b, why would the following script not work. I thought it would take bothIt returns the full (name=john) #!user/local/bin/perl -w open TRY , try.txt; while (TRY) { (my $b=$_) =~ s/^(\() (\w+)/$2/; print $b; } Thank you for humiliating me with this simple question. Olivier The one who still has a long way to go with Perl
Re: Very beginner question
On May 9, [EMAIL PROTECTED] said: (name = john) if I am trying to just extract john for the value $b, why would the following script not work. I thought it would take bothIt returns the full (name=john) Let's run your regex through the regex explainer: (my $b=$_) =~ s/^(\() (\w+)/$2/; NODE EXPLANATION -- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): -- ^the beginning of the string -- (group and capture to \1: -- \( '(' -- )end of \1 -- ' ' -- (group and capture to \2: -- \w+ word characters (a-z, A-Z, 0-9, _) (1 or more times (matching the most amount possible)) -- )end of \2 -- )end of grouping -- You can see that it's matching a '(', and then a ' '. You don't have a space after the ( though. :( Perhaps you want to use: while (TRY) { my ($name) = /^\(\w+ = (\w+)\)$/; print got: $name\n; } That regex matches the (, and then some word, then = , and then saves the next word it finds. -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/ Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/ Acacia Fraternity, Rensselaer Chapter. Brother #734
RE: Very beginner question
[EMAIL PROTECTED] writes .. (name = john) - if I am trying to just extract john for the value $b, why would the following script not work. I thought it would take bothIt returns the full (name=john) #!user/local/bin/perl -w open TRY , try.txt; while (TRY) { (my $b=$_) =~ s/^(\() (\w+)/$2/; print $b; } in the name of TMTOWTDI while(TRY) { chop( my $name = substr $_, rindex( $_, ' ')+1); print $name, \n; } but I suspect (from the statement about the output being '(name=john)') that those spaces might not always be there .. in which case while(TRY) { /=\s*(.*)\)/ print $1, \n; } -- jason king In Denmark, if a horse carriage is trying to pass a car and the horse gets uneasy, the car is required to pull over and stop. If necessary to ease the horse down, you are required to cover the car up. - http://dumblaws.com/