Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

my $var = join('|',@array) not behaving

by stuffy (Monk)
on May 09, 2008 at 02:10 UTC ( #685583=perlquestion: print w/replies, xml ) Need Help??
stuffy has asked for the wisdom of the Perl Monks concerning the following question:

This isn't doing as I expect. Am I missing something simple? here
my @array=[1,"word",2]; my $print_string = join ('|', @array); print "$print_string \n"; print Dumper(@array); print Dumper($print_string);
my output is :
$VAR1 = [ 1, 'word', 2 ]; $VAR1 = 'ARRAY(0x8153c28)';
I would expect that $print_string = "1 | word | 2"


Stuffy
That's my story, and I'm sticking to it, unless I'm wrong in which case I will probably change it ;~)
may be reproduced under the SDL

Replies are listed 'Best First'.
Re: my $var = join('|',@array) not behaving
by ikegami (Pope) on May 09, 2008 at 02:18 UTC

    Always pass a scalar to Dumper. If you had done print Dumper(\@array);, it would have been easier to notice the array contains only one element (a reference to an array containing three elements).

    The problem is that
    my @array=[1,"word",2];
    should be
    my @array=(1,"word",2);

    The [ ] operator creates an array and returns a reference to that array. Not what you want at all. You just wanted to build a list.

Re: my $var = join('|',@array) not behaving
by grep (Monsignor) on May 09, 2008 at 02:24 UTC
    Square brackets [] are for creating a reference to an anonymous array. Parens () construct a list which you can assign to an array.
    You can read more at perldata and perlref

    Try This:

    my @array = ( 1, "word", 2 ); my $print_string = join ('|', @array); print "$print_string\n";
    grep
    One dead unjugged rabbit fish later...
      Parens () construct a list which you can assign to an array.

      Nit: parens group a list. The list is already there, but the precedence of = and , are such that without parentheses, only the first element of the list gets assigned.

        such that without parentheses, only the first element of the list gets assigned.

        Since we're being picky about the language, the list being assigned only has one element, so it's odd to refer to its first element. The assignment is the first element of the larger list.

Re: my $var = join('|',@array) not behaving
by ww (Archbishop) on May 09, 2008 at 02:39 UTC

    Update What follows is mostly true (save the striken phrase), but see the answers above. I misread the square brackets as parens (probably because that'swhat I expected... which goes to show, if nothing else, that seeing what one expects rather than what exists is a source of many errors.

    join expects a list.

    From perldoc -f join:

    join EXPR,LIST
    Joins the separate strings of LIST into a single string with fields separated by the value of EXPR, and returns that new string. Example:
      $rec = join(:, login,$passwd,$uid,$gid,$gcos,$home,$shell);

    Your @array is not a list. Rather, in this case (ie, in the way you've written the assignment to $print_string, the list has a single-element -- a reference to @array.

    See References quick reference.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://685583]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2018-10-19 02:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (106 votes). Check out past polls.

    Notices?