Regex sending me mad
Hi folks. I'm struggling to see what I'm doing wrong. I have the following code in one of my programs but it isn't working as it should. print STDERR enqmake='$enqmake' model='$model'\n; if (!$enqmake $model) { # extract make print STDERR About to split '$model'\n; if ($model=~/ *?(\w*) (.*?) *$/) { $enqmake=lc($1); $model=$2; print STDERR model split into '$enqmake' '$model'\n; } } # extract make This generates: enqmake='' model='Kia Venga' About to split 'Kia Venga' I have a test script which works fine. Can anyone see what I'm doing wrong? #!/usr/bin/perl -w use warnings; use strict; my $t='Kia Venga'; if ($t=~/ *?(\w*) (.*?) *$/) { print 1='$1' 2='$2'\n; } [root@ollie exim]# ~/t 1='Kia' 2='Venga' [root@ollie exim]# -- Gary Stainburn Group I.T. Manager Ringways Garages http://www.ringways.co.uk -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regex sending me mad
On Jul 27, 2012, at 7:04 AM, Gary Stainburn wrote: Hi folks. I'm struggling to see what I'm doing wrong. I have the following code in one of my programs but it isn't working as it should. print STDERR enqmake='$enqmake' model='$model'\n; if (!$enqmake $model) { # extract make print STDERR About to split '$model'\n; if ($model=~/ *?(\w*) (.*?) *$/) { $enqmake=lc($1); $model=$2; print STDERR model split into '$enqmake' '$model'\n; } } # extract make This generates: enqmake='' model='Kia Venga' About to split 'Kia Venga' I have a test script which works fine. Can anyone see what I'm doing wrong? No. Your script works fine for me if I precede it with the following two lines: my $model = 'Kia Venga'; my $engmake; #!/usr/bin/perl -w use warnings; use strict; my $t='Kia Venga'; if ($t=~/ *?(\w*) (.*?) *$/) { print 1='$1' 2='$2'\n; } [root@ollie exim]# ~/t 1='Kia' 2='Venga' Your test script also works fine. Therefore, it must be something else in your larger program. I suggest you use the escape sequence \s for whitespace instead of just using the space character. You should also use the x modifier so that spaces in your pattern will be ignored. That will allow you to determine by inspection what your pattern is really doing. I also use m{ } to delineate the pattern and \z to anchor the end of match instead of $: if( $model =~ m{ \s*? (\w*) \s (.*?) \s* \z }x ) { ... Why aren't you using the split function? ($model,$engmake) = split(' ',$model); -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regex sending me mad
On Fri, 27 Jul 2012 07:29:13 -0700 Jim Gibson jimsgib...@gmail.com wrote: Why aren't you using the split function? ($model,$engmake) = split(' ',$model); That would be: ($model,$engmake) = split(' ',$model, 2); See `perldoc -f split` for details. -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. _Perl links_ official site : http://www.perl.org/ beginners' help : http://learn.perl.org/faq/beginners.html advance help: http://perlmonks.org/ documentation : http://perldoc.perl.org/ news: http://perlsphere.net/ repository : http://www.cpan.org/ blog: http://blogs.perl.org/ regional groups : http://www.pm.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regex sending me mad
On Fri, Jul 27, 2012 at 9:04 AM, Gary Stainburn gary.stainb...@ringways.co.uk wrote: print STDERR About to split '$model'\n; if ($model=~/ *?(\w*) (.*?) *$/) { $enqmake=lc($1); $model=$2; print STDERR model split into '$enqmake' '$model'\n; } } # extract make This generates: enqmake='' model='Kia Venga' About to split 'Kia Venga' Your RE is a bit odd - all that 'non-greedy *' -ness implies troubles. The first space star ? can be greedy, right? You want all the spaces/white space in a row, or rather don't want - as you're anchored on the end, this doesn't do anything for the actual RE work. The next word char * means zero or more - you want at least one, right? Word char or non-white space? The only requirement your RE looks for is the single blank between capture 1 and 2 - so Kia\tVenga won't work. Actually anything w/o a blank will fail ... don't really know enough about your data but try maybe: if ($model=~/(\S+)\s+(.*)\s*$/) { -- a Andy Bach, afb...@gmail.com 608 658-1890 cell 608 261-5738 wk -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regex sending me mad
On 2012-07-27 16:58, Andy Bach wrote: if ($model=~/(\S+)\s+(.*)\s*$/) { The \s* in the end does nothing. Closer: /(\S+)\s+(.*\S)/ Then play with this: perl -Mstrict -we' my $data= $ARGV[0] ? q{Ford} : qq{ \t Fiat Ulysse 2.1 TD}; printf qq{%s %s\n}, split( q{ }, $data, 2 ), q{oops}; printf qq{%s %s\n}, $data =~ / (\S+) \s* ( (?: .* \S )? )/x; ' 1 Ford oops Ford -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
SOLVED Re: Regex sending me mad
On Friday 27 July 2012 15:58:07 Andy Bach wrote: Your RE is a bit odd - all that 'non-greedy *' -ness implies troubles. The first space star ? can be greedy, right? You want all the spaces/white space in a row, or rather don't want - as you're anchored on the end, this doesn't do anything for the actual RE work. The next word char * means zero or more - you want at least one, right? Word char or non-white space? The only requirement your RE looks for is the single blank between capture 1 and 2 - so Kia\tVenga won't work. Actually anything w/o a blank will fail ... don't really know enough about your data but try maybe: if ($model=~/(\S+)\s+(.*)\s*$/) { Thanks Andy, Shawn and Jim. The regex I'd supplied was built up over many attempts to get it working, hence the over the top spec. The problem eventually turned out to be that the space between the make and model wasn't actually a space, i.e. wasn't ASCII 32. I have now got the people generating the data to generate it correctly and all is now fine, with a much simpler regex. Gary -- Gary Stainburn Group I.T. Manager Ringways Garages http://www.ringways.co.uk -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regex sending me mad
On Fri, Jul 27, 2012 at 10:22 AM, Dr.Ruud rvtol+use...@isolution.nl wrote: On 2012-07-27 16:58, Andy Bach wrote: if ($model=~/(\S+)\s+(.*)\s*$/) { The \s* in the end does nothing. Well, I was thinking if it's a multi-word second match: v6 Austin Martinspacespace Then that would matches the rest of the phrase and trims trailing blanks. Closer: /(\S+)\s+(.*\S)/ Yeah, that's better - using the non-whitespace as anchors, so to speak! -- a Andy Bach, afb...@gmail.com 608 658-1890 cell 608 261-5738 wk -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Script to test connecting to Oracle DBs
Hi all, I am looking for a Perl script or something similar that I can use to test connection from a client PC to several databases on a server. Does anyone know of any such script lying around somewhere :(- Currently, am testing connection from a client PC to an Oracle DB using Oracle's sqlplus. All is well and good until I have to test connection to multiple databases on the server. So I am hoping to use a Perl script that I can use which will parse some sort of config file that contains hostname.tns_alias combination and then test connection to the Oracle DB and run a simple SQL like SELECT COUNT(1) from USER_TABLES, if I get an error, that means there is an issue connection to that database, otherwise all is good. At the moment, easiest I can think of using Perl's system command to run sqlplus. Or is it better to look at using DBI? Would be very much appreciated if someone can provide me a simple DBI script to start with if that is the best approach to use. Any suggestion/advice much appreciated. Thanks in advance.
Re: Script to test connecting to Oracle DBs
On 28/07/2012 02:45, newbie01 perl wrote: Hi all, I am looking for a Perl script or something similar that I can use to test connection from a client PC to several databases on a server. Does anyone know of any such script lying around somewhere :(- Currently, am testing connection from a client PC to an Oracle DB using Oracle's sqlplus. All is well and good until I have to test connection to multiple databases on the server. So I am hoping to use a Perl script that I can use which will parse some sort of config file that contains hostname.tns_alias combination and then test connection to the Oracle DB and run a simple SQL like SELECT COUNT(1) from USER_TABLES, if I get an error, that means there is an issue connection to that database, otherwise all is good. At the moment, easiest I can think of using Perl's system command to run sqlplus. Or is it better to look at using DBI? Would be very much appreciated if someone can provide me a simple DBI script to start with if that is the best approach to use. Any suggestion/advice much appreciated. Thanks in advance. I'm not clear why sqlplus won't let you connect to multiple databases, assuming you don't want to connect to them all /simultaneously/? I would say you were better off using the DBI module, and you will also need the SBS::Oracle driver module. The documentation http://metacpan.org/module/DBI http://metacpan.org/module/DBD::Oracle is very extensive, but the basic idea looks like the program below. I hope this helps. Rob use strict; use warnings; use DBI; autoflush STDOUT; my ($user, $pass) = qw/ username password /; my @databases = qw/ db1 db2 db3 db4 /; for my $dbname (@databases) { print Database: $dbname\n; my $dbh = DBI-connect(dbi:Oracle:$dbname, $user, $pass, { PrintError = 1, PrintWarn = 1 } ); next unless $dbh; my ($count) = $dbh-selectrow_array('SELECT COUNT(*) FROM user_tables'); print Database $dbname: Count: $count\n; } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/