PDA

View Full Version : What way to do this without 'barewords'


bazz
08-19-2007, 06:19 PM
Hi,

I have just found this in one of my old scripts and whilst it works, that script doesn't use the 'strict' pragma. :smacks wrist:

I am now using strict and so it throws it out for using barewords. I need to your help to find the correct way.

This is what I have from the old script.


foreach (A..Z) {
print "<a href=\"$script_url?businessType=$businessType&amp;alpha=$_\"> $_ </a>";
$_ eq "Z" ? print "</dd>" : print "| |";
}



I suppose this is one way of doing it correctly but, it looks to me awfully verbose or amateur.



my @lettersAndNumbers = (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0);

foreach (@letterAndNumbers) {
print qq(
<a href="$script_url?alpha=$_"> $_ </a>"
);
}



Whats way would you suggest?
I hope it is clear from the first code that I am trying simply, to show the letters of the alphabet and numbers thru 0-9, as hyperllinks. This is so that the user can choose the start letter of a business name and the script can then call in from a MySQL table, those business names beginning with that letter/number.

Any pointers please?

bazz

nkrgupta
08-19-2007, 07:02 PM
foreach my $alpha('A'..'Z') {
print "<a href=\"$script_url?businessType=$businessType&amp;alpha=$alpha\"> $alpha </a>";
$alpha eq "Z" ? print "</dd>" : print "| |";
}

This should work.

FishMonger
08-19-2007, 08:05 PM
This is only my personal style preference, but I'd write it like this:

foreach my $letter ('A'..'Z') {
print $cgi->a({href=>"$script_url?businessType=$businessType&alpha=$letter"}, " $letter ");
print $letter eq 'Z' ? "</dd>\n" : "| |\n";
}

KevinADC
08-20-2007, 05:16 AM
or using your orignal code:

foreach ('A'..'Z') {
print "<a href=\"$script_url?businessType=$businessType&amp;alpha=$_\"> $_ </a>";
$_ eq "Z" ? print "</dd>" : print "| |";
}

you just need those quotes around the beginning and ending range characters.

bazz
08-20-2007, 11:34 AM
Great guys.

I tried the '' but only around the whole A..Z string and not, individually. :stupid:

@FishMonger: you surprised me. :) whay I was expecting you to use the built-in variable '$_' yet you used $letter. Maybe you are checking to see if we just use your answers or if we try to understand them? ;)

@Kevin and nkrgupta:

I shall use it as Kevin showed i.e., with the $_ variable but in future will try to use the cgi module. it seems to have a lot of features that I cold use but so far, haven't.


bazz

bazz

bazz
08-20-2007, 04:25 PM
oh blimey, I should have thought about numbers too.

using this

foreach ('A'..'Z') {
print "<a href=\"$script_url?businessType=$businessType&amp;alpha=$_\"> $_ </a>";
$_ eq "Z" ? print "</dd>" : print "| |";
}


I want to be able to use numbers 0-9 as well. using two separate foreach loops for now; I shall continue to try to figure it out but your help would still be welcome.

bazz

ralph l mayo
08-20-2007, 05:24 PM
I want to be able to use numbers 0-9 as well. using two separate foreach loops for now;
Using foreach ('A'..'Z', 0..9) might work.

FishMonger
08-20-2007, 06:03 PM
So, you want to generate 260 links (A0 A1...Z8 Z9) ? That's going to be a cluttered mess.

Using 2 (nested) loops will show you why I chose to use $letter instead of $_.

Here's a simple example that demonstrates the issue.
foreach ( qw(apple pear grape) ) {
foreach ( qw(1 2 3) ) {
print "Number:$_ \t Fruit:$_\n"
}
}
foreach my $fruit ( qw(apple pear grape) ) {
foreach my $number ( qw(1 2 3) ) {
print "Number:$number \t Fruit:$fruit\n"
}
}

KevinADC
08-20-2007, 06:08 PM
Yes, it should work, I would just add the quotes for consistency sake in this case but it will work without them:

foreach ('A'..'Z' , '0' .. '9'){
print "<a href=\"$script_url?businessType=$businessType&amp;alpha=$_\"> $_ </a>";
$_ eq "Z" ? print "</dd>" : print "| |";
}

KevinADC
08-20-2007, 06:10 PM
So, you want to generate 260 links (A0 A1...Z8 Z9) ?

That's not what I thought he wanted but you could be right.

bazz
08-20-2007, 07:31 PM
No - no I don't want the cluttered mess thing. I can achieve that all on my own thanks :)

I want to have a single line of A, B, C, D thru to 7, 8, 9, 0. Then when the user chooses a letter or number, the query in the next script shows only the Groups beginning with that letter.

@FishMonger: thanks for the explanation. I just was curious since most of the time (I think), you use the built in vars and this time it seemed a very stark contrast.

@ralph and Kevin:

yep that works. I had tried all sorts (like, [] {} () and various combinations), but the comma wasn't one of them :(

bazz

FishMonger
08-20-2007, 07:38 PM
@FishMonger: thanks for the explanation. I just was curious since most of the time (I think), you use the built in vars and this time it seemed a very stark contrast.

bazz

You're right, I am making some changes to how I code. I'm trying to stay more inline with Perl's Best Practices instead of the brevity I've shown in the past.