Avatar of pete420
pete420

asked on 

String Pattern

hi,

############
FORM PROGRAM
############
#!c:/perl/bin/perl
use CGI ':standard';
print header;
@menu = ('USA', 'China', 'Canada', 'Mexico' );
print '<FORM
         ACTION="http://localhost/cgi-bin/fig-5-4-prog.cgi"  
         METHOD="POST">';
print '<FONT SIZE=4 > What countries have you visited?';
print br;
print '<INPUT TYPE="checkbox" NAME="places" VALUE="0"checked>', "$menu[0]";
print br;
print '<INPUT TYPE="checkbox" NAME="places" VALUE="1">',  "$menu[1]";
print br;
print '<INPUT TYPE="checkbox" NAME="places" VALUE="2"checked>', "$menu[2]";
print br;
print '<INPUT TYPE="checkbox" NAME="places" VALUE="3"checked>', "$menu[3]";
print br;
print br, '<INPUT TYPE=SUBMIT VALUE="Submit">';
print '<INPUT TYPE=RESET></FORM>', end_html;



##############
RECEIVING PROGRAM
##############
#!c:/perl/bin/perl
use CGI ':standard';
print header, start_html("Got Your Countries");

@menu = ('USA', 'China', 'Canada', 'Mexico' );
@response =param('places');

if (@response =~ /[023]/)
{
      print "You have travelled to: <b>@menu[@response]</b><br>";
      print "Why not try travelling to <b>Asia?</b>";
}
elsif (@response =~ /[1]/)
{
      print "You have travelled to: <b>@menu[@response]</b><br>";
      print "Why not try travelling to <b>North America<b>";
}
elsif (@response =~ /[0123]/)#Blank screen
{
      print "You have travelled to: <b>@menu[@response]</b><br>";
      print "It would be recomended that you become a  <b>frequent flyer<b> with the company";
}

elsif (@response =~ /[0]/)
{
      print "You have travelled to: <b>@menu[@response]</b><br>";
      print "You have not travelled to <b>Canada, Mexico or Asia<b>";
}

elsif (@response =~ /[2]/)
{
      print "You have travelled to: <b>@menu[@response]</b><br>";
      print "You have not travelled to <b>US, Mexico or Asia<b>";
}

elsif (@response =~ /[3]/)
{
      print "You have travelled to: <b>@menu[@response]</b><br>";
      print "You have not travelled to <b>US, Canada, or Asia<b>";
}
print "</FONT>", end_html;





The problem is that it does not match my IF's exclusively.
EG. If i just select 0 - North America
it should say you have travveled to us but u have not travelled to canada, mexico or asia

if u run my program u will see the problem as it is hard to explain.

thanks

pete
Scripting Languages

Avatar of undefined
Last Comment
ozo
ASKER CERTIFIED SOLUTION
Avatar of manav_mathur
manav_mathur

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of pete420
pete420

ASKER

thanks for the reply. however im kind of looking for a way to fix mine rather than a different approach.

in mine the wrong answers r output,
the places variable passes through the correct value from the form and it is entered in respose variable.
then it should be a simple case of pattern matching variable response to see what msg to output.
ie. if response says 023, then output why not travel to asia.

i do not understand why my program is picking the wrong statement,

thanks pete
Avatar of manav_mathur
manav_mathur

[023] is a character class. That means [023] will match with a string which has only *one* character out of 0,2,3.

[023]* will match strings composed of any number of 0s 2s or 3s.
[023]? will match 0 or 1 0s 2s or 3s.

Hecent had my morning coffee yet and the only solution that comes to my mind is to process each array value seperately. There might be a more elegant solution.  Hope this helps(and you can always use the solution I posted as a last resort.)

happy PERLing...

Manav
SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of ozo
ozo
Flag of United States of America image

$hash{$_}=1 foreach @response ;
for ($i = 0; $i < 4; $i++) {
if (!defined($hash{$i})) {
push @diff, $i ;
}
}
could be
@hash{@response}=();
@diff=grep!exists$hash{$_},0..3;
Avatar of manav_mathur
manav_mathur

>@hash{@response}=();

Didnt get it. @hash??

Manav
Avatar of manav_mathur
manav_mathur

What I think is

for ($i = 0; $i < 4; $i++) {
if (!defined($hash{$i})) {
push @diff, $i ;
}

can be replaced with
@diff=grep {!exists $hash{$}} 0..3 ;

Manav


Avatar of ozo
ozo
Flag of United States of America image

perldoc perldata
       Entire arrays (and slices of arrays and hashes) are denoted by '@',
       which works much like the word "these" or "those" does in English, in
       that it indicates multiple values are expected.

           @days               # ($days[0], $days[1],... $days[n])
           @days[3,4,5]        # same as ($days[3],$days[4],$days[5])
           @days{'a','c'}      # same as ($days{'a'},$days{'c'})


To be strictly equivalent to your foreach loop, it would have to be
@hash{@response}=(1)x@response;


defined implies exists, but exists does not imply defined.
Avatar of manav_mathur
manav_mathur

still didnt get it :/

why are you doing this
@hash{@response}=();

Manav
Avatar of manav_mathur
manav_mathur

Is it that

@hash{@response} = () ;
In %hash, values corresponding to keys in @response will be 'undefined' but will still 'exist' whereas value for other keys will not 'exist' altogether?? but in this case, aren't we losing information. instead of 'undef'ing @hash{@response}, we could simply check for defined on the original hash??

Another question :
@hash{@response}=(1)x@response;

(1)x@response means "number of elements in @response' times 1.
i,e if @response=[2,4]
then
@hash{@response}=(1)x@response;
is equivalent to
($hash{2},$hash[4])=(1,1)

Manav
Avatar of ozo
ozo
Flag of United States of America image

> we could simply check for defined on the original hash??
The hash is originally empty

> ($hash{2},$hash[4])=(1,1)
Changing [4] to {4}, yes.
Avatar of manav_mathur
manav_mathur

ok,
so by
@hash{@response} = ()

you are only making the required keys 'exist', but 'undef'ined??

the [4] was a typo

Manav
Avatar of manav_mathur
manav_mathur

required keys

should be
required values

Manav
Avatar of ozo
ozo
Flag of United States of America image

Yes.

@hash{@response}=(1)x@response;
or
$hash{$_}=1 foreach @response ;

makes the values exist, defined, and true, so you could then use
@diff=grep {!$hash{$_}} 0..3 ;
Scripting Languages
Scripting Languages

A scripting language is a programming language that supports scripts, programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted (rather than compiled). Primitives are usually the elementary tasks or API calls, and the language allows them to be combined into more complex programs. Environments that can be automated through scripting include software applications, web pages within a web browser, the shells of operating systems (OS), embedded systems, as well as numerous games. A scripting language can be viewed as a domain-specific language for a particular environment; in the case of scripting an application, this is also known as an extension language.

30K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo