perl script - data munging


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl script - data munging
# 1  
Old 02-22-2009
perl script - data munging

Hi guys
I'm trying to print the keys and values of data by using hashes and so on
I was able to retrieve the key but not values. I'm getting result like this
wrong -output
chr1, 1
HASH(0x800d80)-> {arrayid} chr2, 1
HASH(0x800c9c)-> {arrayid} chr3, 1
HASH(0x80177c)-> {arrayid} chr4, 1
HASH(0x801a94)-> {arrayid}

INPUT
arrayids.....chromes
AF0980 chr1
BC0990 chr2
S090TT chr3
VF456T chr4

wanted output
chr1.....AF.....
chr2.....BC... and so on

Perl script I'm using
#!/usr/bin/perl -w
$infile1 = 'chr1.txt';
$outfile4 = 'out4.txt';
open IN3, "< $infile1" or die "Can't open $infile1 : $!";
open OUT4, "> $outfile4" or die "Can't open $outfile4 : $!";
my %chromes;
while (<IN3>) {
chomp;
my ($arrayid, $chrom) = split /\t/;
my $rec = {arrayid => $arrayid};
push @ {$chromes{$chrom}}, $rec;
}
foreach my $chrom (sort keys %chromes) {
my $count = scalar @{$chromes {$chrom} };
print OUT4 "$chrom, $count \n";
print OUT4 map { "$_-> {arrayid} "} @ {$chromes {$chrom} };
}
close IN3;
close OUT4;
# 2  
Old 02-22-2009
The main problem is your sloppy coding. Mainly this line:

print OUT4 map { "$_-> {arrayid} "} @ {$chromes {$chrom} };

specifically:

"$_-> {arrayid} "


the space between '->' and '{arrayid}' is killing the interpolation because you have it wrapped up in double-quotes. Change it to:

{"$_->{arrayid} "}

I have no idea where you learned to write code with the spaces in odd places:

@ {$chromes {$chrom} };

but you need to stop that:

@{$chromes{$chrom}};

as you can see it will bite you in the butt occasionaly. No spaces between data type symbols, the arrow operator, and brackets. In the above line you can write it likes this if you want less visual clutter:

@{ $chromes{$chrom} };

and when there are no quotes you can get away with stuff like this:

$_-> {arrayid}

but not inside of quotes because a space is a space inside of a quoted string, its not empty whitespace like it is when not quoted.
# 3  
Old 02-22-2009
hey thanx for your suggestions
I do agree what u said. it's working now
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. Shell Programming and Scripting

Perl -- Script to re-format data

Hi, I have a file with data in the following format BOX -1.000000 -1.000000 0.000000 30.00000 14.00000 0.1000000 0.000000 0.000000 0.000000 0.000000 0.000000 CYLINDER 3.595000 2.995000 0.000000 0.5100000 2.000000 Z 0.000000 0.000000 0.000000 I want to convert these files... (1 Reply)
Discussion started by: lost.identity
1 Replies

3. Shell Programming and Scripting

Need a perl script to read and write the data

Hi, I have on Designdocument in that information is stored with in tabular format.I need Perlscript to read and write the datausing perl script? Regards, Ravi (0 Replies)
Discussion started by: toravi.pentaho
0 Replies

4. Shell Programming and Scripting

Perl script required for processing the data

I have following result.log file (always has 2 lines) which I need to process, cat result.log name.cmd.method,"result","abc","xyz"; name="hello,mine.12345,"&"tree"&" xyz "&" tree "&" xyz", data="way,"&" 1"&"rate-me"&"1"&"rate-me",str="",ret=""; now I need to extract the strings/data as... (4 Replies)
Discussion started by: perlDiva
4 Replies

5. Shell Programming and Scripting

Perl script to load data by calling sqlldr

Hello all, I know this is Unix forum, but i also know that there are some experts here who can help me out with this situation; I am loading a data file into oracle table using Perl script by calling sqlldr script. It does not do anything, and no data is getting loaded. Any help,... (2 Replies)
Discussion started by: msrahman
2 Replies

6. Shell Programming and Scripting

how to print out data from mysql table in perl script

I'm having trouble with this code. if i do .\read.pl -u user it prints out 2010-12-20 12:00:00 host1 <cmd>a 2010-12-20 12:00:01 host1 <cmd> <execute> 2010-12-20 12:00:02 host1 <cmd>b 2010-12-20 12:00:03 host1 <cmd>c however, if i enter .\read.pl -h host1 it should... (3 Replies)
Discussion started by: kpddong
3 Replies

7. Shell Programming and Scripting

Perl script error to split huge data one by one.

Below is my perl script: #!/usr/bin/perl open(FILE,"$ARGV") or die "$!"; @DATA = <FILE>; close FILE; $join = join("",@DATA); @array = split( ">",$join); for($i=0;$i<=scalar(@array);$i++){ system ("/home/bin/./program_name_count_length MULTI_sequence_DATA_FILE -d... (5 Replies)
Discussion started by: patrick87
5 Replies

8. Shell Programming and Scripting

Data Import perl script

Hi, I have a requirement for creating a Perl Script which will perform Data Import process in an automated way and I am elaborating herewith : Section 1 ) - use the following command line format : "./import.pl -h hostname -p port -f datafile.txt" Section 2) datafile.txt will... (3 Replies)
Discussion started by: scott_apc
3 Replies

9. Shell Programming and Scripting

PERL Text Munging

I have a file like this: host1,neighbor1 host3,neighbor4 host2,neighbor1 host2,neighbor3 host1,neighbor3 host3,neighbor1 host1,neighbor2 host3,neighbor2 I need some PERL magic that will generate output like this: host1,neighbor1,neighbor3,neighbor2 host2,neighbor1,neighbor3... (3 Replies)
Discussion started by: stateful
3 Replies

10. Shell Programming and Scripting

help for a perl script - writing to a data file

Hi, Here is my problem.. i have 2 files (file1, file2).. i have wrote the last two lines and first 4 lines of "file2" into two different variables .. say.. my $firstrec = `head -4 $file2`; my $lastrec = `tail -2 $file2`; and i write the rest of the file2 to a tmpfile and cat it with head... (2 Replies)
Discussion started by: meghana
2 Replies
Login or Register to Ask a Question