stalkeyefly has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

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 tr +anscription, 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 = <FILE>; 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.

Cheers
Stalky

janitored by ybiC: Balanced <code> tags around code block as per Monaster convention

Replies are listed 'Best First'.
Re: array of arrays
by Roy Johnson (Monsignor) on Jun 15, 2004 at 17:39 UTC
    Update: You actually have the square brackets, but you didn't use <code></code> tags around your code.

    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.

    We're not really tightening our belts, it just feels that way because we're getting fatter.
      Sorry all for the lasck of use of code tags. And thanks for the editing. I can appreciate that if you are going to ask if your code is okay that it helps to present bin such away that people can in fact read it.

      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.

        You declared your AoA as a lexical, outside of any braces, so it is visible from that declaration until the end of the file. If it were inside braces, it would only be visible to the closing brace. If you want it to be visible outside the file, you should declare it with our.

        We're not really tightening our belts, it just feels that way because we're getting fatter.
Re: array of arrays
by pbeckingham (Parson) on Jun 15, 2004 at 17:44 UTC

    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 (<FILE>) { if ($line =~ /\s*InterPro\:(\S+)\s(.+) > (.+) ; (.+)/) { push @i2g, [$1, $2, $3, $4]; } } print @i2g; close FILE; exit 0;