Bob Heise on 7 Oct 2007 05:10:02 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] &!&@$%$%## Perl

  • From: Bob Heise <heise2k@gmail.com>
  • To: Philadelphia Linux User's Group Discussion List <plug@lists.phillylinux.org>
  • Subject: Re: [PLUG] &!&@$%$%## Perl
  • Date: Sun, 7 Oct 2007 07:17:54 +0200
  • Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:from:reply-to:to:subject:date:user-agent:references:in-reply-to:content-type:content-transfer-encoding:content-disposition:message-id; bh=ysCQ+gB34+4RbLZHXMPr6wQcM4FLRlZz5jo/ZguJgMg=; b=HNRHrzTXKGB2oV7ykmZZPjzl9DodaBgJI1kY+W6N7VHbfbrEnofAV7LkrEuei83nefr8Ft5QMabsl2Njf/tTgBPgZZ+wt8lRz5bGAzRVAKKGvN3kAsRtS4zVexKqfa0BhG0vbzPX8YNZf18GtUsAnjv6ErjuH6uo3aDDrHXxoVs=
  • Reply-to: heise2k@gmail.com, Philadelphia Linux User's Group Discussion List <plug@lists.phillylinux.org>
  • Sender: plug-bounces@lists.phillylinux.org
  • User-agent: KMail/1.9.6 (enterprise 20070831.706792)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Den Sunday 29 July 2007 22.53.33 skrev W. Chris Shank:
> I am trying to do a simple Case statement in perl. I have a file with
> key:value pairs. I can parse out each key and the value via a loop, but I
> only need 3 keys, so I'm trying to make a Case statement where if the key
> equals any of 3 criteria the corresponding value is saved to a variable.
> Here is my script so far - but the value is never written to the variable.
> What am i doing wrong? Thanks perl wizards for your help.
>
> #!/usr/bin/perl
>
> while(<>) {
> chomp;
> my ($key,$value) = split(/:/);
>
> use Switch;
> switch ($key) {
> case "displayName" { my $displayName = $value }
> case "sn" { my $sn = $value }
> case "givenName" { my $givenName = $value }
> else { }
> }
>
> }
> print "displayName $displayName \n";
> print "sn $sn \n";
> print "giveName $givenName \n";

Hey Chris,

Eric answered your question; however, like the others I think that switch is 
the wrong way to go. A hash lookup should always be faster than if else 
statements, or the ugly switch filter. Maybe something like this would work 
for you:

#!/usr/bin/perl

my $displayName;
my $sn;
my $givenName;

my %options =   (
                'displayName' => sub{ $displayName = $value },
                'sn' => sub{ $sn = $value },
                'givenName' => sub{ $givenName = $value }
                );

while(<>) { 
	chomp; 
	my ($key,$value) = split(/:/);
	$options{$key}->();
}

print "displayName $displayName \n";
print "sn $sn \n";
print "giveName $givenName \n";


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFHCGwChchU42miuVMRAkpQAJ4rZKLggRW8shP+gbTJkGniWSXWygCg6ez7
MMzLJoDpFPBU/ig2uKrUof4=
=zh18
-----END PGP SIGNATURE-----
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug