INDEX Function

A

amerar

Ok, I should be able to figure this out, but guess not.

I have a long string here, and I'm using the INDEX function to find a
string within that. Why then, is it always returning the wrong
position??? Here are some examples. I'm looking for the string "
AND ", notice the capital letters.

Here is my INDEX statement:

for $i (0 .. $#info) {
$customer_id = $info[$i][0];
$screen_name = $info[$i][1];
$screen_query = substr($info[$i][2],4);

$and = index($screen_query, 'AND');
print "AND POSITION IS HERE: $and\n";
}

POSITION IS HERE: 8
HERE: 20,1115,1082,1083,1086,1088,1089,1090|select
master_table.ticker,master_table.comp_name,DIV_YIELD,NET_MARGIN,DEBT_TO_EQUITY,PRICE_TO_BOOK,PRICE_TO_CASH,PRICE_TO_SALES
from master_table,prices,stock_data,daily_zacks_rank where
master_table.m_ticker=stock_data.m_ticker and
master_table.m_ticker=prices.m_ticker and
master_table.m_ticker=daily_zacks_rank.m_ticker AND
upper(PRICE_TO_BOOK) <= 1.2 AND upper(PRICE_TO_CASH) >= 1.5 AND
upper(PRICE_TO_SALES) <= 1.2

POSITION IS HERE: 38
HERE: 20,1115|select master_table.ticker,master_table.comp_name from
master_table,prices,stock_data,daily_zacks_rank where
master_table.m_ticker=stock_data.m_ticker and
master_table.m_ticker=prices.m_ticker and
master_table.m_ticker=daily_zacks_rank.m_ticker AND upper(DIV_YIELD)
= 4 AND upper(DEBT_TO_EQUITY) <= .2

POSITION IS HERE: 381
HERE:
16\1020,1021,1024,1028,1115,1030,1032,1033,1040,1042,1055,1056,1057,1065,1073,1078|
select
master_table.ticker,stock_data.EXCHANGE,stock_data.SECTOR_NAME,stock_data.MKT_VALUE,master_table.comp_name,prices.CLOSING,stock_data.HIGH_52W,stock_data.LOW_52W,daily_zacks_rank.z_rank_d,AVG_RATING,Q_EST_SURP,SURP_PREV,SURP_AVG_4Q,NET_PERC_INS,TREND_EPGR,PE_12M
from master_table,prices,stock_data,daily_zacks_rank where
master_table.m_ticker=stock_data.m_ticker and
master_table.m_ticker=prices.m_ticker and
master_table.m_ticker=daily_zacks_rank.m_ticker(+) AND
upper(daily_zacks_rank.z_rank_d) <= 2 AND upper(AVG_RATING) <= 2 AND
upper(Q_EST_SURP) >= 0 AND upper(SURP_PREV) >= 0 AND
upper(SURP_AVG_4Q) >= 0 AND upper(stock_data.MKT_VALUE) >= 500 AND
upper(stock_data.PERC_CHG_12W) >= 10 AND upper(PRICE_TO_SALES) <= .5

Go figure???
 
J

J. Gleixner

Ok, I should be able to figure this out, but guess not.

I have a long string here, and I'm using the INDEX function to find a
string within that. Why then, is it always returning the wrong
position??? Here are some examples. I'm looking for the string "
AND ", notice the capital letters.

No it's looking for 'AND'.
Here is my INDEX statement:

for $i (0 .. $#info) {
$customer_id = $info[$i][0];
$screen_name = $info[$i][1];
$screen_query = substr($info[$i][2],4);

$and = index($screen_query, 'AND');
print "AND POSITION IS HERE: $and\n";
}

POSITION IS HERE: 8
HERE: 20,1115,1082,1083,1086,1088,1089,1090|select

Since you don't show us what's in $screen_query, how
can we help? Your output never contains
'AND POSITION IS HERE: ' either. Post actual
code that others can run which shows your issue.

e.g.

my $screen_query='blah..';
my $pos = index($screen_query, 'AND');
print "AND POSITION IS HERE: $pos\n" if $pos >= 0;
 
A

amerar

Ok, I should be able to figure this out, but guess not.
I have a long string here, and I'm using the INDEX function to find a
string within that. Why then, is it always returning the wrong
position??? Here are some examples. I'm looking for the string "
AND ", notice the capital letters.

No it's looking for 'AND'.


Here is my INDEX statement:
for $i (0 .. $#info) {
$customer_id = $info[$i][0];
$screen_name = $info[$i][1];
$screen_query = substr($info[$i][2],4);
$and = index($screen_query, 'AND');
print "AND POSITION IS HERE: $and\n";
}
POSITION IS HERE: 8
HERE: 20,1115,1082,1083,1086,1088,1089,1090|select

Since you don't show us what's in $screen_query, how
can we help? Your output never contains
'AND POSITION IS HERE: ' either. Post actual
code that others can run which shows your issue.

e.g.

my $screen_query='blah..';
my $pos = index($screen_query, 'AND');
print "AND POSITION IS HERE: $pos\n" if $pos >= 0;

$screen_query is the SQL query you see. The extremely long string.
Here is the code:

for $i (0 .. $#info) {
$customer_id = $info[$i][0];
$screen_name = $info[$i][1];
$screen_query = $info[$i][2];
print "HERE: $screen_query\n\n";

$string = 'select';
$d = index($screen_query, $string);
print "POSITION IS HERE: $d\n";
}
 
J

J. Gleixner

Ok, I should be able to figure this out, but guess not.
I have a long string here, and I'm using the INDEX function to find a
string within that. Why then, is it always returning the wrong
position??? Here are some examples. I'm looking for the string "
AND ", notice the capital letters.
No it's looking for 'AND'.


Here is my INDEX statement:
for $i (0 .. $#info) {
$customer_id = $info[$i][0];
$screen_name = $info[$i][1];
$screen_query = substr($info[$i][2],4);
$and = index($screen_query, 'AND');
print "AND POSITION IS HERE: $and\n";
}
POSITION IS HERE: 8
HERE: 20,1115,1082,1083,1086,1088,1089,1090|select
Since you don't show us what's in $screen_query, how
can we help? Your output never contains
'AND POSITION IS HERE: ' either. Post actual
code that others can run which shows your issue.

e.g.

my $screen_query='blah..';
my $pos = index($screen_query, 'AND');
print "AND POSITION IS HERE: $pos\n" if $pos >= 0;

$screen_query is the SQL query you see. The extremely long string.
Here is the code:

for $i (0 .. $#info) {
$customer_id = $info[$i][0];
$screen_name = $info[$i][1];
$screen_query = $info[$i][2];
print "HERE: $screen_query\n\n";

$string = 'select';
$d = index($screen_query, $string);
print "POSITION IS HERE: $d\n";
}

One last time...
*Post actual code that others can run which shows your issue.*


my $screen_query = ' some long string with select in it';
my $string = 'select';
my $d = index($screen_query, $string);
print "POSITION IS HERE: $d\n";

POSITION IS HERE: 23
 
A

amerar

(e-mail address removed) wrote:
Ok, I should be able to figure this out, but guess not.
I have a long string here, and I'm using the INDEX function to find a
string within that. Why then, is it always returning the wrong
position??? Here are some examples. I'm looking for the string "
AND ", notice the capital letters.
No it's looking for 'AND'.
Here is my INDEX statement:
for $i (0 .. $#info) {
$customer_id = $info[$i][0];
$screen_name = $info[$i][1];
$screen_query = substr($info[$i][2],4);
$and = index($screen_query, 'AND');
print "AND POSITION IS HERE: $and\n";
}
POSITION IS HERE: 8
HERE: 20,1115,1082,1083,1086,1088,1089,1090|select
Since you don't show us what's in $screen_query, how
can we help? Your output never contains
'AND POSITION IS HERE: ' either. Post actual
code that others can run which shows your issue.
e.g.
my $screen_query='blah..';
my $pos = index($screen_query, 'AND');
print "AND POSITION IS HERE: $pos\n" if $pos >= 0;
$screen_query is the SQL query you see. The extremely long string.
Here is the code:
for $i (0 .. $#info) {
$customer_id = $info[$i][0];
$screen_name = $info[$i][1];
$screen_query = $info[$i][2];
print "HERE: $screen_query\n\n";
$string = 'select';
$d = index($screen_query, $string);
print "POSITION IS HERE: $d\n";
}

One last time...
*Post actual code that others can run which shows your issue.*

my $screen_query = ' some long string with select in it';
my $string = 'select';
my $d = index($screen_query, $string);
print "POSITION IS HERE: $d\n";

POSITION IS HERE: 23

Ok, I am writing this script myself, no one else will run it. I
simply query an Oracle database and return what is in $screen_query.
You saw those values in my first post. Those were the long values.

Then, I am looking for the first occurrence of the work ' AND '. The
perl code to loop through the array is below. Thee is nothing more to
it..........

for $i (0 .. $#info) {
$customer_id = $info[$i][0];
$screen_name = $info[$i][1];
$screen_query = $info[$i][2];
print "HERE: $screen_query\n\n";

$string = ' AND ';
$d = index($screen_query, $string);
print "POSITION IS HERE: $d\n";

If you need to see the values of $query_string, there they are again:

POSITION IS HERE: 8
HERE: 20,1115,1082,1083,1086,1088,1089,1090|select
master_table.ticker,master_table.comp_name,DIV_YIELD,NET_MARGIN,DEBT_TO_EQUITY,PRICE_TO_BOOK,PRICE_TO_CASH,PRICE_TO_SALES
from master_table,prices,stock_data,daily_zacks_rank where
master_table.m_ticker=stock_data.m_ticker and
master_table.m_ticker=prices.m_ticker and
master_table.m_ticker=daily_zacks_rank.m_ticker AND
upper(PRICE_TO_BOOK) <= 1.2 AND upper(PRICE_TO_CASH) >= 1.5 AND
upper(PRICE_TO_SALES) <= 1.2

POSITION IS HERE: 38
HERE: 20,1115|select master_table.ticker,master_table.comp_name from
master_table,prices,stock_data,daily_zacks_rank where
master_table.m_ticker=stock_data.m_ticker and
master_table.m_ticker=prices.m_ticker and
master_table.m_ticker=daily_zacks_rank.m_ticker AND upper(DIV_YIELD)
= 4 AND upper(DEBT_TO_EQUITY) <= .2

POSITION IS HERE: 381
HERE:
16\1020,1021,1024,1028,1115,1030,1032,1033,1040,1042,1055,1056,1057,1065,1073,1078|
select
master_table.ticker,stock_data.EXCHANGE,stock_data.SECTOR_NAME,stock_data.MKT_VALUE,master_table.comp_name,prices.CLOSING,stock_data.HIGH_52W,stock_data.LOW_52W,daily_zacks_rank.z_rank_d,AVG_RATING,Q_EST_SURP,SURP_PREV,SURP_AVG_4Q,NET_PERC_INS,TREND_EPGR,PE_12M
from master_table,prices,stock_data,daily_zacks_rank where
master_table.m_ticker=stock_data.m_ticker and
master_table.m_ticker=prices.m_ticker and
master_table.m_ticker=daily_zacks_rank.m_ticker(+) AND
upper(daily_zacks_rank.z_rank_d) <= 2 AND upper(AVG_RATING) <= 2 AND
upper(Q_EST_SURP) >= 0 AND upper(SURP_PREV) >= 0 AND
upper(SURP_AVG_4Q) >= 0 AND upper(stock_data.MKT_VALUE) >= 500 AND
upper(stock_data.PERC_CHG_12W) >= 10 AND upper(PRICE_TO_SALES) <= .5
 
X

xhoster

$screen_query is the SQL query you see. The extremely long string.
Here is the code:

for $i (0 .. $#info) {
$customer_id = $info[$i][0];
$screen_name = $info[$i][1];
$screen_query = $info[$i][2];
print "HERE: $screen_query\n\n";

$string = 'select';

You are not searching for "AND" (notice case or not), you are searching
for "select".

$d = index($screen_query, $string);
print "POSITION IS HERE: $d\n";

You are printing the sql string before you are printing the position, while
in your example you started the "example" output out with the position
(apparently from an incomplete prior iteration), not the sql string. Once
one realizes that you are out of register, and searching for something
different than you claim you are searching for, and apparently posted the
wrong code in the first place, then your example output seems to be what
one would expect it to be.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
A

amerar

$screen_query is the SQL query you see. The extremely long string.
Here is the code:
for $i (0 .. $#info) {
$customer_id = $info[$i][0];
$screen_name = $info[$i][1];
$screen_query = $info[$i][2];
print "HERE: $screen_query\n\n";
$string = 'select';

You are not searching for "AND" (notice case or not), you are searching
for "select".
$d = index($screen_query, $string);
print "POSITION IS HERE: $d\n";

You are printing the sql string before you are printing the position, while
in your example you started the "example" output out with the position
(apparently from an incomplete prior iteration), not the sql string. Once
one realizes that you are out of register, and searching for something
different than you claim you are searching for, and apparently posted the
wrong code in the first place, then your example output seems to be what
one would expect it to be.

Xho

--
--------------------http://NewsReader.Com/--------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

That was my fault. I was trying different strings to see what kind of
results I'd get and in my copying and pasting, I probably messed
things up.

But in any case, I even printed out the length, and it is wrong, with
no patterns. Maybe there is a character in the string messing things
up???
 
D

Dave Weaver

Ok, I am writing this script myself, no one else will run it.

You are missing the point.
If you would like people here on clpm to help you solve your problem
you must show us a small-but-runnable piece of code AND data that
exhibits the problem you are seeing. Make sure you use strict and
warnings too.

For example:

#!/usr/bin/perl
use strict;
use warnings;

my $screen_query = <<END;
20,1115,1082,1083,1086,1088,1089,1090|select
master_table.ticker,master_table.comp_name,DIV_YIELD,NET_MARGIN,
DEBT_TO_EQUITY,PRICE_TO_BOOK,PRICE_TO_CASH,PRICE_TO_SALES
from master_table,prices,stock_data,daily_zacks_rank where
master_table.m_ticker=stock_data.m_ticker and
master_table.m_ticker=prices.m_ticker and
master_table.m_ticker=daily_zacks_rank.m_ticker AND
upper(PRICE_TO_BOOK) <= 1.2 AND upper(PRICE_TO_CASH) >= 1.5 AND
upper(PRICE_TO_SALES) <= 1.2
END

my $string = ' AND ';
my $d = index($screen_query, $string);
print "POSITION IS HERE: $d\n";


If you cut-and-paste the above program into perl you see the output:
POSITION IS HERE: 394

Now, please show us a sample of the problem you are having, in the
same way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top