Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to match a specific element from an array? 2

Status
Not open for further replies.

Cybertronic

Technical User
May 18, 2006
19
I'm using the following code for checkboxes in my HTML form:

my @color = param('color');
foreach my $color (@color) {
print "You picked $color.<br>\n";
}

At the moment I'm using this code to match an element from my array:

if ($color[0] eq "name of color")

The problem is, is there an alternative to $color[0]? If I use [0], that's only for the very first checkbox as it won't be ticked all the time (there's other checkboxes). I've tried using:

if ($color eq "name of color")

Sadly it doesn't work :(

Please help! :)

 
Welcome to tek-tips!

Here is one way to do it:
Code:
if ( grep { $_ eq 'name of color'} @color ) {
   print "You picked $_.<br>\n";
    }

X
 
I should work;

Code:
my $color = 'red';
my @color = param('color');
foreach my $colors (@color) {
   print "You picked $colors" if $color eq $colors;
}
 
Xaqte, thanks for the welcome and for the help! :)

I've got it working now thanks! :)

Is it possible to get a value from an if statement that is true (like the one Xaqte posted) and stick it in another array which calculates the total?

Here's a better explanation:

Code:
if ( grep { $_ eq 'green'} @color ) {
   $greenprice=185.99;
    }

if ( grep { $_ eq 'red'} @color ) {
   $redprice=85.99;
    }

if ( grep { $_ eq 'yellow'} @color ) {
   $yellowprice=165.99;
    }

if ( grep { $_ eq 'blue'} @color ) {
   $blueprice=125.99;
    }

What I'm after is that if the user has ticked 2 of the checkboxes, it'll only add the selected values. How can I do this? Any ideas are welcome! :)
 
KevinADC, thanks for your post, I was trying to fix my if statement, sorry!

Regarding my previous post, for the selected values, what I mean why add the selected values is that it adds the values together to calculate a total.

How can I edit my posts on these forums? :S
 
Cybertronic,

Those if statements should work, post back if they don't.

I don't know how new you are to perl, but be sure to always:

Code:
use strict;
use warnings;

If you are unsure of what I mean, just ask!

How can I edit my posts on these forums?

You can't. This is why I always make heavy use of the "Preview Post" button.

Thanks for the star!

X
 
Xaqte, they are indeed working :)

I'm new to Perl unfortunately :(

Does the grep function block the out elements in the array in order to match the specific element?

Is it possible for you to help me with my previous post please?

You deserved the star! :)
 
your if statements will work but looks like too much work, you are going through the entire array in each if (grep) condition, one loop should be plenty. Use a hash to store the prices:

Code:
%prices = (
   green => 185.99, 
   red => 85.99,
   yellow => 165.99,
   blue => 125.99
);

for ( @color ) {
   print "The price is $prices{$_}\n";
}
 
KevinADC,

I'm having a problem with the $_ parameter as it doesn't seem to be showing anything :S

Is there an alternative to this $_?

Please let me know :)
 
I assumed the values you are checking are the exact spelling and case as the hash keys:

red
blue
yellow
green

if they aren't then you have to change the hash keys to reflect the exact values of the values you have in @color, do this as a sanity check:

Code:
%prices = (
   green => 185.99,
   red => 85.99,
   yellow => 165.99,
   blue => 125.99
);

for ( @color ) {
   print "\$ = $_\n";
   print "The price is $prices{$_}\n";
}
 
THanks KevinADC :)

It was my mistake, I change the values by mistake!

Is it possible to add the values (ones that have been selected) together as a total?

On a related subject:

Code:
if (@address_line_2{'address_line_2'} eq "")
{
  print "\n";	
}
else
{
  print "              @address_line_2\n";
  print "              <br/>\n";
}

Is there anything wrong with my if statement? It doesn't seem to work properly as it's not printing anything if I type something in the address line 2 field :S
 
Oops!

Code:
if ($address_line_2{'address_line_2'} eq "")
{
  print "\n";    
}
else
{
  print "              $address_line_2\n";
  print "              <br/>\n";
}

I typed @ instead of $, sorry!
 
You have $string_address_2 as a regular scalar variable and you use the same name for a hash table? That is OK as far as perl is concerned, but it is confusing to see code using the sane name for different types of data.

As far as adding goes you use + to add:

Code:
%prices = (
   green => 185.99,
   red => 85.99,
   yellow => 165.99,
   blue => 125.99
);
$total_price = 0;
for ( @color ) {
   print "\$ = $_\n";
   print "The price is $prices{$_}\n";
   $total_price+=$prices{$_}; 
}
print $total_prices;

 
Thank you so much KevinADC! :)

Could you please explain what the $_ parameter does?
 
$_ is just perls default scalar variable. If you don't explicitly assign a scalar variable perl assumes $_ for many things.

It's pretty much the same as:

Code:
foreach $line (@array) {
   print $line;
}

it can be written shorter as:

Code:
for (@array) {
   print $_;
}

or shorter yet:

Code:
for (@array) {
   print;
}

or even shorter:

Code:
print for @array;

they all do the same thing.
 
KevinADC,

That last post was great... very enlightening!

You deserve a star for that, at the least... looks like I've missed out on the fun [wink]!
 
Thanks for the explanation KevinADC!
smiletiniest.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top