Output the answer above yours

PHP 666 611 593 588 580 Characters

Edit: Used even smaller url than previously from tinyurl.

Edit: Following comment on other answer I used int rather than boolean. Also needed to correct an error that became apparent but fixed now.

Here is my code

<?
$a=34727;$b=new DOMDocument();$b->loadHTMLFile("http://turl.no/t2u");$c=$b->saveHTML();$d=$e=1;$f='data-answerid="';$g=strpos($c,$f);$h=substr($c,0,$g);$c=substr($c,($g+15));while($d==1){$g=strpos($c,'"');$i=substr($c,0,$g);if($i==$a){$j=$h;$k=$e;}$g=strpos($c,'vote-count-post ">');$c=substr($c,($g+18));$g=strpos($c,'<');$l=substr($c,0,$g);$g=strpos($c,'data-answerid="');if($g){++$e;++$e;$h=substr($c,0,$g);$c=substr($c,($g+15));}else{$d=2;}}if($k==1){echo 'Top!';}else{$g=strpos($j,'><code>');$m=strpos($j,'</code></pre>');$n=$m-$g-7;$o=substr($j,($g+7),$n);echo nl2br($o);}

I am sure this can be improved using DOMXPath and nodes. However am quite pleased with it. I could not decide what to do in the case of mine is the only answer but with -ve points so ignored. However another answer was posted so not a problem any more.

I also used tinyurl to shorten the url, saving lots of characters.

I hope you like it,

Paul.

PS This will not run in codepad so can't show a fiddle.

Edit: saved 5 more with the php tags being altered

You can see it working here on a domain I do not currently use (so am not just trying a sneaky link). This is not a permanent link though but is currently available.


Javascript + jQuery (92 86 64 char)

To test, open Dev Tools on this page (usually F12), and run my code!

This Javscript needs to be run using the console from this page (and only this page).

alert($("code",$("#answer-34767").prev().prev()).html()||"Top!")

So short, it doesn't even need a scrollbar!

Basically, it finds my answer by ID, and then gets two nodes ahead (skipping the anchor link). If that does not exist, I must be on top. Then it digs down to the first code element and gets it's .innerHTML.

The nice thing is that SE uses jQuery by default, so I get that advantage without even trying.


Delphi (688 873 859 848 840)

I will still try to shave off some characters but it will do the job :)

Edit: instead of getting it shorter I made it longer :P
I forgot to add in the ignoring of answers when it has a negative votecount or no code blocks.

I'm just updating the un-golfed version while editing.
Thanks to @manatwork for the suggested edits that took off 8 characters.

Golfed version:

uses IdHTTP,Classes,MSHTML;const u='http://codegolf.stackexchange.com/questions/34705/output-the-answer-above-yours/34718#34718';var g:TIdHTTP;m:TMemoryStream;l:TStringList;p,t,r,o:OleVariant;s,i,j:int32;c:boolean;begin L:=TStringList.Create;g:=TIdHTTP.Create(nil);m:=TMemoryStream.Create;g.Get(u,m);m.Position:=0;L.LoadFromStream(m);p:=coHTMLDocument.Create as IHTMLDocument2;p.write(l.Text);for I:=0to p.body.all.length-1do begin c:=0>1;t:=p.body.all.item(i);if(t.classname='answer')and(t.id='answer-34718')then if s=0 then writeln('Top!') else for j:=0to o.all.length-1do begin t:=o.all.item(j);if t.tagname='CODE'then writeln(t.innertext);end else if t.classname='answer'then begin for j:=0to t.all.length-1do begin r:=t.all.item(j);if r.tagname='CODE'then c:=1>0 else if c and(r.class='vote-count-post')and(r.innertext[1]<>'-')then begin o:=t;s:=1;end;end;end;end;end.

Ungolfed version:

uses
  IdHTTP,Classes,MSHTML;
const
  u='http://codegolf.stackexchange.com/questions/34705/output-the-answer-above-yours/34718';
  a='answer';b='CODE';
var
  g:TIdHTTP;
  m:TMemoryStream;
  l:TStrings;
  p,t,r,o,z:OleVariant;
  s,i,j:int32;
  c:byte;
begin
  L:=TStringList.Create;
  g:=TIdHTTP.Create(nil);
  m:=TMemoryStream.Create;
  g.Get(u,m);
  m.Position:=0;
  L.LoadFromStream(m);
  p:=coHTMLDocument.Create as IHTMLDocument2;
  p.write(l.Text);
  z:=p.body.all;
  for I:=0to z.length-1do
  begin
    c:=0;
    t:=z.item(i);
    if(t.classname=a)and(t.id='answer-34718')then
      if s=0 then
        write('Top!')
      else
        for j:=0to o.all.length-1do
        begin
          t:=o.all.item(j);
          if t.tagname=b then
            write(t.innertext)
        end
    else if t.classname=a then
      for j:=0to t.all.length-1do
      begin
        r:=t.all.item(j);
        if r.tagname=b then
          c:=1
        else if(c=1)and(r.class='vote-count-post')and(r.innertext[1]<>'-')then
        begin
          o:=t;
          s:=1;
        end;
      end;
  end;
end.

Tags:

Code Golf