Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #109569

solved Re: obtaining a c value from a perl script

From fred flintstone <fred@server.invalid>
Newsgroups comp.lang.c
Subject solved Re: obtaining a c value from a perl script
Date 2017-05-09 02:43 -0700
Message-ID <endhaeFbvqgU1@mid.individual.net> (permalink)
References (4 earlier) <e08349d1-be6b-46c4-b327-eb4e76571c0d@googlegroups.com> <lnd1bqexzs.fsf@kst-u.example.com> <01776996-90c9-4721-ab10-ab6149d828fa@googlegroups.com> <enat1jFqnvhU1@mid.individual.net> <32cc7dc7-4419-44a3-b0f5-50d1a7f0d400@googlegroups.com>

Show all headers | View raw


On 05/08/2017 04:39 AM, Malcolm McLean wrote:
> On Monday, May 8, 2017 at 10:45:31 AM UTC+1, fred flintstone wrote:
>> On 05/03/2017 09:02 AM, Malcolm McLean wrote:
>>>
>>>
>>> fred flintstone speaks English.
>>> I've also faced similar problems, and used similar words. Ben was possibly misled
>>> because "capture" is a term used with a specific meaning for lambda functions
>>> and closures.
>>>
>>
>> I didn't mean this, but now that you brought it up, is there a standard
>> implementation of closures?
>>
> When declaring a function that accepts a function pointer in C, it is good
> practice to always add a void * as a parameter, and accept a void * to pass
> back. Usually this is called something like the "context" pointer. If caller
> doesn't need it, it is simply null. But it can point to a structure containing
> detailed information, and it means you don't need a global (which can be almost
> unworkable if you've got more than one instance of the function pointer live).
>
> The context pointer is effectively a closure, but there's no real syntactical
> support, and it won't automatically call a destructor when the function pointer
> is destroyed.
>

Thanks for your elaboration, malcolm, it prompted the literal solution 
of this thread.

Make query number 11? [Y] :Y
to server is 11 2457987.25976179 2457987.25883747 2457987.2592592
right ascension of moon 10h 4m 0s
right ascension of sun 10h 4m 0s
moon seconds is 36240
sun seconds is 36240
equal, while condition fails at julian day 2457987.2592592
This program was called with "./jup".
Value from perl : 2457987.259259
Can't use an undefined value as a SCALAR reference at 3.neptune.pl line 102.
$

----

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
   double d;
   printf ("This program was called with \"%s\".\n",argv[0]);

   if (argc == 2)
     {
        d = atof(argv[1]);
        printf("Value from perl : %f\n", d );
        char array[20] = "Hello from C";
        return ( array );
     }
   else
     {
       printf("The program was called wrong.\n");
     }

   return 0;
}

I tried to implement toward the idea of throwing in a pointer, just in 
case a callee requires it. That's not what the subject of this thread 
was, and I've marked it solved so that no one is obligated to hang 
around to help the hapless OP. I won't pursue it here because my 
asbestos suit is giving out.

I was hoping to see a response from C, but didn't quite get there. Perl 
caller after sig.
-- 
fred

use warnings;
use strict;
use 5.010;
use WWW::Mechanize::GZip;
use HTML::TableExtract;

#use HTML::TableExtract qw(tree);
use open ':std', OUT => ':utf8';
use Prompt::Timeout;
use constant TIMEOUT  => 3;
use constant MAXTRIES => 30;

## redesign for solar eclipse of aug 21, 2017

my $site = 'http://www.fourmilab.ch/yoursky/cities.html';
my $mech = 'WWW::Mechanize::GZip'->new;
$mech->get($site);
$mech->follow_link( text => 'Portland OR' );
my $before_bound = 2457987.04167;    #before conjunction
my $after_bound  = 2457988.0;        #after conjunction
$mech->set_fields(qw'date 2');
my $moon_seconds = 5;
my $sun_seconds  = 3;
my $upper        = $after_bound;
my $lower        = $before_bound;
my $equal;
my $equal_sec;
my $now_string = localtime;
my $filename   = '3.neptune.txt';
open( my $jh, '>>', $filename ) or die "Could not open file '$filename' $!";
say $jh "Script executed at $now_string";
say $jh "       attempt  upper  lower  guess";
my $attempts = 1;

## hard code 2 objects to look at

my $e1 = 5;    #moon
my $e2 = 2;    #sun

## hard code column

my $column = 1; #right ascension

while ( ( $sun_seconds != $moon_seconds ) ) {

     my $default = ( ( $attempts >= MAXTRIES ) ) ? 'N' : 'Y';
     my $answer = prompt( "Make query number $attempts?", $default, 
TIMEOUT );
     exit if $answer =~ /^N/i;

     my $guess = closetohalf( $upper, $lower );
     say "to server is $attempts $upper $lower $guess ";
     say $jh "to server is $attempts $upper $lower $guess ";
     $mech->set_fields( jd => $guess );
     $mech->click_button( value => "Update" );
     my $te = 'HTML::TableExtract'->new;
     $te->parse( $mech->content );
     my $table = ( $te->tables )[3];    #ephemeris table

     # looking to get the whole row

     my $row_ref1 = $table->row($e1);
     my @row1     = @$row_ref1;
     say $jh "row1 is @row1";
     my $row_ref2 = $table->row($e2);
     my @row2     = @$row_ref2;
     say $jh "row2 is @row2";



     my $moon = $row1[$column];
     say "right ascension of moon $moon";
     say $jh "right ascension of moon $moon";
     my $sun = $row2[$column];
     say "right ascension of sun $sun";
     say $jh "right ascension of sun $sun";
     $moon_seconds = string_to_second($moon);
     say "moon seconds is $moon_seconds";
     say $jh "moon seconds is $moon_seconds";
     $sun_seconds = string_to_second($sun);
     say "sun seconds is $sun_seconds";
     say $jh "sun seconds is $sun_seconds";

     if ( $sun_seconds < $moon_seconds ) {
         $upper = $guess;
     }
     elsif ( $moon_seconds < $sun_seconds ) {
         $lower = $guess;
     }
     else {
         $equal = $guess;
         say "equal, while condition fails at julian day $equal";
         say $jh "equal, while condition fails at julian day $equal";
         $equal_sec = $moon_seconds;
     }

     $attempts++;
}

say $jh "equal seconds is $equal_sec";

my $return_pointer = pass_to_c( $equal );
say "return pointer pointed to $$return_pointer";

# re-design 4-29-17 for solar eclipse

sub string_to_second {
     my $string = shift;
     my $return = 9000;
     if ( my $success = $string =~ /^(\d*)h\s+(\d*)m\s+(\d*)s$/ ) {
         $return = 3600 * $1 + 60 * $2 + $3;
     }
     else {
         say "string was misformed";
     }
     return $return;
}

sub pass_to_c {
     my $string = shift;
     my $ref_to_c;
     my $ref_from_c;
     system "./jup $string";

     return $ref_from_c;
}

sub closetohalf {
     my ( $up, $low ) = @_;
     $low + ( $up - $low ) * ( 0.4 + rand 0.2 );
}

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-04-30 04:36 -0700
  Re: obtaining a c value from a perl script Noob <root@127.0.0.1> - 2017-04-30 14:48 +0200
    Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-01 02:25 -0700
      Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-01 08:46 -0700
        Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-03 02:12 -0700
          Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-03 11:48 +0100
  Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-30 16:16 +0100
    Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-01 04:18 -0700
      Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-01 13:41 +0100
        Re: obtaining a c value from a perl script Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2017-05-03 05:10 -0700
          Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-03 15:00 +0100
          Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-03 08:55 -0700
            Re: obtaining a c value from a perl script Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2017-05-03 09:02 -0700
              Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-03 17:22 +0100
                Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-08 01:43 -0700
                Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-08 09:06 -0700
                Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-09 00:35 -0700
                Re: obtaining a c value from a perl script David Brown <david.brown@hesbynett.no> - 2017-05-09 13:59 +0200
              Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-08 02:45 -0700
                Re: obtaining a c value from a perl script Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2017-05-08 04:39 -0700
                solved Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-09 02:43 -0700
                Re: solved Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-09 15:06 +0100
                Re: solved Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-10 01:03 -0700
                Re: solved Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-10 11:18 +0100
                Re: solved Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-12 02:26 -0700
                Re: solved Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-12 11:51 +0100
                Re: solved Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-12 03:49 -0700
                Re: solved Re: obtaining a c value from a perl script Manfred <invalid@invalid.add> - 2017-05-10 23:21 +0200
                Re: solved Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-10 16:23 -0700
                Re: solved Re: obtaining a c value from a perl script Robert Wessel <robertwessel2@yahoo.com> - 2017-05-11 15:57 -0500
                Re: solved Re: obtaining a c value from a perl script Jerry Stuckle <jstucklex@attglobal.net> - 2017-05-11 18:57 -0400
                Re: solved Re: obtaining a c value from a perl script Robert Wessel <robertwessel2@yahoo.com> - 2017-05-11 18:38 -0500
                Re: solved Re: obtaining a c value from a perl script Robert Wessel <robertwessel2@yahoo.com> - 2017-05-11 18:46 -0500
                Re: solved Re: obtaining a c value from a perl script Jerry Stuckle <jstucklex@attglobal.net> - 2017-05-11 21:11 -0400
                Re: solved Re: obtaining a c value from a perl script Robert Wessel <robertwessel2@yahoo.com> - 2017-05-12 01:51 -0500
                Re: solved Re: obtaining a c value from a perl script Jerry Stuckle <jstucklex@attglobal.net> - 2017-05-14 23:49 -0400
                Re: obtaining a c value from a perl script Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-09 07:46 -0700
            Re: obtaining a c value from a perl script mark.bluemel@gmail.com - 2017-05-03 09:06 -0700
              Re: obtaining a c value from a perl script gazelle@shell.xmission.com (Kenny McCormack) - 2017-05-03 17:02 +0000
              Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-08 01:51 -0700
      Re: obtaining a c value from a perl script Manfred <noname@invalid.add> - 2017-05-04 18:21 +0200
        Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-08 03:52 -0700
          Re: obtaining a c value from a perl script Jerry Stuckle <jstucklex@attglobal.net> - 2017-05-08 08:38 -0400
          Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-08 09:14 -0700
          Re: obtaining a c value from a perl script "James R. Kuyper" <jameskuyper@verizon.net> - 2017-05-08 12:17 -0400
            Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-08 09:26 -0700
              Re: obtaining a c value from a perl script jameskuyper@verizon.net - 2017-05-08 11:02 -0700
                Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-08 11:39 -0700
              Re: obtaining a c value from a perl script Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-09 07:16 -0700
                Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-09 08:18 -0700
                Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-10 00:29 -0700
                Re: obtaining a c value from a perl script Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-10 05:26 -0700
            Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-08 18:20 +0100

csiph-web