Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Re: Re: I'm so confused

by st4k (Novice)
on Nov 24, 2001 at 08:47 UTC ( #127226=note: print w/replies, xml ) Need Help??

in reply to Re: Re: I'm so confused
in thread I'm so confused

Hey the join function works but not in the way I expected it to work.
print join "\n", @out;
works just fine and inserts the newline seperator just like I want it to do while
join "\n", @out;
print @out;
returns the chompd output
the solution I figured out was
@out = join "\n", @out;
print @out;
Is this the correct way to do this? Im trying to be as strict as possible and learn perl without using cheats and workarounds if you know what I mean. If I get the hang of properly structured programming I think it can cary over to whatever language I learn in the future. THANKS AGAIN EVERYONE!

Replies are listed 'Best First'.
Re: Re: Re: Re: I'm so confused
by jlongino (Parson) on Nov 24, 2001 at 12:21 UTC
    st4k, as rchiav stated and as I showed you earlier, you probably don't want to store the newline characters in your array. In my example using print join("\n",@out); I demonstrated a way to manipulate the array (without changing its contents) and printing the results each element on a new line. By storing the data without newlines you could also do something slightly different:
    my @out = qw(one two three four five six seven eight nine); my $i = 0; foreach (@out) { print "\$out[",$i++,"] = '$_'\n"; } =output= $out[0] = 'one' $out[1] = 'two' $out[2] = 'three' $out[3] = 'four' $out[4] = 'five' $out[5] = 'six' $out[6] = 'seven' $out[7] = 'eight' $out[8] = 'nine'
    But if you had stored the newlines in the array, the last ' in each line would've been pushed to the next line by the newline character stored in the array:
    =output= $out[0] = 'one ' $out[1] = 'two ' $out[2] = 'three ' $out[3] = 'four ' $out[4] = 'five ' $out[5] = 'six ' $out[6] = 'seven ' $out[7] = 'eight ' $out[8] = 'nine '
    You have more flexibility if you store only the data.

    In your example @out = join "\n", @out; you are destroying your array. Instead of the output above you would get the following output using the same print loop:

    my @out = qw(one two three four five six seven eight nine); @out = join "\n", @out; my $i = 0; foreach (@out) { print "\$out[",$i++,"] = '$_'\n"; } =output= $out[0] = 'one two three four five six seven eight nine'
    Instead of having 9 rows you now have only one row made up of a string with interspersed newlines. As a rule, most people don't print an array using   print @array; except when debugging.

    Try playing around with the print loop above to test other types of array manipulation. It will give you a better idea of how your data is stored in the array structure.


      Instead of join to add the newlines, you want to use map. Something along the lines of:
      @out = map { "$_\n" } @out;
      What map does is to do the action in {} to every element in the array. :-)

      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://127226]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2021-10-24 00:35 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (88 votes). Check out past polls.