[perl] Hash aanroepen

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • smiler69
  • Registratie: Februari 2010
  • Laatst online: 30-12-2020
Heren,

Ik ben bezig met een perl scriptje aan het maken om een bepaalt input naar een formaat veranderd moet worden. Om ik lang verhaal kort te maken.
Ik wil gebruik maken van hashes. Maar ik heb wat problemen met het aanroepen van de hash. Ik zal vast wat over het hoofd hebben gezien, maar ik kom er niet uit.

Het volgende heb ik:

Ik heb een bestand met de input:
code:
1
2
3
9:78:90:64:965:0
77:55:55:9:99:2
enz


Nou heb ik een functie dat de data in een array of een hash zet.
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# fileToArray
# loads data of file in array
# non_csv=1 word list without index
# non_csv=2 key=whole line, value=index
sub fileToArray            
{                          
    my @records;           
    my $file=shift;        
    my $non_csv=shift;
    my ($iid,$item);
    open FILE, "<$file" or die "Error: Can't open $file. $!";
#   create hash of file with 2 columns
    if ($non_csv eq '1') {
        my %hash;
        while( <FILE> ){ 
            chomp;
            ($iid,$item) =split(':');
        $hash{ $item }=$iid;
        }  
        return \%hash;
}          
#   create hash of file
    elsif ($non_csv eq '2') {
        my %hash;
        my $i=0;
        while(my $line= <FILE> ){
            $hash { $line }=$i;
            #print $line,"\n";
        }
        return \%hash;
}
    else{
#we will read the file line by line 
        while(<FILE>)
        {  
            # read each line in $_  
            # chomp the trailing newline from $_ 
            chomp;
            my @columns = split ":", $_;
            push @records, \@columns;
    }
    return @records;
    # Gets amount of columns of csv
}
close(FILE);
return @records;
}


Normaal gesproken roep ik een hash aan:
code:
1
$e= &fileToArray('g4',2);


Print een item met een key: 77:55:55:9:99:2
code:
1
print $e-> 77:55:55:9:99:2


Dit hoort een bepaalde index terug te geven. Maar ik krijg de volgende error terug:
code:
1
Use of uninitialized value in print at ./ngram.pl line 364, <FILE> line 627471.


Wat meestal betekent dat de key niet bestaat. Dus ik dacht dat misschien dat de hash leeg is, maar wanneer ik de keys van de hash print, zie ik alle keys.
code:
1
print keys %$e;


Wat doe ik verkeerd of zie ik wat over het hoofd?

Acties:
  • 0 Henk 'm!

  • Keiichi
  • Registratie: Juni 2005
  • Laatst online: 11-09 21:35
De functie returned een array, misschien ligt het daaraan.

Solar @ Dongen: http://solar.searchy.net/ - Penpal International: http://ppi.searchy.net/


Acties:
  • 0 Henk 'm!

  • smiler69
  • Registratie: Februari 2010
  • Laatst online: 30-12-2020
Keiichi schreef op dinsdag 05 juli 2011 @ 15:27:
De functie returned een array, misschien ligt het daaraan.
Nee, ik heb de return @records gecomment, maar ik krijg dezelfde error.

Ook heb ik na return \%hash;
last; geprobeerd,maar dat mocht niet baten

Acties:
  • 0 Henk 'm!

  • JaWi
  • Registratie: Maart 2003
  • Laatst online: 26-08 22:41

JaWi

maak het maar stuk hoor...

Probeer eens je hash-key in quotes te zetten, en, gebruik curly brackets:
Perl:
1
print $e->{"77:55:55:9:99:2"}

Statistics are like bikinis. What they reveal is suggestive, but what they hide is vital.


Acties:
  • 0 Henk 'm!

  • Keiichi
  • Registratie: Juni 2005
  • Laatst online: 11-09 21:35
in
code:
1
2
3
4
5
    elsif ($non_csv eq '2') {
        my %hash;
        my $i=0; 
        while(my $line= <FILE> ){
            $hash { $line }=$i;


Nog een chomp $line; in je while zetten.

:) Een newline character kan perfect in je key voorkomen, maar het maakt het daarna niet makkelijker.

Solar @ Dongen: http://solar.searchy.net/ - Penpal International: http://ppi.searchy.net/


Acties:
  • 0 Henk 'm!

  • smiler69
  • Registratie: Februari 2010
  • Laatst online: 30-12-2020
In dit geval maakte het gebruik van Quotes of curly brackets niet uit.

chomp $line; did the trick.
en ik was $i++; vergeten @ regel 29.

Het werkt.

Thnx heren.
Pagina: 1