Hi,
I tried to make some changes to my script, but it still does not work.
Here is a better explanation:
this script that is executed like this: ./colstat.pl -f <filename> -e ;
(specify what 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:
1: 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);
$hash{$c + 1} = [ @array ];
}
for my $nr ( keys %hash ) {
print sort "$nr: @{ $hash{$nr}}\n";
}
The whole script: colstat.pl
#!/usr/bin/perl
# Needed pkg
use Getopt::Std;
use strict "vars";
# Global variables
my $VERBOSE = 0;
my $DEBUG = 0;
################################################################
# handle flags and
# Example: c == "-c", c: == "-c argument"
my $opt_string = 'hvdf:e:';
getopts( "$opt_string", \my %opt ) or usage() and exit 1; # exit other than 0 =
error!!
# print help message if -h is invoked
if ( $opt{'h'} ){
usage();
exit 0;
}
$VERBOSE = 1 if $opt{'v'};
$DEBUG = 1 if $opt{'d'};
my $FILE = $opt{'f'};
my $DELIMITER = $opt{'e'};
# main program content
# the actual code shuld run her
open (FILE, "$FILE") or die ("ERROR $!\n");
my @array;
my %hash = ();
my $column;
my @dataarray;
my $line;
my $counter;
my $var = $DELIMITER;
my $counter;
my $c;
while ( $line = <FILE> ) {
chomp $line;
@dataarray = split /$var/,$line;
$column = $dataarray[$c];
push(@array,$column);
$hash{$c + 1} = [ @array ];
}
for my $nr ( keys %hash ) {
print sort "$nr: @{ $hash{$nr}}\n";
}
##########################################
# Helper routines
sub usage {
# print the correct use of this script
print "Usage:\n";
print "-h Usage\n";
print "-v Verbose\n";
print "-d Debug\n";
print "-f Input file\n";
print "-e Delimiter\n";
}
sub verbose {
print $_[0] if ( $VERBOSE or $DEBUG );
}
sub debug {
print $_[0] if ( $DEBUG );
}
Pastebot is powered by POE.
Den 27. mars 2010 kl. 10.09 skrev Shlomi Fish:
> 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 .