On 21/03/2012 16:53, lina wrote:
Hi,

For the following keys, I wish the values to be the abbreviation of them,

Nat. Neurosci.
Expert Reviews in Molecular Medicine.
Intermolecular Forces
Nature Cell Biol.
CNS Neurol Disord Drug Targets
Nat. Rev. Mol. Cell Biol.

I came up one, not working though,


#!/usr/bin/env perl

use strict;
use warnings;
use autodie qw(open close);
use 5.012;


open my $fh, '<', "try.txt";

my %abbrev;
my @abbre;
while(my $line =<$fh>){
        if (/([[:upper:]]).*([A-Z]).*?([A-Z])+?/){
                print $1,$2;
                print $3,"\n" if defined $3  ## Here is a mess. I don't know 
how to handle it
                $abbre[]=join '',$1,$2,$3;
                $abbrev{$line} = $abbre;
        }
}

Hi Lina

I think the program below does what you need.

Hope it helps,

Rob


use strict;
use warnings;

use autodie qw(open close);
use 5.012;

my %abbrev;

open my $fh, '<', 'try.txt';

while(my $line = <$fh>) {
  chomp $line;
  my $abbr = join '', $line =~ /[A-Z]/g;
  $abbrev{$abbr} = $line;
}

use Data::Dumper;
print Data::Dumper->Dump([\%abbrev], ['*abbrev']);

**OUTPUT**

%abbrev = (
            'CNSNDDT' => 'CNS Neurol Disord Drug Targets',
            'NCB' => 'Nature Cell Biol.',
            'IF' => 'Intermolecular Forces',
            'NRMCB' => 'Nat. Rev. Mol. Cell Biol.',
            'NN' => 'Nat. Neurosci.',
            'ERMM' => 'Expert Reviews in Molecular Medicine.'
          );

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to