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

Hi there, I have an array @data with a single $column variable storing the column to be parsed from a input file.This $column has 125 elements with array indices 0 to 124. My question is if there is any way that I can store all the indices of this array as a single value in order to declare $data $index as a generalization for any element of array? Thanks

Replies are listed 'Best First'.
Re: How to store the indexes of a array
by BrowserUk (Patriarch) on Oct 14, 2011 at 01:34 UTC

    I've read this question half a dozen times and I am still none the wiser as to what you are seeking to do.

    I'd strongly suggest that you post a worked example -- using say 10 elements rather than 125 -- so that we mere mortals stand a chance of grasping your intent.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to store the indexes of a array
by NetWallah (Canon) on Oct 14, 2011 at 03:27 UTC
    Here is how I interpret your question (and answer it):
    my @data = (Some existing values); my @column = <Read 125 values from some file>; for my $index(@columns){ .. use $data[$index] }
    Hopefully, this will help you formulate your question more coherently.

                "XML is like violence: if it doesn't solve your problem, use more."

      For instance, my code is :

      my $infile = $ARGV[0]; open (IFILE, "<", $infile) || die "Cannot open : $!\n"; ## read the input file my @data; my $COLUMNTOPARSE; my $local; while (<IFILE>) { chomp; my @local = split ('\t',$_); $COLUMNTOPARSE = $local[$ARGV[1]]; push @data; $COLUMNTOPARSE; } close IFILE; my $count = @data;

      Now I wanted to get a generalization of all the indices of 125 elements stored in the $COLUMNTOPARSE in order to calculate variance of those elements

      my $variance; my $var; foreach my $data[$index] (@data) { $var = 1/($count-1); $variance = $var*(($data[$index]-$Average)**2); } print "$variance\n";

      But this $data$index way does not work.How should I proceed? Thanks..

        It doesn't sound to me like you want the indices of the array, but the actual values. When you use foreach (or for) with a scalar followed by an array, that's what you get. So you can just go ahead and use the values in your formula:

        foreach my $value (@data) { $var = 1/($count-1); $variance = $var*(($value-$Average)**2); }

        If you actually need the indices, you can use a C-style for loop or the range operator, in both cases letting the scalar value of the array represent the number of elements in it:

        for( my $i=0; $i < @data; $i++ ){ # do something with $data[$i] } # or for my $i (0..(@data-1)){ # do something with $data[$i] }
Re: How to store the indexes of a array
by flexvault (Monsignor) on Oct 14, 2011 at 10:44 UTC

    I'm reading into your question, but I have a script that downloads a CSV file with about 50 fields per record and then parses it to add records to a Berkeley DB. Unfortunately, every once in a while, the producer of the CSV file changes or worse adds a field in the middle of the record. My solution was to add a file:

    date 1 Account 19 Client 4 Address 1 5 Address 2 6 . . .

    Note: The "Account \t 19" is just to show key and index don't have to be in sequential order.

    The script reads this file into a hash where the first field is the column heading and second field is the index into the DB array (example code is untested):

    my %hash = (); my @array = (); my $no = 0; while (<$Fields>) { my $var = $_; chomp($var); my ( $key, $index ) = split(/\t/,$var); $hash{ $key } = $index; $array[$no] = $key; $no++; }

    The script then parses the first record(keys in hash) of the CSV file to verify all fields are in the same order as the $array and that nothing has changed. If something has changed, the script emails me but doesn't corrupt the DB. I then modify the above file to conform to the new data format. The file is the order of the input fields and the index is the location in the array that is written to the DB. If a new field has been added in the middle, I place it in key order in the file and the index is the next element at the end of the array.

    Once the key order is verified the script then processes the actual data and updates the DB.

    I hope this is similar to your problem.

    Good Luck

    "Well done is better than well said." - Benjamin Franklin