CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
needed help with the weird scheme of setting commatas and tabs

 



Wildcard
Novice

Jun 18, 2017, 10:23 AM

Post #1 of 3 (1931 views)
needed help with the weird scheme of setting commatas and tabs Can't Post


Code
# Reverse-Operator 

@Anna = 6..10;
@Bob = reverse (@Anna); # 109876

print @Bob ;



$, = "," ;

foreach $, (@Bob) {
}
print @Bob; # same with commatas


#10, 9, 8, 7, 6

$t = " \t " ; #now try tabs

foreach $t (@Anna) {
}
print @Anna; #No prevail

print "\n Anna"; #You gotta be kidding me

How you can see, I have problems setting up the right scheme.
On my first argument I want to show how numbers are just added to eachother. Then with the foreach I implemented the commatas after each iteration. And it succeeded. Now, the problem is, when I try to do that inside the " ", it doesn't work.
But now I can't structure the lines properly, whcih means no \n or at least I dont know how to set this. Help would be much appreciated :D


(This post was edited by Wildcard on Jun 18, 2017, 10:24 AM)


Laurent_R
Veteran / Moderator

Jun 18, 2017, 10:41 PM

Post #2 of 3 (1923 views)
Re: [Wildcard] needed help with the weird scheme of setting commatas and tabs [In reply to] Can't Post

Hi,
I am not sure of what you are trying to do exactly, but this loop:

Code
foreach $, (@Bob) {  
}

isn't doing anything useful.
This code:

Code
$, = "," ;                                                           
print @Bob; # same with commas

would do the same.

Similarly, this:

Code
foreach $t (@Anna) {  
}

is not doing anything useful.

If you want the items of Anna separated by tabs, try this:

Code
$, = "\t"; 
print @anna;



BillKSmith
Veteran

Jun 19, 2017, 6:51 AM

Post #3 of 3 (1917 views)
Re: [Wildcard] needed help with the weird scheme of setting commatas and tabs [In reply to] Can't Post

My use of $, in my answer to your previous question seems to have misled you. $, is an alias for the perl special variable $OUTPUT_FIELD_SEPARATOR. Please read the documentation for this at perlvar.
If you do not understand the document, ask here. Note: This variable does not affect your array, only the way that it is printed.

In your new thread, your first 'print' prints the elements of @Bob with no whitespace or commas because $, is undefined (default). Your second 'print' printed the same elements separated by commas because you changed the value of $, to a comma. Your third 'print' has quotes around the array name. 'Print' considers this a single element (a string). There are no elements to separate so the value of $, makes no difference at all.

There is another special variable $LIST_SEPARATOR ($") Which controls how an array is interpolated into a string.
You can use this variable to put tabs in the string.


Code
$" = "\t"; 
print "\n@Anne\n";



Laurent already point out that neither of your loops do anything useful. It is only because of a bit of weirdness in perl 'for' loops that the first one did no harm. In general, when a loop finishes, the value of the loop variable is undefined. (You were just lucky that $, was still a comma.)

The use of strict and warnings may be optional, but you should ALWAYS use them. Why should gurus help you when you are to lazy to get all the help that perl provides?
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives