Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with if conditionals

Status
Not open for further replies.

jonmy

Technical User
Joined
Jul 8, 2007
Messages
9
Location
US
Hi

I am trying to make a perl script execute different blocks of code depending on the instruction parsed to it from an html input page

However, I am not a programmer - although I understand what is going on to some extent. Essentially the instructions are either 'Yahoo' or 'database'. I think I'm having problems setting up the conditionals correctly as well as some variables.

I am though having problems making the script execute based on the choice - at the moment it seems to only want to use 'database' (which is an internal database).

Any help would be appreciated.

$sector = "XBD"; # default sector
$threshold = 0.7; #default threshold
$duration = 6; #default duration
$periods = 0; #default duration
@dur_array = (0,0,0,0,0,0);
$datafeed = "database"; #default datafeed

# Determine which sector the user wants
@fields = split(/&/,$combo_value);
foreach $f (@fields) {
if ($f =~ m/(.*)=(.*)/) {
if ($1 eq "sector") { $sector = $2;}
if ($1 eq "threshold") { $threshold = $2;}
if ($1 eq "duration")
{
$periods++;
if ($2 eq "1") { $dur_array[0] = 1;}
elsif ($2 eq "2") { $dur_array[1] = 1;}
elsif ($2 eq "4") { $dur_array[2] = 1;}
elsif ($2 eq "12") { $dur_array[3] = 1;}
elsif ($2 eq "24") { $dur_array[4] = 1;}
elsif ($2 eq "48") { $dur_array[5] = 1;}
else { print "previous case not true";}
}

if ($1 eq "$datafeed") { $datafeed = $2;}


}
}


# now find out the components in the sector (from component text file or Yahoo DB)


if ($datafeed eq "database")
{

$comp_url = " my $v = get ($comp_url);
@components = split(",", $v);

}


elsif ($datafeed eq "Yahoo")
{

$comp_url = " my $v = get ($comp_url);
my @components = get_comps($v);
#print join(',',@components),"<BR>";

}

else { print "blah";}


print "<center><h1>Correlations between components of $sector</h1></center>\n";
$siz = $#components+1;



Jonmy
 
This looks familiar. Do you have this question posted on another forum?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
yes - but so far making very little headway...
 
Yes Kevin, he posted on TSDN

jonmy,

I wish you luck in finding help with this, however, my suggestion to you would be to simply hire a programmer. Your problem leans more towards do this project for me rather than the help me learn how to do this myself vein. And I at least am not interested in that.

- Miller
 
Where is $combo_value coming from?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It's defined above in the previous code as:


use LWP::Simple;
$size = $ENV{'CONTENT_LENGTH'};
read (STDIN, $combo_value, $size);

...but I'm not sure exactly what it refers to.
 
OK. I assume people access the script via a form on an html page. Is one of the form fields named "database"? Does it have a value of "database" and/or "Yahoo" depending on a choice made by the user? Is the spelling of the form field and the values exactly as you have posted them, including correct case?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
That's right - it's as the following...

It's meant by the way as a research tool for correlations amongst equities. Everything works - except this parsing of the datafeed information/choice. And as I'm trying to learn Perl I wanted to see if I could get help in undestanding the problem...

I don't think the problem is on the html page but it's highly possible, of course, I (or my colleague who did most of the original work) missed something.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>


<HEAD>



<TITLE>Sector Correlation Scan</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 5.50.4134.600" name=GENERATOR>


</HEAD>
<BODY>

<FORM action=" method=post name=scanform>
<TABLE style="HEIGHT: 197px" cellSpacing=2 cellPadding=2 align=center
bgColor=#0a1666 border=2>
<TBODY>
<TR>
<TD align=middle colSpan=2><FONT color=#ffffff><B>Sector Correlation Scan
<BR><FONT face=arial size=1>...choose a sector</B></FONT></FONT></TD></TR>
<TR>
<TD style="HEIGHT: 71px"><FONT color=#ffffff>
<SELECT size=5 name=sector>
<OPTION value=XBD selected>Broker Dealers (XBD)</OPTION>
<OPTION value=HGX>Housing (HGX)</OPTION>
<OPTION value=BKX>Banks (BKX)</OPTION>
<OPTION value=SOXX>Semiconductors (SOXX)</OPTION>
<OPTION value=BTK>Biotechnology (BTK)</OPTION>
<OPTION value=XOI>Oil Pumpers (XOI)</OPTION>
<OPTION value=OSX>Oil Drillers (OSX)</OPTION>
<OPTION value=XNG>Natural Gas (XNG)</OPTION>
<OPTION value=DJUSCH>Chemicals (DJUSCH)</OPTION>
<OPTION value=DFX>Defense (DFX)</OPTION>
<OPTION value=XAL>Airlines (XAL)</OPTION>
<OPTION value=DJT>Transports (DJT)</OPTION>
<OPTION value=UTY>Utilities (UTY)</OPTION>
<OPTION value=RXS>Drugs (RXS)</OPTION>
<OPTION value=HUI>Gold (HUI)</OPTION>
<OPTION value=DJI>Dow Jones Ind. (DJI)</OPTION>
</SELECT> </FONT></TD></TR>


<TR>
<TD style="HEIGHT: 50px" align=left colSpan=2><FONT face=arial size=1 color=white>&nbsp;&nbsp;Threshold<SELECT style="Z-INDEX: 101; LEFT: 16px; POSITION: relative; TOP: -1px" size=1 align=middle name=threshold>
<OPTION value=-1.00>-1.00</OPTION>
<OPTION value=-0.95>-0.95</OPTION>
<OPTION value=-0.90>-0.90</OPTION>
<OPTION value=-0.85>-0.85</OPTION>
<OPTION value=-0.80>-0.80</OPTION>
<OPTION value=-0.75>-0.75</OPTION>
<OPTION value=-0.70>-0.70</OPTION>
<OPTION value=-0.65>-0.65</OPTION>
<OPTION value=-0.60>-0.60</OPTION>
<OPTION value=-0.55>-0.55</OPTION>
<OPTION value=-0.50>-0.50</OPTION>
<OPTION value=-0.45>-0.45</OPTION>
<OPTION value=-0.40>-0.40</OPTION>
<OPTION value=-0.35>-0.35</OPTION>
<OPTION value=-0.30>-0.30</OPTION>
<OPTION value=-0.25>-0.25</OPTION>
<OPTION value=-0.20>-0.20</OPTION>
<OPTION value=-0.15>-0.15</OPTION>
<OPTION value=-0.10>-0.10</OPTION>
<OPTION value=0.10>&nbsp;0.10</OPTION>
<OPTION value=0.15>&nbsp;0.15</OPTION>
<OPTION value=0.20>&nbsp;0.20</OPTION>
<OPTION value=0.25>&nbsp;0.25</OPTION>
<OPTION value=0.30>&nbsp;0.30</OPTION>
<OPTION value=0.35>&nbsp;0.35</OPTION>
<OPTION value=0.40>&nbsp;0.40</OPTION>
<OPTION value=0.45>&nbsp;0.45</OPTION>
<OPTION value=0.50>&nbsp;0.50</OPTION>
<OPTION value=0.55>&nbsp;0.55</OPTION>
<OPTION value=0.60>&nbsp;0.60</OPTION>
<OPTION value=0.65>&nbsp;0.65</OPTION>
<OPTION value=0.70 selected>&nbsp;0.70</OPTION>
<OPTION value=0.75>&nbsp;0.75</OPTION>
<OPTION value=0.80>&nbsp;0.80</OPTION>
<OPTION value=0.85>&nbsp;0.85</OPTION>
<OPTION value=0.90>&nbsp;0.90</OPTION>
<OPTION value=0.95>&nbsp;0.95</OPTION>
<OPTION Value=1.00>&nbsp;1.00</OPTION>
</SELECT> <BR>
</TD>
</TR>


<TR><TD style="HEIGHT: 120px" align="left" colSpan=2><FONT face=arial size=1 color=white>&nbsp;&nbsp;Period<SELECT multiple style="Z-INDEX: 101; LEFT: 32px; POSITION: relative; TOP: 0px" size=6 align=middle name=duration>


<OPTION value=48 selected>12 month</OPTION>
<OPTION value=24>&nbsp;&nbsp;6 month</OPTION>
<OPTION value=12>&nbsp;&nbsp;3 month</OPTION>
<OPTION value=4>&nbsp;&nbsp;1 month</OPTION>
<OPTION value=2>&nbsp;&nbsp;2 week</OPTION>
<OPTION value=1>&nbsp;&nbsp;1 week</OPTION>
</SELECT> <BR>

</TD></TR>


<TR><TD style="HEIGHT: 50px" align="left" colSpan=2><FONT face=arial size=1 color=white>&nbsp;&nbsp;Data Feed
<SELECT style="Z-INDEX: 101; LEFT: 11px; POSITION: relative; TOP: -1px" size=1 align=middle name=datafeed>
<option value='database' selected>Database</option>
<option value='Yahoo'>Yahoo!</option>

</SELECT> <BR>

</TD></TR>




<TR><td style="HEIGHT: 50px; align=left">&nbsp;

>



<INPUT style="LEFT: 41px; POSITION: relative; TOP: -0px" type="submit" value="Scan">

</TD></TR>
</TBODY></TABLE>
</FORM>
</BODY></HTML>
 
in your script, change this line:

Code:
if ($1 eq [b]"$datafeed"[/b]) { $datafeed = $2;}

to:

Code:
if ($1 eq [b]'datafeed'[/b]) { $datafeed = $2;}

and see if that helps.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Try this:

Code:
@fields = split(/\&/,$combo_value);
foreach $f (@fields) {
chomp $f;
 
there shouldn't be any record seperators to chomp() off. The problem is as I noted above. He has a "$" where there shouldn't be one. He transformed a string into a scalar.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks Guys but still not working correctly.

Kevin: That helped a little (I obviously put one too many $ in) What's happening now is that data is being analysed from the database rout but while the Yahoo rout is not going to the database it is not pulling data from Yahoo either - it seems to skip past this instruction.


Max: did you mean putting 'chomp $f;' just before the if statement? What would be the effect of this?

ie...

@fields = split(/\&/,$combo_value);
foreach $f (@fields) {
chomp $f;

if ($f =~ m/(.*)=(.*)/) {
 
The script should now be getting into the "Yahoo" block:

Code:
    elsif ($datafeed eq "Yahoo")
          {

    $comp_url = "[URL unfurl="true"]http://finance.yahoo.com/q/cp?s=%5E".$sector;[/URL]
    my $v = get ($comp_url);
    my @components = get_comps($v);
    #print join(',',@components),"<BR>";

    }

what you need to do is determine why get() or get_comps() is not returning anything. Did you uncomment the last line in the above block?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
no - I have it exactly as you've reproduced it. last line remains commented too.

To make it even more confusing this block when used in a script on it's own, works

Originally the html had some code script in it and depending on what was chosen from the datafeed, the info would be parsed to one of two scripts - one with the database block the other with the Yahoo block. Individually in different scripts, both blocks function correctly.
 
Uncommen the last line and see if it prints something.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It prints a list horizontally at the top of the page of the sector components (although it's difficult to know where it got them from... Yahoo or database). Rest of the block still not executed.
 
do this:

Code:
    elsif ($datafeed eq "Yahoo")
          {
    [b]print "We are in the Yahoo block\n";[/b]
    $comp_url = "[URL unfurl="true"]http://finance.yahoo.com/q/cp?s=%5E".$sector;[/URL]
    my $v = get ($comp_url);
    my @components = get_comps($v);
    print join(',',@components),"<BR>";
    [b]print "Exiting Yahoo block\n";[/b] 
    }

Now you can see if if you in the Yahoo block.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I guess it is. It prints out 'We are in the yahoo block', a list of components... followed by 'Exiting Yahoo block' but it's then not being acted on to produce the correlation values.
 
Well, you will have to figure that part out or post the get_comps() part of the script.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Excuse the delay in replying...

as mentioned earlier, this works when on its own...

ie when no if statement is present but just the Yahoo block:

# now find out the components in the sector
$comp_url = "my $v = get ($comp_url);
my @components = get_comps($v);
#print join(',',@components),"<BR>";
print "<center><h1>Correlations between components of $sector</h1></center>\n";
$siz = $#components+1;



And here's the get_comps part, which again, works:

sub get_comps

{
#print "I came here!<BR>";
@lines = split(/\n/,$_[0]);
foreach $l (@lines) {
#print $l,"\n";
if ($l =~ m/pf=%5E$sector,(.*)&amp;.done/) {
#print "<h2>This line satisfied the test!</h2><BR>";
@compos = split(/,/,$1);
}
}
#print join(',',@compos),"<BR>";
return (@compos);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top