#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Print problem in subroutine


    Suppose I have declared severals subroutines in a code and I want to print all the values of a "for" loop used in one of the subroutines

    part of the code where I want to get the print

    sub upstream

    my ($i,$j,$k, $up, $in, $dna, $size, $upstream, @temp, @pos,@neg, @genome_data );

    .

    .

    .

    .

    .

    .

    @genome_data = @_;

    for ($i=0; $i<@genome_data; $i++) {

    $genome_data$i{GENOME_SIZE} = $size;

    }

    end of part of code

    I want to print $genome_data$i{GENOME_SIZE} for every i ..... How can i do that

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    162
    Rep Power
    18
    Here's a suggestion:

    Code:
    #! /usr/bin/perl
    
    use strict;
    use warnings;
    
    sub myRoutine {
        my @hereArr = @_;
        my %hereHash = ();
        my $i = 0;
        
        for ($i = 0; $i < @hereArr; $i++) {
            $hereHash{"size"} = [$i, 100];
            print "Printing for $i --> $hereHash{\"size\"}[1]\n";
        }
    }
    
    myRoutine ("one", "two", "three");
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    I want to print $genome_data$i{GENOME_SIZE} for every i ..... How can i do that
    Those hashes (%genome_data0, %genome_data1, etc) aren't defined in the code you posted. Are they defined elsewhere, or did you mean to say that you want to print out the data in the @genome_data array and $i is the array index?

    Is @genome_data a plain array, or is it an array of hash refs?
    Last edited by FishMonger; September 12th, 2013 at 10:14 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    Originally Posted by ashprog
    $genome_data$i{GENOME_SIZE} = $size;
    This syntax is weird. Are you trying to use symbolic references or what? You should probably go for an array of hashes, something like this:

    Perl Code:
    $genome_data[$i]{GENOME_SIZE} = $size;


    As for your question, it depends where you have declared @genome_data or %genome_data (not clear to me whether you intend it to be an array of a hash).

IMN logo majestic logo threadwatch logo seochat tools logo