Fideist11 has asked for the wisdom of the Perl Monks concerning the following question:

This couldn't be a simpler question. say i've got a string stored in $current_board[$x] (value of $x is unimportant) and the form of the string is something like " - - -P- -K- - - - - - - - -". I want to split the string into an array with "-" as the delimiter and then i want to reassign the scalar $current_board[$x] to be a reference to the new array. Here's my current working implementation in 2 lines of code. It seems I should be able to remove the need for the @tmp variable and get it all onte one line...but how?
my @tmp = split('-',$current_board[$x]); $current_board[$x] = \@tmp;
Thanks!

Replies are listed 'Best First'.
Re: Turn 2 lines into 1
by japhy (Canon) on Jul 19, 2002 at 18:58 UTC
    $me = [ split /-/, $me ];

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Turn 2 lines into 1
by dug (Chaplain) on Jul 19, 2002 at 18:56 UTC
    You can coerce the split call to return an array ref with:
    $current_board[$x] = [ split('-',$current_board[$x]) ];

    update: Just like Aristotle says :)
    update again: and just like japhy says.

    Sometimes I think Perlmonks need to have the equivilant of outfielders calling a fly ball :)
Re: Turn 2 lines into 1
by kvale (Monsignor) on Jul 19, 2002 at 18:53 UTC
    $current_board[$x] = \@{split('-',$current_board[$x])};
    -Mark

      Nope, that doesn't work. @{} dereferences an array ref; but split returns an array, not a reference.

      The solution we are looking for here is $current_board[$x] = [ split('-',$current_board[$x]) ]; Similarly to [ ] that builds an anonymous array, { } builds an anonymous hash and returns a reference to it. Of interest: perlref/perlreftut (=references + ref tutorial), perldsc (= data structures cookbook) and perllol (= lists of lists).

      Makeshifts last the longest.

Re: Turn 2 lines into 1
by Fideist11 (Sexton) on Jul 19, 2002 at 19:02 UTC
    Awesome. Thanks for the quick answer!