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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to test a non numeric variable

Status
Not open for further replies.

bobk5444

Programmer
Jan 28, 2005
17
US
hello, i've checked many sites for examples and am amazed that i can't find a simple example of an if statement to test for an alphanumber value ie: if($key == "a" )

to run this pgm: perl del3.pl delin

delin data is:
a=1
b=2
c=3

thanks for any pointers
ps: java programmer trying to learn PERL!

#!/usr/bin/perl -w
die "Usage: scanmegadata <data file> <site name>\n"
unless @ARGV == 1;

my $megalithFile = $ARGV[0];

open MEGADATA, "<$megalithFile"
or die "Can't open $megalithFile: $!\n";

my ( $key, $value );

my %hash = ();

while ( <MEGADATA> ) {
chop;
( $key, $value ) = split( /=/, $_ );

$hash{$key} = $value;
}

while ( my ($key, $value) = each(%hash) ) {
if($key == "a" )
{
#print "$key => $value\n";
}
}

for my $key ( keys %hash ) {
my $value = $hash{$key};

if($value==1)
{
print "$key => $value\n";
}
}

close MEGADATA;

exit 9;
 
Welcome to Perl - You'll love it once you are on your feet.
Perl uses a different set of rules from a basic program language. If statements are still used but the more powerful match and substitution operators are the tool to use for finding something.
Do searches for regex and pattern matching in a perl manual and make sure you have plenty of coffee to hand. Get a grip of the basics first as a full blown matching pattern frightens even the best Perl coder.
This is also a good forum where everyone is treated with respect. I have coded it for only a few years but the more experienced on the site collectively know everything. I have tested them on numerous occasions and they haven't failed yet.
Thanks Guys!

Keith
 
Although what travs69 said is absolutely correct, you don't need this loop:
Code:
[olive][b]while[/b][/olive] [red]([/red] [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [red]([/red][blue]$key[/blue],   [blue]$value[/blue][red])[/red] = [url=http://perldoc.perl.org/functions/each.html][black][b]each[/b][/black][/url][red]([/red][blue]%hash[/blue][red])[/red] [red])[/red] [red]{[/red]
    [olive][b]if[/b][/olive][red]([/red][blue]$key[/blue] == [red]"[/red][purple]a[/purple][red]"[/red] [red])[/red]
    [red]{[/red]
       [gray][i]# .. do whatever .. [/i][/gray]
    [red]}[/red]
[red]}[/red]

You just need to :
Code:
[olive][b]if[/b][/olive] [url=http://perldoc.perl.org/functions/exists.html][black][b]exists[/b][/black][/url][red]([/red][blue]$hash[/blue][red]{[/red][red]"[/red][purple]a[/purple][red]"[/red][red]}[/red][red])[/red] [red]{[/red]
   [gray][i]# .. do whatever .. [/i][/gray]
[red]}[/red]
 
thanks travs!!

going to do some homework now, thanks for the reference!

how a nice weekend+

BobK
 
looks good bigmar, thanks, going to try that!

good weekend!

BobK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top