2 Replies - 1084 Views - Last Post: 26 November 2010 - 08:19 PM

#1 lucky04  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-September 07

HELP! Using finger/soundex in perl

Posted 15 October 2010 - 12:30 PM

My code still runs, but i'm getting:
"substr outside of string at ./prog9.pl line 148"
"Use of uninitalized value $bar in substitution (s///) at ./prog9.pl line 150.

Not sure where its choking.

The purpose of hte program was to use a soundex program to search for command line entered lastname and finger the requested information.


#!/usr/bin/perl

use strict;
use warnings;
use feature ":5.10";
#use diagnostics;

my $num = 0;
if (@ARGV != 1)
{
	print "\nUsage: $0 LastName\n\n";
	exit(1);
}
chomp $ARGV[0];

####################################################################################
open (IN,"/etc/passwd");
my @wholeFile = <IN>;
close IN;

#print "Here is the file /etc/passwd file with blank lines:\n\n";

#foreach my $line (@wholeFile)
#{
#	print $line, "\n";
#}

#print "Press ENTER to continue and use the functions to grab info:\n\n";
#<>;

foreach my $line (@wholeFile)
{
	my $userid = &get_user_id($line);
	my $lastname = &get_last_name($line);
	#print "$userid has a last name of $lastname!\n";

	if(soundex($lastname) eq soundex($ARGV[0]))
	{
		my @args = ("finger", $userid);
		system (@args);
		$num++;
	}

}

say "\nThe name you were looking for, ".$ARGV[0]." converted to ".soundex($ARGV[0])."\n";

if ($num == 0)
	{
		say "finger: ".$ARGV[0].": no such user.\n\n";
	}

sub get_user_id
{
	my @userid = split(/://>,shift);
	return $userid[0];
}

sub get_last_name
{
	my @lastname = split (/://>,shift);
	if ($lastname[4] ne "" || $lastname[4] ne " ")
	{
		$lastname[4] =~ s/,.*?//g;
		$lastname[4] =~ /(\S+)/g;	
		return $1;
	}
	else
	{
		say "No last name provided\n";
	}
}
####################################################################################
#ASSIGNMENT EXAMPLE CODE:
#
#my %sndx = ("Euler" => "E4600", "Gauss" => "G2000", "Hilbert" => "H4163", "Knuth" => "K5300", "Lloyd" => "L3000", "Lukasieicz" => "L2220");
#
#print "Processing known names:\n\n";
#my @names = sort {$a cmp $b} keys %sndx;
#
#foreach my $nm (@names)
#{
#	my $findSdx = &soundex($nm);
#	print "The soundex for $nm should be $sndx{$nm} and is $findSdx\n";
#	if ($findSdx ne $sndx{$nm})
#	{
#		print "\tHowever, that is incorrect!\n\n";
#	}
#}
#
###############################################################################
sub soundex
{

	my $temp = uc shift;

	my $sdxValue = "*** $temp not processed ***";
	
	my $ltr = substr($temp, 0, 1);

	
	#soundex work:  Spent an hour trying to convert my C++ code into 
	#perl code, and it didnt work - at all =(.
	#FAILCODE#############################################################
	#$temp =~ tr/BFPV/1111/;
	#$temp =~ tr/CGJKQSXZ/22222222/;
	#$temp =~ tr/DT/33/;
	#$temp =~ tr/L/4/;
	#$temp =~ tr/MN/55/;
	#$temp =~ tr/R/6/;
	#$temp =~ tr/AEHIOUWY/00000000/;
	######################################################################
	
	$temp =~ s/[bfpv]/1/gi;
	$temp =~ s/[cgjkqsxz]/2/gi;
	$temp =~ s/[dt]/3/gi;
	$temp =~ s/[l]/4/gi;
	$temp =~ s/[mn]/5/gi;
	$temp =~ s/[r]/6/gi;
	$temp =~ s/[aehiouwy]/0/gi;
	my $foo = $temp;
	
	$foo =~ s/6+/6/g;
	$foo =~ s/5+/5/g;
	$foo =~ s/4+/4/g;
	$foo =~ s/3+/3/g;
	$foo =~ s/2+/2/g;
	$foo =~ s/1+/1/g;
	$foo =~ s/0+/0/g;
	
	my $bar = substr($foo,1,4);	

	$bar =~ s/[0\-']//g;
	$bar .= "0000";
	$bar = substr($bar,0,4);	


	$sdxValue = $ltr.$bar;
	return $sdxValue;

}

###############################################################################



This post has been edited by lucky04: 15 October 2010 - 12:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: HELP! Using finger/soundex in perl

#2 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: HELP! Using finger/soundex in perl

Posted 17 October 2010 - 03:09 AM

My primary suggestion would be to get Text::Soundex from CPAN and use that instead of writing (and having to debug...) your own implementation. The first time I used Text::Soundex it took me about five minutes to set up and figure out (it has a very simple interface), which is much less than the hour you spent on your "FAILCODE" section even without trying to fix your current problem.

As for the cause of the warnings you're getting, it would appear that, for some reason, $foo is empty. Have you verified that your soundex routine is receiving the correct input and that $foo looks like you expect prior to setting "my $bar = substr($foo,1,4)"?
Was This Post Helpful? 1
  • +
  • -

#3 chorny_cpan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 52
  • Joined: 13-May 09

Re: HELP! Using finger/soundex in perl

Posted 26 November 2010 - 08:19 PM

Text::Soundex is a core module, so there is no need to download it from CPAN.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1