Perl Programming with Mapping


 
Thread Tools Search this Thread
Top Forums Programming Perl Programming with Mapping
# 1  
Old 12-04-2009
Perl Programming with Mapping

Code:
 
use strict;
print "Enter last 4 digits of phone number:";
chomp(my $number=<>);
die "Invalid number: '$number'\n" unless $number=~/^\d{4}$/;
my @d=split(undef,$number);
my %map={
2=>"[abc]",
3=>"[def]",
4=>"[ghi]",
5=>"[jkl]",
6=>"[mno]",
7=>"[pqrs]",
8=>"[tuv]",
9=>"[wxyz]",
};
my $r=$map{$d[0]}.$map{$d[1]}.$map{$d[2]}.$map{$d[3]};
open FILE,"</usr/share/dict/words";
my $x=0;
while(<FILE>){
        chomp;
        if($_=~/^$r$/){
                print "$_\n";
        }
        $x++;
}

This program is suppose to construct a regular expression mapping the letters to character classes then search the file for matching words to find what the last four digits of a phone number may spell.

My problem is that it is not giving me any output.

Any help would be greatly appreciated.
# 2  
Old 12-04-2009
Your first error: you didn't use warnings;
Next up, split expect a regular expression as it's first parameter, so if you want to split on the space between characters, use '//' instead of undef.
The regex you're building, by itself, is ok. But you're trying to match it anchored at the beginning and the end of the word, which means it will only ever return 4 character long words. I don't know if that's what you want, but is severely limits available results.

And I'd suggest an additional check / error correction, in case someone inputs a '1' as one of the digits, as that'll throw an unsightly "uninitialized value" error.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating unique mapping from multiple mapping

Hello, I do not know if this is the right title to use. I have a large dictionary database which has the following structure: where a b c d e are in English and p q r s t are in a target language., the two separated by the delimiter =. What I am looking for is a perl script which will take... (5 Replies)
Discussion started by: gimley
5 Replies

2. Shell Programming and Scripting

Perl programming help

I am trying to make a simple perl program that reads 20 characters upstream from the codon ATG in a given sequence. The following is what I have. I just dont know how to make the program read 20 characters upstream from the ATG codon. print "\nThis program will read 20 characters upstream... (1 Reply)
Discussion started by: patiencenpray
1 Replies

3. Shell Programming and Scripting

perl programming

how to link the linux files in perl on the local webpage ???? suppose we have some results and want to get them published on the local webpage of our internal site. how this can be done using HTML and perl together , so that the results are published directly on the webpage. thanks kullu (0 Replies)
Discussion started by: kullu
0 Replies

4. Programming

Help me with perl programming

Hi, i am very beginer to perl, I am reading one xml file and i am creating hash table for that file. i written code like this #!/usr/bin/perl use warnings; use strict; use XML::LibXML::Reader; #Reading XML with a pull parser my $file; open( $file, 'formal.xml'); my $reader =... (8 Replies)
Discussion started by: veerubiji
8 Replies

5. Programming

Programming help - Perl !

I am having a text file with Vivek 50 Ram 34 Hulk 45 Vivek 23 Ram 23 Vivek 55 Now I need a perl script to display the fields of 1st column & the 2nd column with summation (& avoid the duplicates). Vivek 128 Ram 57 hulk 45 Plz help me... (1 Reply)
Discussion started by: gameboy87
1 Replies

6. Shell Programming and Scripting

Perl programming error

Hi, everyone!! i am new to perl programming.. plz help me. #!C:/perl/bin use warnings; use strict; use Text::CSV_XS; my @rows = ""; my $row; my $count; my $fh; my @fields = ""; my $csv = Text::CSV_XS->new ({binary =>1}) or die "cannot use CSV:" .Text::CSV->error_diag (); open... (3 Replies)
Discussion started by: kvth
3 Replies

7. Shell Programming and Scripting

Perl Vs Shell Programming

Can someone please tell me what the big deal about perl is? i have been doing shell programming for quite a number of years and I have to say, there's very little if any thing that I can't do in shell programming. i just need to investigate how to do it. so, my question is, does deep... (1 Reply)
Discussion started by: SkySmart
1 Replies

8. UNIX for Dummies Questions & Answers

Is PERL a programming language?

I need a small and simple clarification... Can someone tell me whether PERL is a programming language or not. Also, can shell scripts also considered as programming language or not. Also, please tell me the exact difference between programming language and scripting. Please help.... (3 Replies)
Discussion started by: Anjan1
3 Replies

9. Shell Programming and Scripting

Perl Programming for Splitting

Hi, I am extracting SQL queries into a file and the file is as follows ********************************************************* select BatchKey ,restartStatus ,batchContextBuffer ,batchPgmId ,StartKey , EndKey ,Mcbatchcontrol_ver from qsecminload.Mcbatchcontrol_t where RefId = :1 ... (5 Replies)
Discussion started by: sagarbsa
5 Replies

10. UNIX for Dummies Questions & Answers

PERL - DB programming

Hi friends, What are the possible ways to connect to DB2 database from Perl (on unix). I need to connect to DB2 and get records for further processing. Can you please suggest the best possible way. I heard about DBI/DBD, if you have some sample scripts please post them too. Thanks in advance. (3 Replies)
Discussion started by: satguyz
3 Replies
Login or Register to Ask a Question