On Sunday 28 Mar 2010 10:55:09 alekto wrote: > 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)
Yes , but in bash/etc. you need to do << -e \; >> because ";" is the statement separator. > 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 > . > . > . > . > . > OK . Are the hash keys going to be irregularly distributed, or do you escape them to be sequential? If the latter is the case, you are better served by an array. > 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"; 1. Why do you only have «use strict "vars";» instead of «use strict; use warnings;»? 2. Why are you using Getopt::Std instead of Getopt::Long? > > # 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'}; Please declare the variables as close as possible to using them: {{{ my $VERBOSE = $opt{'v'} ? 1 : 0; }}} > $DEBUG = 1 if $opt{'d'}; > my $FILE = $opt{'f'}; > my $DELIMITER = $opt{'e'}; > Nice. > # main program content > # the actual code shuld run her > > open (FILE, "$FILE") or die ("ERROR $!\n"); > Use three-args-open, and lexical file-handles: {{{ open my $fh, "<", $FILE or die "Error opening file - $!\n"; }}} Also don't call variables in all-caps. > 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]; Why are you using $c without initialising it? > push(@array,$column); > $hash{$c + 1} = [ @array ]; You're keep assigning to the same $c and overwriting the $hash{$c + 1} value. use warnings would have warned you about it because $c is undef. > } > 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. > :-D. Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ My Aphorisms - http://www.shlomifish.org/humour.html 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: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/