Longest prefix matching -answer found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Longest prefix matching -answer found
# 1  
Old 12-12-2009
Longest prefix matching -answer found

Hi Everyone,
Code:
#!/usr/bin/perl

use strict;
use warnings;

my %prefix_to_rate = (
    '93'  => "1.50",
    '6iii'  => "0.22"
);

my ( $shortest, $longest ) =
  ( sort { $a <=> $b } map { length } keys %prefix_to_rate )[ 0, 1 ];

for my $len ( reverse $shortest .. $longest ) {
        print  $prefix_to_rate{'93'};
}

So the output will print 1.50.

Would like to ask if '93' => "1.50", '6iii' => "0.22" are stored in a tabbed txt file 1.txt:
93 1.50
6iii 0.22

how to read this 1.txt into %prefix_to_rat? Thanks

Found solution from KelvinADC:
Code:
my %hash = ();
open(FH,'1.txt') or die "$!";
while(<FH>){
   chomp;
   my @fields = split(/\t/);
   $hash{$fields[0]} = $fields[1];
}

my ( $shortest, $longest ) =
  ( sort { $a <=> $b } map { length } keys %hash )[ 0, 1 ];

for my $len ( reverse $shortest .. $longest ) {
        print  $hash{'93'};
}


Last edited by jimmy_y; 12-12-2009 at 01:16 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to uppercase matching line when string found?

Hello, Could you please help me how to search the string in a file, and when found; change the existing line to uppercase in command line? I tried: ?whichcommand? -A "EXT" fileA | awk '{print tolower($0)}' | tee fileB tr command simply converts entire file to uppercase but this is not what... (4 Replies)
Discussion started by: baris35
4 Replies

2. Shell Programming and Scripting

Parse the longest matching string

Hello experts, I am trying to unscramble a mixed signal into component signals. Let the list of known signals be $ cat tmplist DU DU4016 GFF GFF2010 GFF201019 G2115 G211 DU40 (1 Reply)
Discussion started by: senhia83
1 Replies

3. Shell Programming and Scripting

Extract Uniq prefix from a start and end prefix

Dear All, assume i have a file with content: <Start>6000</Start> <Stop>7599</Stop> the output is: 6000 7000 7100 7200 7300 7400 7599 how should we use any awk, sed, perl can do this task, means to extract the uniq prefixes from the start and stop prefix. Thanks Jimmy (3 Replies)
Discussion started by: jimmy_y
3 Replies

4. UNIX for Dummies Questions & Answers

awk - Print lines if only matching key is found

I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. Thanks a lot. Any help is appreciated. Script I am using: awk 'FNR == NR && ! /^]*$/ {... (9 Replies)
Discussion started by: High-T
9 Replies

5. Shell Programming and Scripting

How to print few lines before and after matching word is found suing grep?

Hi, here are few lines present in the logs. I want to grep on Error and print few lines before and after Error word is found line1 Line2 Line3 Error Line4 Line5 Line6 Line7 I want the output to be Line2 Line3 Error Line5 (1 Reply)
Discussion started by: arghadeep adity
1 Replies

6. Shell Programming and Scripting

Find min.max value if matching columns found using AWK

Input_ File : 2 3 4 5 1 1 0 1 2 1 -1 1 2 1 3 1 3 1 4 1 6 5 6 6 6 6 6 7 6 7 6 8 5 8 6 7 Desired output : 2 3 4 5 -1 1 4 1 6 5 6 8 5 8 6 7 (3 Replies)
Discussion started by: vasanth.vadalur
3 Replies

7. UNIX for Dummies Questions & Answers

Clarification on '1 days ago' in date command [Found answer, posted within]

I know the topic of getting yesterday's date has been covered ad nauseum, but I just want to be clear on something. I recently started using the command date --date='1 days ago' '+%m/%d/%y' to get yesterday's date and it's been working great. I just want to be certain that it is going to... (1 Reply)
Discussion started by: DeCoTwc
1 Replies

8. UNIX for Advanced & Expert Users

in sed ,to get longest word

i want the longest word from the file using sed. can any one help me in this case? (6 Replies)
Discussion started by: lakshmananindia
6 Replies

9. Shell Programming and Scripting

Need to replace a . with / which is having a matching Prefix

Hi Input File: export NAME='AA.BB.CC' export FILE=1.2.3 AA.BB.CC export MAIL= '1.3.3' export char='XX.YY.ZZ' Out File export NAME='AA/BB/CC' export FILE=1.2.3 AA.BB.CC export MAIL= '1.3.3' export char='XX/YY/ZZ' Only the Lines which have export and have alphabets after =... (9 Replies)
Discussion started by: pbsrinivas
9 Replies

10. UNIX for Dummies Questions & Answers

already found the answer

Hi folks, I Got a little question here, When i try to start a telnet session to my unix server, the unix server states the following message: telnetd: All network ports in use. Connection closed I know it has something to do with pseudo tty's? I've tried to search the... (3 Replies)
Discussion started by: Erik Rooijmans
3 Replies
Login or Register to Ask a Question