Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello friends,
I have a question in perl programming.

I am trying on a logic where i have to ignore "'" (apostrophe) in a array.

Here is my code:
*************

sub func { my ($value) = @_ ; if($value =~ s/\'//) { $value = "'".$value."'"; } }

************
This works fine if the data in a column is :

&&&&&&&&&&&&&&

hello Ron, welcome to Itn'l ariport

&&&&&&&&&&&&&&
but giving ORA-01756: quoted string not properly terminated error if the data is
&&&&&&&&&&&&&&

he'llo Ron, welco'me to Itn'l ariport

&&&&&&&&&&&&&&
so i tried something like this
*************

sub func { my ($value) = @_ ; if($value =~ /\'/) { if($value[$i] = 0; $value[$i] <= length($value); $value[$i]++) { if($value =~ s/\'//); } $value = "'".$value."'"; } }

************
actually i am trying to ignore "'" (apostrophe) in a array. since i am not getting the logic atleast i am trying to replace it with nothing.

i am getting syntax errors here.

I am learing perl. Any help would be greatly appreciated.
regards,

Code tags and formatting to match OP's intent added by davido

Replies are listed 'Best First'.
Re: how to ignore ' in an array
by Joost (Canon) on Aug 03, 2005 at 15:34 UTC
Re: how to ignore ' in an array
by davorg (Chancellor) on Aug 03, 2005 at 15:49 UTC

    Well, your immediate problem is because you're only removing the first apostrophe from the string. You need to use the /g operator on the substitution operator.

    But, like Joost says, the real solution is to use the DBI quote method or placeholders.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: how to ignore ' in an array
by davidrw (Prior) on Aug 03, 2005 at 15:45 UTC
    Are you trying to ignore items in an array that have a single quote, or are you trying to rmeove single quotes from a string? If the first, than in general anything involving "ignoring X from array" means grep. If the second, note that you'll want the /g modifier so the substitution is done more than once.
    # removing items from array: my @values = ( "stuff", "has'some'quotes", "foo" ); @values = grep( $_ !~ /'/, @values ); # removing single quotes from a string: my $string = "has'some'quotes\nand a second'line'of stuff"; $string =~ s/'//sg;
      If you are just removing characters from an array of words, just tr and map.
      my @data = qw ('hello world' wondered o'brian!); print "Before: @data\n"; map {tr/'//d} @data; print "After: @data\n";
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: how to ignore ' in an array
by NateTut (Deacon) on Aug 03, 2005 at 15:55 UTC
    It sounds like your problem is more of an Oracle issue. Try replacing each ' in your string with ''. Thats how apostrophes are escaped is SQL.
      Thats how apostrophes are escaped is SQL

      Actually, that can vary with different dialects of SQL. I've seen versions where you can double quotes in order to escape them and versions where you use '\'. But if you use placeholders or the DBI quote method you don't need to know about that - as it's all hidden within the DBD module that you're using.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

        And dialect differences aside, depending on the underlying database's implementation data passed by placeholders may never be seen by the SQL parser making it that much harder for people to attempt XSS attacks (if it's not passed through the SQL parser then the black hat doesn't even get an opening to play What's my quoting mechanism?)

        --
        We're looking for people in ATL

        Hello, i tried to use place holders....... and i am getting following error: ORA-01008: not all variables bound (DBD ERROR: error possibly near <*> indicator...... Here is my code: use DBI; use DBD::Oracle qw(:ora_types); use DBD::ODBC; use Getopt::Long; main(); ########################################################### sub main { $dbh = DBI->connect("dbi:ODBC:$DSN"); $sqlstatement="SELECT * from test_table"; $dbh->{LongReadLen} = 140000; $sth = $dbh->prepare($sqlstatement); $sth->execute || DIE("Could not execute SQL statement ... maybe invalid?"); $row_access=0; while (@row=$sth->fetchrow_array) { $c1 = func("COL1",0,1); $c2 = func("COL2",1,2); $INSERT_SQL="INSERT INTO trg_table(". "column1,". "column2". ") VALUES ". "(?,". "?)"; $INSERT_CURSOR=$LDA_DB->prepare($INSERT_SQL); $rv = $INSERT_CURSOR->execute(); $INSERT_CURSOR->finish; $row_access++; } $INSERT_CURSOR->finish; $LDA_DB->disconnect; close(W); } ########################################################### sub func { # if there is no value it replaces with " " otherwise it replaces with value. } ########################################################### column1, column2 being columns of target table and COL1, COL2 of source table. can any one help me with this........ while using place holders what all the things i need to keep in mind..... regards.....