Martha kavishe am Freitag, 14. Oktober 2005 15.47:
> Hi All,

Hi Martha

Marcello already explained that the shell uses spaces to separate arguments, 
e.g.

# my_prog gu 1 

passes "gu" and "1" in @ARGV to the prog, while

# my_prog "gu 1" 

will pass only "gu 1" (without quotes; these are handled by the shell).

Apart from that, your program could be formulated much shorter.
When I understand right, you want your program accept arguments of the form

        string "gu", 
        then optionally space(s), 
        then a number between 1 and 11.


> I received some response to my problem but unfortunately my problem isn't
> solved, I'm still struggling..
>
> Thank you all for your contribution
>
> Here is a better short code for the program which gives a clear picture of
> what I'm trying to ask help for.

# *always* start your code with

use strict;
use warnings;

# this will enforce you declaring your variables below, e.g. $dsn.

> use DBI;
>
> $dsn= "dbi:mysql:db_martha:localhost";
>
> my $user= "mysql";
>
> my $password = "mysql123";
>
> $dbh = DBI->connect($dsn,$user,$password);
>
> ######GET ARGS #####
>
> $msg = $ARGV[1];
>
> $source = $ARGV[0];

In Perl, $ARGV[0] contains the first argument, not the program name.

> $msg =~ s/\"/' /igx;
>
> $time = localtime();
>
> open (OUTFILE ,">>C:\\logs\\martha.txt");

# *always* check:

open (OUTFILE ,">>C:\\logs\\martha.txt") or die "blabla: $!";

> print OUTFILE "$time\t$source\t$msg\n";
>
> close(OUTFILE);

# dito

> $found = 0;
>
> ###### GU #####

# The following if-else-forest can be transformed to:

my ($number)=$msg=~/^\s*gu\s*(\d{1,2})\s*$/i;
# (this allows also spaces before and after) 
# since 'gu' is the only allowd string, you don't have to catch it 
# into a variable.

if ($number <= 11) {
        my $vote='gu'.$number;

        # no do the db stuff
}

# to avoid using a hardcoded constand string, you could also define it at
# the begin of the program.

> if (($msg =~ /gu1/i) && ($msg !~ /gu1[0123456789]+/i))
>
> {
>
> $vote = 'gu1';
>
> $found = 1;
>
> }
>
> elsif (($msg =~ /gu2/i) && ($msg !~ /gu2[0123456789]+/i))
>
> {
>
> $vote = 'gu2';
>
> $found = 1;
>
> }
>
> .
>
> .some more elseif statements here
>
> .
>
> elsif (($msg =~ /gu10/i)&& ($msg !~ /gu10[0123456789]+/i))
>
> {
>
> $vote = 'gu10';
>
> $found = 1;
>
> }
>
> elsif (($msg =~ /gu11/i)&& ($msg !~ /gu11[0123456789]+/i))
>
> {
>
> $vote = 'gu11';
>
> $found = 1;
>
> }
>
> if ($found == 1)
>
> {
>
> $statement = "insert into t_count (time_in, sender, code) values
> (now(),'$source','$msg')"; #insert into a database
>
> $sth= $dbh->prepare("$statement");
>
> $sth->execute;
>
> print "You entered $vote .\n"; }
>
> else
>
> {
>
> print "Error message or anything....for testing";
>
> }
>
> $dbh->disconnect;

# also check result of operation here

> exit;

# exit is not needed

>
> #######EndHere######
>
> from the above code, the program accepts GU1 to GU11 but not GU 1(space
> before no), when i send GU 1, it jumps to the else statement. I want the
> program to ignore the space......
>
> I thought  by putting x on the line   ($msg =~ s/\"/' /igx;) will ignore
> the space but it doesn't
>
> Your suggestion/help are highly appreciated...
>
> Help me please

hth,

joe

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to