I'm new to programming - and I though that PERL would be a great place to start (as my aim to to make my researchs groups knowlage avalible on the web - to the world as i'm sure they are all interested).
I'd like someone to take a look at my code if possible. I think that its creating an array of arrays but I'm not 100% sure. Now i know if I start putting all the sub-routines together before I know they work I'm in bother!
#!/usr/bin/perl use strict; use warnings; # subroutine to build AoA # example data from file being used # InterPro:IPR000005 Helix-turn-helix, AraC type > GO:regulation of transcription, DNA-dependent ; GO:0006355 # InterPro:IPR000005 Helix-turn-helix, AraC type > GO:intracellular ; GO:0005622 # InterPro:IPR000006 Vertebrate metallothionein > GO:metal ion binding ; GO:0046872 # note from a VERY large file my @i2g; my @miniarray; my @i2glines; my $line; my ($ipid,$ipdesc,$godesc,$goid); open (FILE, "interpro2go.txt"); @i2glines =; foreach $line (@i2glines) { if ($line =~ /\s*InterPro\:(\S+)\s(.+) > (.+) ; (.+)/){ my ($ipid,$ipdesc,$godesc,$goid); @miniarray = ($ipid, $ipdesc, $godesc, $goid); push @i2g, [ @miniarray ]; } } print "job done? Doing something but the correct thing?"; print @i2g; # !!!!!!!!!!! return for use to the main prog keeping AoA avaliable to the main_processing (na other subroutine!! close FILE; exit;
What I intend to do is search this array using an array of values pulled from a hash of arrays. The key to the Hash of arrays is unique but the elements that I need to search here are not that's why I opted for the array of arrays.
Cheersjanitored by [ybiC]: Balanced <code> tags around code block as per Monaster convention
If you believe that
push @i2g, @miniarray;is creating an array of arrays, you are wrong. Multidimensional data structures are created by collecting references -- an array of arrays is really an array of array references, for example. So to do what (I think) you want:
push @i2g, [@miniarray];perldoc perlreftut for some more information.
The code that I posted does run - part of the out put shown below. I just did not know if this was the correct output.
ARRAY(0x846aa04)ARRAY(0x846aa4c)ARRAY(0x846aa94)ARRAY(0x846aadc)...etc..
Now assuming thats correct, do I need to send the AoA back to the main program for it to be used by another sub_routine. or can I change the way its decleared to make it a global variable?
this is a far as i have got with scoping - using my outside a subroutine, I make a the variable visiable to whole program. Using my insdie a subroutine local makes it local....
Edited by Chady -- shortened the list of array refs because it was breaking the browsers and the actual memory addresses were not relevant.
Yikes. I reformatted, made your single A an AoA using array references, and made some little changes. You weren't handling the regex-ed data at all. This is totally untested, but then - so was yours. I suggest showing us some of your input to improve that regular expression (just anchoring it would be nice), and consistently using whitespace metacharacters.
#!/usr/bin/perl use strict; use warnings; open (FILE, 'interpro2go.txt') || die 'cannot open file'; my @i2g; foreach my $line () { if ($line =~ /\s*InterPro\:(\S+)\s(.+) > (.+) ; (.+)/) { push @i2g, [$1, $2, $3, $4]; } } print @i2g; close FILE; exit 0;
perlmonks.org content © perlmonks.org and pbeckingham, Roy Johnson, stalkeyefly
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03