ugly code!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pedro Graca

    ugly code!

    <?php
    function ugly_array() { return array(1, 2, 3, 4, 5); }
    $arr = ugly_array();
    echo $arr[2];
    ?>

    not so ugly :)


    now ... I want to get rid of the $arr temporary variable.

    attempts (no more <?php and ?>; one attempt per line)

    echo ugly_array()[2]; ## parse error

    echo (ugly_array())[2]; ## parse error!

    echo ((ugly_array())[2]); ## parse error

    echo ({ugly_array()}[2]); ## parse error

    echo ugly_array()([2]); ## parse error

    echo +(ugly_array())[2]; ## parse error!!!!

    echo '-'.(ugly_array() )[2]; ## parse error


    and finally ... the ugly code that works

    echo implode('', array_slice(ugl y_array(), 2, 1));



    What am I missing here?
    Can't I get an element of an array returned from a function?

    What's so different about the array being named or unnamed?

    --
    ..sig
  • Pedro Graca

    #2
    Re: ugly code!

    I wrote:[color=blue]
    ><?php
    > function ugly_array() { return array(1, 2, 3, 4, 5); }[/color]
    [color=blue]
    > What am I missing here?
    > Can't I get an element of an array returned from a function?
    >
    > What's so different about the array being named or unnamed?[/color]

    tested on Linux:
    $ php -v
    PHP 4.3.3 (cli) (built: Nov 19 2003 23:12:29)
    Copyright (c) 1997-2003 The PHP Group
    Zend Engine v1.3.0, Copyright (c) 1998-2003 Zend Technologies

    tested on Windows:[color=blue]
    > cli\php -v[/color]
    PHP 5.0.0-dev (cli) (built: Feb 13 2003 00:14:28)
    Copyright (c) 1997-2003 The PHP Group
    Zend Engine v2.0.0-dev, Copyright (c) 1998-2003 Zend Technologies

    --
    ..sig

    Comment

    • R. Rajesh Jeba Anbiah

      #3
      Re: ugly code!

      Pedro Graca <hexkid@hotpop. com> wrote in message news:<bpu9kh$1s 8q62$1@ID-203069.news.uni-berlin.de>...[color=blue]
      > <?php
      > function ugly_array() { return array(1, 2, 3, 4, 5); }
      > $arr = ugly_array();
      > echo $arr[2];
      > ?>
      >
      > not so ugly :)
      >
      >
      > now ... I want to get rid of the $arr temporary variable.[/color]

      IMO, you can't.

      ---
      "Dying is an art, like everything else"---Sylvia Plath
      Email: rrjanbiah-at-Y!com

      Comment

      • Juha Suni

        #4
        Re: ugly code!

        Pedro Graca wrote:[color=blue]
        > <?php
        > function ugly_array() { return array(1, 2, 3, 4, 5); }
        > $arr = ugly_array();
        > echo $arr[2];[color=green]
        >>[/color]
        >
        > not so ugly :)
        >
        >
        > now ... I want to get rid of the $arr temporary variable.
        >
        > attempts (no more <?php and ?>; one attempt per line)[/color]

        snip

        how about modifying your function to better suite your needs:

        function ugly_array($i = "none") {
        $tmparr = array(1, 2, 3, 4, 5);
        if ($i == "none") { return $tmparr; }
        else { return $tmparr[$i]; }
        }

        This way the array works just as before if no parameters are given. But
        you can also use it like this:

        echo ugly_array(2);

        HTH

        --
        Suni

        Comment

        • Pedro Graca

          #5
          Re: ugly code!

          Juha Suni wrote:[color=blue]
          > [...]
          > how about modifying your function to better suite your needs:
          >
          > function ugly_array($i = "none") {
          > $tmparr = array(1, 2, 3, 4, 5);
          > if ($i == "none") { return $tmparr; }
          > else { return $tmparr[$i]; }
          > }
          >
          > This way the array works just as before if no parameters are given. But
          > you can also use it like this:
          >
          > echo ugly_array(2);[/color]

          Nice idea :)
          Thing is I can't change the function that returns the array (my problem
          showed up when using explode()), byt I will try a wrapper:

          function my_explode($sep arator, $string, $limit=0, $i=-1) {
          if ($limit) {
          $tmparray = explode($separa tor, $string, $limit);
          } else {
          $tmparray = explode($separa tor, $string);
          }
          if ($i>=0) {
          if ($i<count($tmpa rray)) return $tmparray[$i];
          else return NULL;
          } else {
          return $tmparray;
          }
          }

          --
          --= my mail address only accepts =--
          --= Content-Type: text/plain =--

          Comment

          • Chung Leong

            #6
            Re: ugly code!

            A tiny bit less ugly:

            echo array_pop(array _slice(ugly_arr ay(), 2, 1));

            Pedro Graca <hexkid@hotpop. com> wrote in message news:<bpu9kh$1s 8q62$1@ID-203069.news.uni-berlin.de>...[color=blue]
            > <?php
            > function ugly_array() { return array(1, 2, 3, 4, 5); }
            > $arr = ugly_array();
            > echo $arr[2];
            > ?>
            >
            > not so ugly :)
            >
            >
            > now ... I want to get rid of the $arr temporary variable.
            >
            > attempts (no more <?php and ?>; one attempt per line)
            >
            > echo ugly_array()[2]; ## parse error
            >
            > echo (ugly_array())[2]; ## parse error!
            >
            > echo ((ugly_array())[2]); ## parse error
            >
            > echo ({ugly_array()}[2]); ## parse error
            >
            > echo ugly_array()([2]); ## parse error
            >
            > echo +(ugly_array())[2]; ## parse error!!!!
            >
            > echo '-'.(ugly_array() )[2]; ## parse error
            >
            >
            > and finally ... the ugly code that works
            >
            > echo implode('', array_slice(ugl y_array(), 2, 1));
            >
            >
            >
            > What am I missing here?
            > Can't I get an element of an array returned from a function?
            >
            > What's so different about the array being named or unnamed?[/color]

            Comment

            • Thomas Mlynarczyk

              #7
              Re: ugly code!

              I often encounter a similar problem:
              A function returns an array and I just need one element from it. It seems
              illogical to me that I cannot simply write each($a)[0]. I tried to use {...}
              but it doesn't work. Instead I am forced to create a variable to store the
              result of each() and then take my element in a separate instruction. Is
              there really no way to do this in one step?

              Thomas


              Comment

              • Robin Goodall

                #8
                Re: ugly code!

                Pedro Graca wrote:[color=blue]
                > <?php
                > function ugly_array() { return array(1, 2, 3, 4, 5); }
                > $arr = ugly_array();
                > echo $arr[2];
                > ?>
                >
                > not so ugly :)
                >
                >
                > now ... I want to get rid of the $arr temporary variable.[/color]
                [snip]

                Why not just have your own index function:

                function index($arr,$i) { return $arr[$i]; }

                (probably with some bounds checking though)

                then use:

                echo index(ugly_arra y(),2);

                Robin

                Comment

                • Pedro Graca

                  #9
                  Re: ugly code!

                  Robin Goodall wrote:[color=blue]
                  > Why not just have your own index function:
                  >
                  > function index($arr,$i) { return $arr[$i]; }
                  >
                  > (probably with some bounds checking though)
                  >
                  > then use:
                  >
                  > echo index(ugly_arra y(),2);[/color]

                  Great! Thank you Robin

                  <?php
                  function index(&$arr, $i) { return $arr[$i]; }
                  function ugly_array() { return array(1, 2, 3, 4, 5); }

                  echo index(ugly_arra y(), 2);
                  // not as "beautiful" as ugly_array()[2] but nearly so :)
                  ?>

                  --
                  --= my mail address only accepts =--
                  --= Content-Type: text/plain =--

                  Comment

                  • Shawn Wilson

                    #10
                    Re: ugly code!

                    Robin Goodall wrote:[color=blue]
                    >
                    > Pedro Graca wrote:[color=green]
                    > > <?php
                    > > function ugly_array() { return array(1, 2, 3, 4, 5); }
                    > > $arr = ugly_array();
                    > > echo $arr[2];
                    > > ?>
                    > >
                    > > not so ugly :)
                    > >
                    > >
                    > > now ... I want to get rid of the $arr temporary variable.[/color]
                    > [snip]
                    >
                    > Why not just have your own index function:
                    >
                    > function index($arr,$i) { return $arr[$i]; }
                    >
                    > (probably with some bounds checking though)
                    >
                    > then use:
                    >
                    > echo index(ugly_arra y(),2);[/color]

                    That's pretty. Never though of it.

                    Thanks,
                    Shawn

                    --
                    Shawn Wilson
                    shawn@glassgian t.com

                    Comment

                    Working...