Hi alekto!
A few comments on your code based on your excerpt.
On Saturday 27 Mar 2010 00:08:31 alekto wrote:
> Hi,
> I got this script that is executed like this: ./colstat.pl -f <filename> -e
> ; (specifywhat to use in the split function in order to divide the
> columns) I would like to put each line from the input file into an array,
> and I want to put this array into a hash, where the column number equals
> to the hash key/pointer, so the print would look somehow like this:
>
> nr 1: 34 67 927 536 8 92
> nr 2: 4 563 1 56789 983
> nr 3: 43 02 903 63 73 893
> .
> .
> .
> .
> .
>
> Following is the script I got this far, this does not print out the output
> I just described, but an output looking like this: 6: 34 4 78 1 5463
>
> The part with the probem:
>
>
> while ( $line = <FILE> ) {
> chomp $line;
> @dataarray = split /$var/,$line;
> $column = $dataarray[$c];
> push(@array,$column);
> $counter++;
>
> }
1. Add "use strict;" and "use warnings;" and fix all their errors and
warnings. You need to declare variables using "my".
2. Don't use bareword filehandles - see
http://www.perlfoundation.org/perl5/index.cgi?ancient_perl
> $hash{$counter + 1} = [ @array ];
>
Why are you populating the hash after the loop? Don't you want to have every
line in a separate record? Furthermore, if you're using integer keys,
shouldn't you be using an array instead?
> for my $nr ( keys %hash ) {
> print sort "$nr: @{ $hash{$nr} }\n";
> }
You cannot sort a single string. Do you want to sort @{ $hash{$nr} }. If so,
you can use:
{{{
sort { $a <=> $b } @{ $hash{$nr} }
}}}
Or:
{{{
sort { $a cmp $b } @{ $hash{$nr} }
}}}
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
The Case for File Swapping - http://shlom.in/file-swap
Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/