WeixiangYan/CodeTransOcean huggingface dataset:
Task: Averages_Median




Go: package main

import (
    "fmt"
    "sort"
)

func main() {
    fmt.Println(median([]float64{3, 1, 4, 1}))    
    fmt.Println(median([]float64{3, 1, 4, 1, 5})) 
}

func median(a []float64) float64 {
    sort.Float64s(a)
    half := len(a) / 2
    m := a[half]
    if len(a)%2 == 0 {
        m = (m + a[half-1]) / 2
    }
    return m
}







Fortran: program Median_Test

  real            :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), &
                     b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /)

  print *, median(a)
  print *, median(b)

contains

  function median(a, found)
    real, dimension(:), intent(in) :: a
      
      
      
    logical, optional, intent(out) :: found
    real :: median

    integer :: l
    real, dimension(size(a,1)) :: ac

    if ( size(a,1) < 1 ) then
       if ( present(found) ) found = .false.
    else
       ac = a
       
       
       
       call sort(ac)

       l = size(a,1)
       if ( mod(l, 2) == 0 ) then
          median = (ac(l/2+1) + ac(l/2))/2.0
       else
          median = ac(l/2+1)
       end if

       if ( present(found) ) found = .true.
    end if

  end function median

end program Median_Test
Task: Averages_Median





Java: 
public static double median(List<Double> list) {
    Collections.sort(list);
    return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2;
}






Fortran: program Median_Test

  real            :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), &
                     b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /)

  print *, median(a)
  print *, median(b)

contains

  function median(a, found)
    real, dimension(:), intent(in) :: a
      
      
      
    logical, optional, intent(out) :: found
    real :: median

    integer :: l
    real, dimension(size(a,1)) :: ac

    if ( size(a,1) < 1 ) then
       if ( present(found) ) found = .false.
    else
       ac = a
       
       
       
       call sort(ac)

       l = size(a,1)
       if ( mod(l, 2) == 0 ) then
          median = (ac(l/2+1) + ac(l/2))/2.0
       else
          median = ac(l/2+1)
       end if

       if ( present(found) ) found = .true.
    end if

  end function median

end program Median_Test
Task: Averages_Median







Python: def median(aray):
    srtd = sorted(aray)
    alen = len(srtd)
    return 0.5*( srtd[(alen-1)//2] + srtd[alen//2])

a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2)
print a, median(a)
a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2)
print a, median(a)




Fortran: program Median_Test

  real            :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), &
                     b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /)

  print *, median(a)
  print *, median(b)

contains

  function median(a, found)
    real, dimension(:), intent(in) :: a
      
      
      
    logical, optional, intent(out) :: found
    real :: median

    integer :: l
    real, dimension(size(a,1)) :: ac

    if ( size(a,1) < 1 ) then
       if ( present(found) ) found = .false.
    else
       ac = a
       
       
       
       call sort(ac)

       l = size(a,1)
       if ( mod(l, 2) == 0 ) then
          median = (ac(l/2+1) + ac(l/2))/2.0
       else
          median = ac(l/2+1)
       end if

       if ( present(found) ) found = .true.
    end if

  end function median

end program Median_Test
Task: Averages_Median








VB: Private Function medianq(s As Variant) As Double
    Dim res As Double, tmp As Integer
    Dim l As Integer, k As Integer
    res = 0
    l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1)
        If l Then
            res = quick_select(s, k)
            If l Mod 2 = 0 Then
                tmp = quick_select(s, k + 1)
                res = (res + tmp) / 2
            End If
        End If
    medianq = res
End Function
Public Sub main2()
    s = [{4, 2, 3, 5, 1, 6}]
    Debug.Print medianq(s)
End Sub



Fortran: program Median_Test

  real            :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), &
                     b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /)

  print *, median(a)
  print *, median(b)

contains

  function median(a, found)
    real, dimension(:), intent(in) :: a
      
      
      
    logical, optional, intent(out) :: found
    real :: median

    integer :: l
    real, dimension(size(a,1)) :: ac

    if ( size(a,1) < 1 ) then
       if ( present(found) ) found = .false.
    else
       ac = a
       
       
       
       call sort(ac)

       l = size(a,1)
       if ( mod(l, 2) == 0 ) then
          median = (ac(l/2+1) + ac(l/2))/2.0
       else
          median = ac(l/2+1)
       end if

       if ( present(found) ) found = .true.
    end if

  end function median

end program Median_Test
Task: Averages_Median






PHP: function median($arr)
{
    sort($arr);
    $count = count($arr); //count the number of values in array
    $middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value
    if ($count % 2) { // odd number, middle is the median
        $median = $arr[$middleval];
    } else { // even number, calculate avg of 2 medians
        $low = $arr[$middleval];
        $high = $arr[$middleval+1];
        $median = (($low+$high)/2);
    }
    return $median;
}

echo median(array(4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2)) . "\n";  // 4.4
echo median(array(4.1, 7.2, 1.7, 9.3, 4.4, 3.2)) . "\n";       // 4.25





Fortran: program Median_Test

  real            :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), &
                     b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /)

  print *, median(a)
  print *, median(b)

contains

  function median(a, found)
    real, dimension(:), intent(in) :: a
      
      
      
    logical, optional, intent(out) :: found
    real :: median

    integer :: l
    real, dimension(size(a,1)) :: ac

    if ( size(a,1) < 1 ) then
       if ( present(found) ) found = .false.
    else
       ac = a
       
       
       
       call sort(ac)

       l = size(a,1)
       if ( mod(l, 2) == 0 ) then
          median = (ac(l/2+1) + ac(l/2))/2.0
       else
          median = ac(l/2+1)
       end if

       if ( present(found) ) found = .true.
    end if

  end function median

end program Median_Test
Task: Averages_Median

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

typedef struct floatList {
    float *list;
    int   size;
} *FloatList;

int floatcmp( const void *a, const void *b) {
    if (*(const float *)a < *(const float *)b) return -1;
    else return *(const float *)a > *(const float *)b;
}

float median( FloatList fl )
{
    qsort( fl->list, fl->size, sizeof(float), floatcmp);
    return 0.5 * ( fl->list[fl->size/2] + fl->list[(fl->size-1)/2]);
}

int main()
{
    static float floats1[] = { 5.1, 2.6, 6.2, 8.8, 4.6, 4.1 };
    static struct floatList flist1 = { floats1, sizeof(floats1)/sizeof(float) };

    static float floats2[] = { 5.1, 2.6, 8.8, 4.6, 4.1 };
    static struct floatList flist2 = { floats2, sizeof(floats2)/sizeof(float) };

    printf("flist1 median is %7.2f\n", median(&flist1)); 
    printf("flist2 median is %7.2f\n", median(&flist2)); 
    return 0;
}









COBOL: FUNCTION MEDIAN(some-table (ALL))
Task: Averages_Median



C#: using System;
using System.Linq;

namespace Test
{
    class Program
    {
        static void Main()
        {
            double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 };

            myArr = myArr.OrderBy(i => i).ToArray();
            

            int mid = myArr.Length / 2;
            double median;

            if (myArr.Length % 2 == 0)
            {
                
                median = (myArr[mid] + myArr[mid - 1]) / 2.0;
            }
            else
            {
                
                median = myArr[mid];
            }

            Console.WriteLine(median);
            Console.ReadLine();
        }
    }
}







COBOL: FUNCTION MEDIAN(some-table (ALL))
Task: Averages_Median


C++: #include <algorithm>



template <typename Iterator>
double median(Iterator begin, Iterator end) {
  
  Iterator middle = begin + (end - begin) / 2;

  
  std::nth_element(begin, middle, end);

  if ((end - begin) % 2 != 0) { 
    return *middle;
  } else { 
    
    Iterator lower_middle = std::max_element(begin, middle);
    return (*middle + *lower_middle) / 2.0;
  }
}

#include <iostream>

int main() {
  double a[] = {4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2};
  double b[] = {4.1, 7.2, 1.7, 9.3, 4.4, 3.2};

  std::cout << median(a+0, a + sizeof(a)/sizeof(a[0])) << std::endl; 
  std::cout << median(b+0, b + sizeof(b)/sizeof(b[0])) << std::endl; 

  return 0;
}








COBOL: FUNCTION MEDIAN(some-table (ALL))
Task: Averages_Median





Java: 
public static double median(List<Double> list) {
    Collections.sort(list);
    return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2;
}





COBOL: FUNCTION MEDIAN(some-table (ALL))
Task: Averages_Median







Python: def median(aray):
    srtd = sorted(aray)
    alen = len(srtd)
    return 0.5*( srtd[(alen-1)//2] + srtd[alen//2])

a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2)
print a, median(a)
a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2)
print a, median(a)



COBOL: FUNCTION MEDIAN(some-table (ALL))
Task: Averages_Median








VB: Private Function medianq(s As Variant) As Double
    Dim res As Double, tmp As Integer
    Dim l As Integer, k As Integer
    res = 0
    l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1)
        If l Then
            res = quick_select(s, k)
            If l Mod 2 = 0 Then
                tmp = quick_select(s, k + 1)
                res = (res + tmp) / 2
            End If
        End If
    medianq = res
End Function
Public Sub main2()
    s = [{4, 2, 3, 5, 1, 6}]
    Debug.Print medianq(s)
End Sub


COBOL: FUNCTION MEDIAN(some-table (ALL))
Task: Averages_Median




Go: package main

import (
    "fmt"
    "sort"
)

func main() {
    fmt.Println(median([]float64{3, 1, 4, 1}))    
    fmt.Println(median([]float64{3, 1, 4, 1, 5})) 
}

func median(a []float64) float64 {
    sort.Float64s(a)
    half := len(a) / 2
    m := a[half]
    if len(a)%2 == 0 {
        m = (m + a[half-1]) / 2
    }
    return m
}






COBOL: FUNCTION MEDIAN(some-table (ALL))
Task: Averages_Median






PHP: function median($arr)
{
    sort($arr);
    $count = count($arr); //count the number of values in array
    $middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value
    if ($count % 2) { // odd number, middle is the median
        $median = $arr[$middleval];
    } else { // even number, calculate avg of 2 medians
        $low = $arr[$middleval];
        $high = $arr[$middleval+1];
        $median = (($low+$high)/2);
    }
    return $median;
}

echo median(array(4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2)) . "\n";  // 4.4
echo median(array(4.1, 7.2, 1.7, 9.3, 4.4, 3.2)) . "\n";       // 4.25
Task: Averages_Median






PHP: function median($arr)
{
    sort($arr);
    $count = count($arr); //count the number of values in array
    $middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value
    if ($count % 2) { // odd number, middle is the median
        $median = $arr[$middleval];
    } else { // even number, calculate avg of 2 medians
        $low = $arr[$middleval];
        $high = $arr[$middleval+1];
        $median = (($low+$high)/2);
    }
    return $median;
}

echo median(array(4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2)) . "\n";  // 4.4
echo median(array(4.1, 7.2, 1.7, 9.3, 4.4, 3.2)) . "\n";       // 4.25






Ada: with Ada.Text_IO, Ada.Float_Text_IO;

procedure FindMedian is

    f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 );
    min_idx: integer;
    min_val, median_val, swap: float;

begin
    for i in f'range loop
        min_idx := i;
        min_val := f(i);
        for j in i+1 .. f'last loop
            if f(j) < min_val then
                min_idx := j;
                min_val := f(j);
            end if;                
        end loop;
        swap := f(i); f(i) := f(min_idx); f(min_idx) := swap;
    end loop;      

    if f'length mod 2 /= 0 then
        median_val := f( f'length/2+1 );
    else
        median_val := ( f(f'length/2) + f(f'length/2+1) ) / 2.0;
    end if;
    
    Ada.Text_IO.Put( "Median value: " );
    Ada.Float_Text_IO.Put( median_val );
    Ada.Text_IO.New_line;    
end FindMedian;
Task: Averages_Median






PHP: function median($arr)
{
    sort($arr);
    $count = count($arr); //count the number of values in array
    $middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value
    if ($count % 2) { // odd number, middle is the median
        $median = $arr[$middleval];
    } else { // even number, calculate avg of 2 medians
        $low = $arr[$middleval];
        $high = $arr[$middleval+1];
        $median = (($low+$high)/2);
    }
    return $median;
}

echo median(array(4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2)) . "\n";  // 4.4
echo median(array(4.1, 7.2, 1.7, 9.3, 4.4, 3.2)) . "\n";       // 4.25




COBOL: FUNCTION MEDIAN(some-table (ALL))
Task: Pointers and references



C#: static void Main(string[] args)
{
	int p;

	p = 1;
	Console.WriteLine("Ref Before: " + p);
	Value(ref p);
	Console.WriteLine("Ref After : " + p);

	p = 1;
	Console.WriteLine("Val Before: " + p);
	Value(p);
	Console.WriteLine("Val After : " + p);

	Console.ReadLine();
}

private static void Value(ref int Value)
{
	Value += 1;
}
private static void Value(int Value)
{
	Value += 1;
}









Ada: type Int_Access is access Integer;
Int_Acc : Int_Access := new Integer'(5);
Task: Pointers and references

C: inline unsigned char foo (unsigned char a, unsigned char b, unsigned char c){
     return a+b+c;
}











Ada: type Int_Access is access Integer;
Int_Acc : Int_Access := new Integer'(5);
Task: Pointers and references





Java:  public class Foo { public int x = 0; }

 void somefunction() {
     Foo a; 
     a = new Foo(); 
     Foo b = a; 
     a.x = 5; 
     System.out.println(b.x); 
 }







Ada: type Int_Access is access Integer;
Int_Acc : Int_Access := new Integer'(5);
Task: Pointers and references







Python:  
 a = "foo"
 
 b = []
 
 class Foo(object):
     pass
 c = Foo()
 
 class Bar(object):
     def __init__(self, initializer = None)
         
         if initializer is not None:
            self.value = initializer
 d = Bar(10)
 print d.value
 
 if a is b: pass
 
 if id(a) == id(b): pass
 
 def a(fmt, *args):
     if fmt is None:
         fmt = "%s"
      print fmt % (args)
 
 b.append(a)
 
 del(a)
 
 b[0]("foo")  
              





Ada: type Int_Access is access Integer;
Int_Acc : Int_Access := new Integer'(5);
Task: Pointers and references








VB: Dim samplevariable as New Object
Dim anothervariable as Object
Set anothervariable = sameplevariable




Ada: type Int_Access is access Integer;
Int_Acc : Int_Access := new Integer'(5);
Task: Pointers and references

C: inline unsigned char foo (unsigned char a, unsigned char b, unsigned char c){
     return a+b+c;
}
Task: Pointers and references



C#: static void Main(string[] args)
{
	int p;

	p = 1;
	Console.WriteLine("Ref Before: " + p);
	Value(ref p);
	Console.WriteLine("Ref After : " + p);

	p = 1;
	Console.WriteLine("Val Before: " + p);
	Value(p);
	Console.WriteLine("Val After : " + p);

	Console.ReadLine();
}

private static void Value(ref int Value)
{
	Value += 1;
}
private static void Value(int Value)
{
	Value += 1;
}
Task: Pointers and references


C++: int* pointer2(&var);
Task: Pointers and references





Java:  public class Foo { public int x = 0; }

 void somefunction() {
     Foo a; 
     a = new Foo(); 
     Foo b = a; 
     a.x = 5; 
     System.out.println(b.x); 
 }
Task: Pointers and references







Python:  
 a = "foo"
 
 b = []
 
 class Foo(object):
     pass
 c = Foo()
 
 class Bar(object):
     def __init__(self, initializer = None)
         
         if initializer is not None:
            self.value = initializer
 d = Bar(10)
 print d.value
 
 if a is b: pass
 
 if id(a) == id(b): pass
 
 def a(fmt, *args):
     if fmt is None:
         fmt = "%s"
      print fmt % (args)
 
 b.append(a)
 
 del(a)
 
 b[0]("foo")
Task: Pointers and references








VB: Dim samplevariable as New Object
Dim anothervariable as Object
Set anothervariable = sameplevariable
Task: Pointers and references




Go: var p *int  
i = &p
Task: Pointers and references



C#: static void Main(string[] args)
{
	int p;

	p = 1;
	Console.WriteLine("Ref Before: " + p);
	Value(ref p);
	Console.WriteLine("Ref After : " + p);

	p = 1;
	Console.WriteLine("Val Before: " + p);
	Value(p);
	Console.WriteLine("Val After : " + p);

	Console.ReadLine();
}

private static void Value(ref int Value)
{
	Value += 1;
}
private static void Value(int Value)
{
	Value += 1;
}








Fortran: real, pointer :: pointertoreal
Task: Pointers and references





Java:  public class Foo { public int x = 0; }

 void somefunction() {
     Foo a; 
     a = new Foo(); 
     Foo b = a; 
     a.x = 5; 
     System.out.println(b.x); 
 }






Fortran: real, pointer :: pointertoreal
Task: Pointers and references







Python:  
 a = "foo"
 
 b = []
 
 class Foo(object):
     pass
 c = Foo()
 
 class Bar(object):
     def __init__(self, initializer = None)
         
         if initializer is not None:
            self.value = initializer
 d = Bar(10)
 print d.value
 
 if a is b: pass
 
 if id(a) == id(b): pass
 
 def a(fmt, *args):
     if fmt is None:
         fmt = "%s"
      print fmt % (args)
 
 b.append(a)
 
 del(a)
 
 b[0]("foo")  
              




Fortran: real, pointer :: pointertoreal
Task: Pointers and references






PHP: <?php

$a = 1;
$b =& $a; // $b and $a are now linked together
$b = 2; //both $b and $a now equal 2
$c = $b;
$c = 7; //$c is not a reference; no change to $a or $b
unset($a); //won't unset $b, just $a.


function &pass_out() {
    global $filestr;  //$exactly equivalent to: $filestr =& $_GLOBALS['filestr'];

    $filestr = get_file_contents("./bigfile.txt");
    return $_GLOBALS['filestr'];
}
function pass_in(&$in_filestr) {
    echo "File Content Length: ". strlen($in_filestr);

    
    $in_filestr .= "EDIT";
    echo "File Content Length is now longer: ". strlen($in_filestr);
}

$tmp = &pass_out(); // now $tmp and the global variable $filestr are linked
pass_in($tmp); // changes $tmp and prints the length

?>





Fortran: real, pointer :: pointertoreal
Task: Pointers and references

C: inline unsigned char foo (unsigned char a, unsigned char b, unsigned char c){
     return a+b+c;
}









COBOL:        01  ptr                     USAGE POINTER TO Some-Type.
       01  prog-ptr                USAGE PROGRAM-POINTER "some-program".
Task: Pointers and references



C#: static void Main(string[] args)
{
	int p;

	p = 1;
	Console.WriteLine("Ref Before: " + p);
	Value(ref p);
	Console.WriteLine("Ref After : " + p);

	p = 1;
	Console.WriteLine("Val Before: " + p);
	Value(p);
	Console.WriteLine("Val After : " + p);

	Console.ReadLine();
}

private static void Value(ref int Value)
{
	Value += 1;
}
private static void Value(int Value)
{
	Value += 1;
}







COBOL:        01  ptr                     USAGE POINTER TO Some-Type.
       01  prog-ptr                USAGE PROGRAM-POINTER "some-program".
Task: Pointers and references


C++: int* pointer2(&var);








COBOL:        01  ptr                     USAGE POINTER TO Some-Type.
       01  prog-ptr                USAGE PROGRAM-POINTER "some-program".
Task: Pointers and references





Java:  public class Foo { public int x = 0; }

 void somefunction() {
     Foo a; 
     a = new Foo(); 
     Foo b = a; 
     a.x = 5; 
     System.out.println(b.x); 
 }





COBOL:        01  ptr                     USAGE POINTER TO Some-Type.
       01  prog-ptr                USAGE PROGRAM-POINTER "some-program".
Task: Pointers and references







Python:  
 a = "foo"
 
 b = []
 
 class Foo(object):
     pass
 c = Foo()
 
 class Bar(object):
     def __init__(self, initializer = None)
         
         if initializer is not None:
            self.value = initializer
 d = Bar(10)
 print d.value
 
 if a is b: pass
 
 if id(a) == id(b): pass
 
 def a(fmt, *args):
     if fmt is None:
         fmt = "%s"
      print fmt % (args)
 
 b.append(a)
 
 del(a)
 
 b[0]("foo")  
              



COBOL:        01  ptr                     USAGE POINTER TO Some-Type.
       01  prog-ptr                USAGE PROGRAM-POINTER "some-program".
Task: Pointers and references








VB: Dim samplevariable as New Object
Dim anothervariable as Object
Set anothervariable = sameplevariable


COBOL:        01  ptr                     USAGE POINTER TO Some-Type.
       01  prog-ptr                USAGE PROGRAM-POINTER "some-program".
Task: Pointers and references




Go: var p *int  
i = &p      






COBOL:        01  ptr                     USAGE POINTER TO Some-Type.
       01  prog-ptr                USAGE PROGRAM-POINTER "some-program".
Task: Pointers and references






PHP: <?php

$a = 1;
$b =& $a; // $b and $a are now linked together
$b = 2; //both $b and $a now equal 2
$c = $b;
$c = 7; //$c is not a reference; no change to $a or $b
unset($a); //won't unset $b, just $a.


function &pass_out() {
    global $filestr;  //$exactly equivalent to: $filestr =& $_GLOBALS['filestr'];

    $filestr = get_file_contents("./bigfile.txt");
    return $_GLOBALS['filestr'];
}
function pass_in(&$in_filestr) {
    echo "File Content Length: ". strlen($in_filestr);

    
    $in_filestr .= "EDIT";
    echo "File Content Length is now longer: ". strlen($in_filestr);
}

$tmp = &pass_out(); // now $tmp and the global variable $filestr are linked
pass_in($tmp); // changes $tmp and prints the length

?>






Ada: type Int_Access is access Integer;
Int_Acc : Int_Access := new Integer'(5);
Task: Pointers and references






PHP: <?php

$a = 1;
$b =& $a; // $b and $a are now linked together
$b = 2; //both $b and $a now equal 2
$c = $b;
$c = 7; //$c is not a reference; no change to $a or $b
unset($a); //won't unset $b, just $a.


function &pass_out() {
    global $filestr;  //$exactly equivalent to: $filestr =& $_GLOBALS['filestr'];

    $filestr = get_file_contents("./bigfile.txt");
    return $_GLOBALS['filestr'];
}
function pass_in(&$in_filestr) {
    echo "File Content Length: ". strlen($in_filestr);

    
    $in_filestr .= "EDIT";
    echo "File Content Length is now longer: ". strlen($in_filestr);
}

$tmp = &pass_out(); // now $tmp and the global variable $filestr are linked
pass_in($tmp); // changes $tmp and prints the length

?>
Task: Pointers and references






PHP: <?php

$a = 1;
$b =& $a; // $b and $a are now linked together
$b = 2; //both $b and $a now equal 2
$c = $b;
$c = 7; //$c is not a reference; no change to $a or $b
unset($a); //won't unset $b, just $a.


function &pass_out() {
    global $filestr;  //$exactly equivalent to: $filestr =& $_GLOBALS['filestr'];

    $filestr = get_file_contents("./bigfile.txt");
    return $_GLOBALS['filestr'];
}
function pass_in(&$in_filestr) {
    echo "File Content Length: ". strlen($in_filestr);

    
    $in_filestr .= "EDIT";
    echo "File Content Length is now longer: ". strlen($in_filestr);
}

$tmp = &pass_out(); // now $tmp and the global variable $filestr are linked
pass_in($tmp); // changes $tmp and prints the length

?>




COBOL:        01  ptr                     USAGE POINTER TO Some-Type.
       01  prog-ptr                USAGE PROGRAM-POINTER "some-program".