How to separate the values in perl??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to separate the values in perl??
# 1  
Old 05-12-2009
How to separate the values in perl??

Hi,

I have a string like that and i have to separate the key and value pair.

Code:
$str="OWN - NLM STAT- MEDLINE DA - 19990311 DCOM- 19990311 LR - 20061115 AB - The perlfaq comprises several documents that answer the most commonly asked questions about Perl and Perl programming. 
It's divided by topic into nine major sections outlined in this document.
Practical extraction reporting language ";

Here i want to create a hash. I tried like this first by using regular expressions but i am not able to separate the values.


[CODE]

if($str=~/(.*)\s+\-\s+(.*)/)
{
print "$1 and $2";
}
But it is not separating. The output i want like this:

Code:
OWN   NLM 
STAT  MEDLINE
DA      19990311
DCOM  19990311
LR       20061115
AB       The perlfaq comprises several documents that answer the most commonly asked questions about Perl and Perl programming. 
It's divided by topic into nine major sections outlined in this document

I tried splitting 5also but didn't work!!

Any suggesstions??

How can i get the above output???

Regards
Vanitha
# 2  
Old 05-12-2009
hi it works but you need to tweak it a bit ...

Code:
$ > cat b.pl

$str = $ARGV[0] ;
$str =~ s/([\w\d]*\s*\-\s*[\w\d]*)\s*?/$1|/g ;

my @T = split (/\|/,$str) ;

$" = "\n" ;
print "@T\n" ;

$ >

$ > perl b.pl
OWN - NLM
 STAT- MEDLINE
 DA - 19990311
 DCOM- 19990311
 LR - 20061115
 AB - The
 perlfaq comprises several documents that answer the most commonly asked questions about Perl and Perl programming.
It's divided by topic into nine major sections outlined in this document.
Practical extraction reporting language

# 3  
Old 05-13-2009
Quote:
Originally Posted by zedex
hi it works but you need to tweak it a bit ...

Code:
$ > cat b.pl

$str = $ARGV[0] ;
$str =~ s/([\w\d]*\s*\-\s*[\w\d]*)\s*?/$1|/g ;

my @T = split (/\|/,$str) ;

$" = "\n" ;
print "@T\n" ;

$ >

$ > perl b.pl
OWN - NLM
 STAT- MEDLINE
 DA - 19990311
 DCOM- 19990311
 LR - 20061115
 AB - The
 perlfaq comprises several documents that answer the most commonly asked questions about Perl and Perl programming.
It's divided by topic into nine major sections outlined in this document.
Practical extraction reporting language

Hi,

Thanks a lot!!
How to create hash for this so that OWN, STAT, DA & COM are keys and the corresponding entries are values??

Regards
Archana
# 4  
Old 05-13-2009
after getting values in @T
just try the following code ... i did not test it so there might be some errors
Code:
$i = 0 ; 
$j = 1 ; 
%HASH = () ; 

foreach my $k ( @T ) 
{
    my $key = $k[$i] ;
    my $val  = $k[$j] ;

    $HASH{$key} = $val ; 
    $i += 2 ;
    $j += 2 ;
}


Last edited by zedex; 05-13-2009 at 05:17 AM.. Reason: added missing code
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print line is values between two fields in separate file

I am trying to use awk to find all the $3 values in file2 that are between $2 and $3 in file1. If a value in $3 of file2 is between the file1 fields then it is printed along with the $6 value in file1. Both file1 and file2 are tab-delimited as well as the desired output. If there is nothing to... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Separate a hash variable into 2 parts in Perl

Dear Perl users/experts, Could somebody help me how to solve my problem, I have a hash variable that I want to convert into dot file (graphviz). I know how to convert it to dot file but I need some modification on the output of the hash variable before convert it to dot file. Eeach key of... (1 Reply)
Discussion started by: askari
1 Replies

3. Shell Programming and Scripting

Perl :: reading values from Data Dumper reference in Perl

Hi all, I have written a perl code and stored the data into Data structure using Data::Dumper module. But not sure how to retreive the data from the Data::Dumper. Eg. Based on the key value( Here CRYPTO-6-IKMP_MODE_FAILURE I should be able to access the internal hash elements(keys) ... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. Shell Programming and Scripting

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Perl -> Identify Values

Hi, I tried to find out the users under the file /etc/shadow , where the value should be 90, which shouldn't be less or more than that.. if its more or less, it should give me the output as BAD. { print (GOOD); } Else { print (BAD); } (10 Replies)
Discussion started by: gsiva
10 Replies

6. Shell Programming and Scripting

Divide data with specific column values into separate files

hello! i need a little help from you :) ... i need to split a file into separate files depending on two conditions using scripting. The file has no delimiters. The conditions are col 17 = "P" and col 81 = "*", this will go to one output file; col 17 = "R" and col 81 = " ". Here is an example. ... (3 Replies)
Discussion started by: chanclitas
3 Replies

7. Shell Programming and Scripting

Need help with shell, trying to append or separate values in a string

Ok. I for the life of me cant figure out how to do this. I need Help. So here is what I'm trying to do. I have a block of text. They are FIPS codes for counties. Below is the block. There are probably a few ways to do this. The first line starting with ARC021....... this line is a list of... (2 Replies)
Discussion started by: chagan02
2 Replies

8. Shell Programming and Scripting

perl -write values in a file to @array in perl

Hi can anyone suggest me how to write a file containing values,... say 19 20 21 22 .. 40 to an array @array = (19, 20, ... 40) -- Thanks (27 Replies)
Discussion started by: meghana
27 Replies

9. UNIX for Dummies Questions & Answers

to separate values from a string

Hi I would like to take input from user like username/password@connectstring I should be able to cut the username and password and connect string for example if someone enters like sam/sammy@ora1 my program should take sam as username sammy as password and ora1 as connectstring and... (3 Replies)
Discussion started by: ssuresh1999
3 Replies

10. UNIX for Advanced & Expert Users

insert pipe in file to separate values

hi all... i need your help, because i donīt know what to do... i have a flat file like this: B065200512312004123111010000061451 000021853 B065200512312004123111020000621907 000417802 B065200512312004123111030000005214 000005861 B065200512312004123111040000120133 000088448 and i need... (5 Replies)
Discussion started by: DebianJ
5 Replies
Login or Register to Ask a Question