Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Help with @LoL

by zzspectrez (Hermit)
on Nov 26, 2000 at 11:26 UTC ( [id://43357]=perlquestion: print w/replies, xml ) Need Help??

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

Ok.. Im pulling my hair out and it hurts.. Im going to stop.. I don't what to get premature baldness. Can someone enlighten me on some code..

Im writing some code that involves a couple List of a List. I noticed that when I was printing out one of the lists, I was getting spaces between the elements within the list items. So to figure out why, I wrote the following code to find what was causing the spaces. I made subroutines to simulate the different kind of ways I was using list data. It appears the spaces where caused by the interpolation of the variable in one of my print statements but I dont understand why. All the cases do what I was expecting except the last print of data from SUB4.

Why is SUB4: 2nd way being printed differently then SUB4??

#!/usr/bin/perl -w use strict; sub my_sub () { return ("1", "2", "3"); } sub my_sub2 () { my @data = (); for my $x (1..3){ push @data, ["1","2","3"]; } return @data; } sub my_sub3 () { my $test_str = "1::2::3\n"; (my @data) = $test_str =~ /^(\d{1})::(\d{1})::(\d{1})/; return @data; } sub my_sub4 () { my @data = (); while (<main::DATA>){ chomp; push @data, [split /::/]; } return @data; } print "SUB1:\n"; print my_sub(),"\n\n"; print "SUB2:\n"; for my $x (my_sub2()){ print @$x,"\n"; } print "\n"; print "SUB3: \n"; print my_sub3,"\n\n"; my @data = my_sub4(); print "SUB4: \n"; for my $x (@data){ print @$x,"\n"; } print "\n"; print "SUB4: 2nd way\n"; for my $x (@data){ print "@$x\n\n"; } __END__ 1::2::3 1::2::3 1::2::3

Here is the output:

SUB1: 123 SUB2: 123 123 123 SUB3: 123 SUB4: 123 123 123 SUB4: 2nd way 1 2 3 1 2 3 1 2 3

Thanks for any help!
zzSPECTREz

Replies are listed 'Best First'.
Re: Help with @LoL
by jlp (Friar) on Nov 26, 2000 at 11:35 UTC
    You are interpolating an array into a string; when you do this, you get a string with the elements of the array separated by the value of the $" variable, by default a single space. e.g. -
    my @array = qw(foo bar baz); my $string = "@array"; print "This is an interpolated array: $string";
    Will yield: This is an interpolated array: foo bar baz
    Hope this helps!

      Thanks!! Thats the answer. I havent run across $" special variable before. So in other words, the following code gives me the response I was expecting.

      $" = ''; print "SUB4: 3rd way\n"; for my $x (@data){ print "@$x\n"; }

      Prints the following.

      SUB4: 3rd way 123 123 123

      zzSPECTREz

        First of all zzspectrez , let me say that this is an excellent example of what every post should look like. Sample code, input, output and good description. ++ for that alone.

        Second of all, regarding changing the value of $". Messing with any special variable when you are not obfuscating and have a perfectly good other way to do it is a bad idea. Sometihng like assigning a new value to one of these beasties can have really strange unpredictable effects as you expand your code and, should you miss you did it somewhere else or should you call a sub that was altered for one purpose by changing one of these, you will quickly get yourself really confused. Fastolfe suggests looking into join above and I have to urge you to do the same. That would look sometihng like :

        print "SUB4: 2nd way\n"; for my $x (@data){ print join "", @$x, "\n"; }
        If you feel you must change $" then try and use local on your change so that it is really constricted to the one place you need the change to be in effect.
        <myExperience> $mostLanguages = 'Designed for engineers by engineers.'; $perl = 'Designed for people who speak by a linguist.'; </myExperience>
        You may also be interested in the join function, which is specifically for bringing together different array elements in a single string. You won't have to mess with special variables.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found