in reply to How to return a two dimensional array from a function in Perl?

without changing the old matrix which is passed as an argument to this function.

There's no such thing as a 2d array in Perl. There are arrays whose elements are references to other arrays. That's what my (@graph) = @_; doesn't make a copy of the "2d array". Search for "deep copy"

Other bugs:

Other tips:

Replies are listed 'Best First'.
Re^2: How to return a two dimensional array from a function in Perl?
by graff (Chancellor) on Nov 03, 2008 at 03:40 UTC
    and and or are designed to be used to join statements. Use && and || inside of expressions.

    Huh? What practical difference is there between this:

    if (($graph[$i][$j] == 0) and ($i != $j))
    and this:
    if (($graph[$i][$j] == 0) && ($i != $j))
    apart from the fact the former is more English-like? Isn't each condition around the conjunction a kind of statement? (What exactly is the difference between a "statement" and an "expression"? -- actually, I'm not sure I want to get into that...)

      What practical difference is there between this:

      One practice is more likely to eventually burn you than the other.

      What exactly is the difference between a "statement" and an "expression"?

      I suppose it'd be safer to look at whether the program's flow is being controlled or not. or next, or return, or die, etc.

        One practice is more likely to eventually burn you than the other.

        Please correct me if I'm wrong: The important point about choosing between "and/or" vs. "&&/||" involves their differences in precedence relative to other operators (especially the "comma operator"); the classic example (often seen in SoPW questions) being:

        open $fh, "<", $filename || die "oops!"; # should use "or" here # (or else put parens around the args for the " +open()" call)
        But the "burn" only happens when you use  &&/|| in a context where  and/or would be needed to do the right thing. In contrast, I can't think of any context appropriate for  &&/|| such that  and/or wouldn't serve equally well.

        If you can find a context where  &&/|| produce an intended result and replacing them with  and/or produces a different (incorrect) result, that would be remarkably informative (and surprising, I think). NB: No fair making up obfu examples that depend on l-to-r vs. r-to-l differences -- people who get into that kind of trouble need other kinds of help and advice.

        I'm guessing that BrowserUK has a similar view, and the reason we're both giving you grief about this is that you seem to be suggesting there are situations where using  and/or should be disfavored or unadvisable, whereas B and I would rather say that the better advice is "when in doubt, use  and/or" (which is what the OP seemed to be doing in the first place).

Re^2: How to return a two dimensional array from a function in Perl?
by BrowserUk (Patriarch) on Nov 03, 2008 at 09:23 UTC
    and and or are designed to be used to join statements. Use && and || inside of expressions.

    Go on, I'll bite. Is there any matter of substance--like a practical difference in the results, or some hidden trap for the unwary--behind this statement?

    Or is it just another icky meme based on personel preference and groundless dogma.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Is there ... some hidden trap for the unwary--behind this statement?

      Yes, but it's not hidden. The precedence difference is documented. It comes up often enough, but I can't remember any examples.

      Or is it just another icky meme based on personel preference and groundless dogma.

      None of my style decisions are groundless.

        my style decisions

        That sounds a lot like "personal preference".

        And if "the precedence difference." is your justifiction of entreating other to [only] "Use && and || inside of expressions.", and you "can't remember any examples" of how not following that entreaty will cause any problems, (beyond perhaps offending your sensibilities), then it also sounds pretty groundless.

        As someone once said: In the absence of evidence, opinion is indistinguishable from prejudice.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: How to return a two dimensional array from a function in Perl?
by gunners.newark (Initiate) on Nov 03, 2008 at 17:05 UTC
    Thanks a lot for your suggestions ikegami.
      Hi,

      I am trying to do a simple return of a multidimensional array. I hoping to keep it simple, clean, so that I may use channel programming AI like ZPX Engine. $LON is a multidimensional array for this example. Don't care about the array reference at [0]. Want to return $LON and cleanly receive it into a target from the subroutine. [Left Hand] = [Right Hand] programming style. Basically an exact copy without re-processing it again. $a = $b; done.

      Super simple example below, did not even put in strict and warnings.

      Thank you in advanced.

      $ListPeople = (); $ListPeople = &ListOfNames; $counter = 0; while( $ListPeople[$counter][0] ne "" ){ print "$counter. What is my Name: $ListPeople[$counter][0] $ListPe +ople[$counter][1] $ListPeople[$counter][2]\n\n"; $counter++; }; sub ListOfNames{ $LON = (); $LON[0][0] = "Tim"; $LON[0][1] = "O"; $LON[0][2] = "Tay"; $LON[1][0] = "John"; $LON[1][1] = "Fitz"; $LON[1][2] = "Gerald"; $LON[2][0] = "Gerald"; $LON[2][1] = "Fitz"; $LON[2][2] = "John"; return( $LON ); }; exit(0);

      Output Should look like

      0. What is my Name: Tim O Tay

      1. What is my Name: John Fitz Gerald

      2. What is my Name: Gerald Fitz John

        > Super simple example below, did not even put in strict and warnings.

        But using strict and warnings would have told you what's wrong.

        These are two different variables:

        $LON = (); $LON[0][0] = "Tim";

        Two solutions, either

        my $LON ; # scalar is array ref $LON->[0][0] = "Tim"; ... return $LON;

        or

        my @LON; # array $LON[0][0] = "Tim"; ... return \@LON; # return ref

        See perldsc and perlref for more

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        A version showing some variations of technique (tested under Perl 5.8.9):

        c:\@Work\Perl\monks>perl -e "use warnings; use strict; ;; my @ListPeople = ListOfNames(); ;; PERSON: for my $counter (0 .. $#ListPeople) { my $ar_person = $ListPeople[$counter]; last PERSON if $ar_person->[0] eq ''; print qq{$counter. What is my Name: @$ar_person \n\n}; }; ;; sub ListOfNames { my @LON; ;; $LON[0][0] = 'Tim'; $LON[0][1] = 'O'; $LON[0][2] = 'Tay'; ;; $LON[1] = [ 'John', 'Fitz', 'Gerald' ]; ;; $LON[2] = [ qw(Gerald Fitz John) ]; ;; push @LON, [ '', 'not', 'here' ], [ 'Billy', 'Bob', 'Thornton' ]; ;; return @LON; } " 0. What is my Name: Tim O Tay 1. What is my Name: John Fitz Gerald 2. What is my Name: Gerald Fitz John
        The  ListOfNames() function could simply be written as:
        sub ListOfNames { return [ 'Tim', 'O', 'Tay' ], [ 'John', 'Fitz', 'Gerald' ], [ qw(Gerald Fitz John) ], [ '', 'not', 'present' ], [ 'Billy', 'Bob', 'Thornton' ], ; }


        Give a man a fish:  <%-{-{-{-<