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

Hi Monks,

Need help on array.
i have this file with the following content:

name;address1;address2;postcode;state;country

i have this code to get the array

open(RESULT,"file") || die "EXECUTION ERROR : Fail to open"; while (<RESULT>) { chop(); $REC = $_; @LINEREC = split(/\;/,$REC); print "LINEREC[0]\n"; } close RESULT;

Understood that it will print "name". If i want to do the reverse that is print the LINEREC [ ? ] value, how do i do it?
Example: I want to know "postcode" fall in which array. and the script should print "LINEREC[ 3 ]"

Thanks in advance

Replies are listed 'Best First'.
Re: get array number
by ikegami (Patriarch) on Jan 20, 2006 at 06:22 UTC

    So you want to lookup the field number given the field name? Hashes are usually great as lookup tables. You can use $. to determine whether this is the first line of the file or not (or you could create a flag).

    use strict; use warnings; my %col_idx; open(my $result_fh, '<', 'file') # 3-arg safer or die("Unable to open input file: $!\n"); # $! meaningful while (<$result_fh>) { chomp(); # chomp safer my @fields = split(/;/, $_); if ($. == 1) { @col_idx{@fields} = 0..$#fields; } else { # Do what you want here. print "$fields[$col_idx{name}], "; print "$fields[$col_idx{postcode}]\n"; } }
      The following is a slightly different version that will allow you to simply type just the field name instead of $col_idx{XXX}:
      use strict; use warnings; my @col_names; open(my $result_fh, '<', 'file') # 3-arg safer or die("Unable to open input file: $!\n"); # $! meaningful while (<$result_fh>) { chomp(); # chomp safer my @fields = split(/;/, $_); if ($. == 1) { @col_names = @fields; } else { my %fields; @fields{@col_names} = @fields; # Do what you want here. print "$fields{name}, "; print "$fields{postcode}\n"; } }

      Untested.

      Or just read the first line and parse the headers outside your while loop and avoid the extra logic inside (of course the truly paranoid would then want to error check and make sure that first read didn't return undef . . . :).

Re: get array number
by helphand (Pilgrim) on Jan 20, 2006 at 06:14 UTC

    A couple of comments;

    • you really really should;
      use strict; use warnings;
    • chomp() is safer than chop() in this context.

    As for your question, if I understand it correctly, you need to fix the print statement to refer to your array variable, as written, it is simply printing the text that is enclosed in the quotes.

    print "name = $LINEREC[0]\n"; print "postcode = $LINEREC[3]\n";

    Scott

      Hi Scott,

      Thanks for your feedback. The problem is that i might not know what array "Postcode" is and the sequence in the file might not be in orders e.g. it can be Postcode;Name;Address2;Address3;.....

      I still need to know postcode or other value is in which array. Is there any way to do it?

      Thanks...

        You wrote:
        I still need to know postcode or other value is in which array. Is there any way to do it?
        Of course there is some way :) We know that postcode is consists of numbers only, name does not include any number, and adress is words & numbers.
        So.. that is draft code for You.
        my @file; $file[0]="Alexey;123456789;USA some state some street home # 12\n"; $file[1]="20102020;Nikola;Russian Federation some state some street ho +me # 13\n"; $file[2]="England some state some street home # 14;5545487987;Max\n"; my @sorted_values; foreach $file_string(@file){#like your while(<>) chomp $file_string; my @string_values=split/;/,$file_string; my $sorted; foreach (@string_values){ if (/\D/ && !/\W/ ){#This is name $sorted->{NAME}=$_; }elsif(/\D/ && /\W/){#This is adress $sorted->{ADRESS}=$_; }else{#This is postcode $sorted->{POSTCODE}=$_; } } push @sorted_values, $sorted; } foreach (@sorted_values){ print "NAME: $_->{NAME} ADRESS: $_->{ADRESS} POSTCODE: $_->{POSTCODE} +\n"; }

        Provided you know the field names in advance, then ikegami's solution will work perfectly for you.

        Scott

        how do you know whose fild is the postcode? on the file have any way to know, cant you organize the file? sorry about my english...