Hashes and subroutines

D

deadpickle

What I want to do is search a text file for the correct station ID. I
hope this is clearer from the code. I run it and it returns the correct
values but I get the warning "Odd number of elements in hash assignment
at search1.pl line 15, <ID> line 6." Im not sure what this means. Any
help?
Also I want to pass a few variables to a subroutine but am not sure how
to do this.

Text file
===================================================================
KLNK 261412Z 04011KT 3/4SM R36/5500VP6000FT BR OVC003 07/06 A2992 RMK
AO2
KCHS 131656Z 15009KT 10SM -RA FEW027 BKN044 24/20 A2994 RMK AO2 RAB44
SLP137 P0000 T02440200
KEMP 101653Z AUTO 33016KT 10SM BKN065 BKN090 07/00 A2993 RMK AO2 PK WND
33027/1559 SLP136 T00670000
KHSI 101653Z AUTO 36015KT 7SM OVC007 03/01 A3013 RMK AO2 SLP214
T00280006
KRSL 101653Z AUTO 35021G27KT 10SM BKN018 OVC030 06/00 A3009 RMK AO2 PK
WND 35027/1649 SLP194 T00560000
KRWL 101653Z AUTO 25018G22KT 10SM CLR M02/M07 A3027 RMK AO2 SLP273
T10171067

My Program
===================================================================
#=========================================================
# This code is used to search a text file for the correct
# ASOS sites are then runs the metarcode subroutine to
# create the placefile that GRLevel3 needs.
#=========================================================
use strict;
use warnings;
$\ = "\n";
# Open the text file containing the stations
open (ID, "stations.txt");
while (my $stations = <ID>) {
my @station = split (/\n/,$stations); # enter each line into an array
foreach (@station) { # Search the array for the correct stations
print $_;
if (my %stid = $_ =~ m/^KRWL/) {
my $id = $_;
print $id;
}
}
}

This is a prelimanary program so it does not pass the variables to the
subroutine. I want to pass three variables to the subroutine: 1) the
array resulting from the search;2) the latitude of the site; 3) the
longitude. THe lat and long need to be inserted into a variable on the
subroutine and so does the array. I am not sure on how to do this and
any help wouild be great. Thanks.
 
J

John W. Krahn

deadpickle said:
What I want to do is search a text file for the correct station ID. I
hope this is clearer from the code. I run it and it returns the correct
values but I get the warning "Odd number of elements in hash assignment
at search1.pl line 15, <ID> line 6." Im not sure what this means.

It means that you have to assign an even number of elements to a hash.

Also I want to pass a few variables to a subroutine but am not sure how
to do this.

Text file
===================================================================
KLNK 261412Z 04011KT 3/4SM R36/5500VP6000FT BR OVC003 07/06 A2992 RMK
AO2
KCHS 131656Z 15009KT 10SM -RA FEW027 BKN044 24/20 A2994 RMK AO2 RAB44
SLP137 P0000 T02440200
KEMP 101653Z AUTO 33016KT 10SM BKN065 BKN090 07/00 A2993 RMK AO2 PK WND
33027/1559 SLP136 T00670000
KHSI 101653Z AUTO 36015KT 7SM OVC007 03/01 A3013 RMK AO2 SLP214
T00280006
KRSL 101653Z AUTO 35021G27KT 10SM BKN018 OVC030 06/00 A3009 RMK AO2 PK
WND 35027/1649 SLP194 T00560000
KRWL 101653Z AUTO 25018G22KT 10SM CLR M02/M07 A3027 RMK AO2 SLP273
T10171067

My Program
===================================================================
#=========================================================
# This code is used to search a text file for the correct
# ASOS sites are then runs the metarcode subroutine to
# create the placefile that GRLevel3 needs.
#=========================================================
use strict;
use warnings;
$\ = "\n";
# Open the text file containing the stations
open (ID, "stations.txt");

You should *ALWAYS* verify that the file opened correctly:

open ID, '<', 'stations.txt' or die "Cannot open 'stations.txt' $!";

while (my $stations = <ID>) {
my @station = split (/\n/,$stations); # enter each line into an array

Because you haven't modified $/ the value in $stations should have just one
newline at the end of $stations.

foreach (@station) { # Search the array for the correct stations
print $_;
if (my %stid = $_ =~ m/^KRWL/) {

You are assigning a single value to the hash %stid which is why you get the
warning.

my $id = $_;
print $id;
}
}
}

Your while loop could be written more simply as:

while ( <ID> ) {
chomp;
if ( /^KRWL/ ) {
print;
}
}

This is a prelimanary program so it does not pass the variables to the
subroutine.

There is no subroutine in your example.
I want to pass three variables to the subroutine: 1) the
array resulting from the search;

There is no array in your example.
2) the latitude of the site; 3) the
longitude. THe lat and long need to be inserted into a variable on the
subroutine and so does the array. I am not sure on how to do this and
any help wouild be great. Thanks.

Which fields contain the lat and long?



John
 
J

J. Gleixner

deadpickle said:
What I want to do is search a text file for the correct station ID. I
hope this is clearer from the code. I run it and it returns the correct
values but I get the warning "Odd number of elements in hash assignment
at search1.pl line 15, <ID> line 6." Im not sure what this means. Any
help?

Look in perldiag:

perldoc perldiag


"Odd number of elements in hash assignment

(W misc) You specified an odd number of elements to initialize a hash,
which is odd, because hashes come in key/value pairs."

Also I want to pass a few variables to a subroutine but am not sure how
to do this.

perldoc perlsub
Text file
===================================================================
KLNK 261412Z 04011KT 3/4SM R36/5500VP6000FT BR OVC003 07/06 A2992 RMK
AO2
KCHS 131656Z 15009KT 10SM -RA FEW027 BKN044 24/20 A2994 RMK AO2 RAB44
SLP137 P0000 T02440200
KEMP 101653Z AUTO 33016KT 10SM BKN065 BKN090 07/00 A2993 RMK AO2 PK WND
33027/1559 SLP136 T00670000
KHSI 101653Z AUTO 36015KT 7SM OVC007 03/01 A3013 RMK AO2 SLP214
T00280006
KRSL 101653Z AUTO 35021G27KT 10SM BKN018 OVC030 06/00 A3009 RMK AO2 PK
WND 35027/1649 SLP194 T00560000
KRWL 101653Z AUTO 25018G22KT 10SM CLR M02/M07 A3027 RMK AO2 SLP273
T10171067

My Program
===================================================================
#=========================================================
# This code is used to search a text file for the correct
# ASOS sites are then runs the metarcode subroutine to
# create the placefile that GRLevel3 needs.
#=========================================================
use strict;
use warnings;
$\ = "\n";
# Open the text file containing the stations
open (ID, "stations.txt");
What if it fails??

open( my $ids, '<', 'stations.txt' ) or die "Can't open stations.txt: $!";
while (my $stations = <ID>) {
while ( my $stations = said:
my @station = split (/\n/,$stations); # enter each line into an array

You're already reading one line at at time, by default.
foreach (@station) { # Search the array for the correct stations
print $_;
if (my %stid = $_ =~ m/^KRWL/) {
You're setting a hash to one value. A hash has a key and a value, hence
your error.
my $id = $_;
print $id;
}
}
}

open( my $station_data, '<', 'stations.txt' )
or die "Can't open stations.txt: $!";
while( my $station = <$station_data> ) {
print $station if $station =~ /^KRWL/;
}
close ( $station_data );

This is a prelimanary program so it does not pass the variables to the
subroutine. I want to pass three variables to the subroutine: 1) the
array resulting from the search;2) the latitude of the site; 3) the
longitude. THe lat and long need to be inserted into a variable on the
subroutine and so does the array. I am not sure on how to do this and
any help wouild be great. Thanks.

sub some_sub
{
my $data = shift;
my $latitude = shift;
my $longitude = shift;

# do something

}

my @data = qw ( value1 value2 );
some_sub( \@data, '12.003', '34.45' );

See: perlsub for a lot of documentation on subroutines.
 
J

John W. Krahn

deadpickle said:
What I want to do is search a text file for the correct station ID. I
hope this is clearer from the code. I run it and it returns the correct
values but I get the warning "Odd number of elements in hash assignment
at search1.pl line 15, <ID> line 6." Im not sure what this means.

It means that you have to assign an even number of elements to a hash.
Also I want to pass a few variables to a subroutine but am not sure how
to do this.

Text file
===================================================================
KLNK 261412Z 04011KT 3/4SM R36/5500VP6000FT BR OVC003 07/06 A2992 RMK
AO2
KCHS 131656Z 15009KT 10SM -RA FEW027 BKN044 24/20 A2994 RMK AO2 RAB44
SLP137 P0000 T02440200
KEMP 101653Z AUTO 33016KT 10SM BKN065 BKN090 07/00 A2993 RMK AO2 PK WND
33027/1559 SLP136 T00670000
KHSI 101653Z AUTO 36015KT 7SM OVC007 03/01 A3013 RMK AO2 SLP214
T00280006
KRSL 101653Z AUTO 35021G27KT 10SM BKN018 OVC030 06/00 A3009 RMK AO2 PK
WND 35027/1649 SLP194 T00560000
KRWL 101653Z AUTO 25018G22KT 10SM CLR M02/M07 A3027 RMK AO2 SLP273
T10171067

My Program
===================================================================
#=========================================================
# This code is used to search a text file for the correct
# ASOS sites are then runs the metarcode subroutine to
# create the placefile that GRLevel3 needs.
#=========================================================
use strict;
use warnings;
$\ = "\n";
# Open the text file containing the stations
open (ID, "stations.txt");

You should *ALWAYS* verify that the file opened correctly:

open ID, '<', 'stations.txt' or die "Cannot open 'stations.txt' $!";

while (my $stations = <ID>) {
my @station = split (/\n/,$stations); # enter each line into an array

Because you haven't modified $/ the value in $stations should have just one
newline at the end of $stations.

foreach (@station) { # Search the array for the correct stations
print $_;
if (my %stid = $_ =~ m/^KRWL/) {

You are assigning a single value to the hash %stid which is why you get the
warning.

my $id = $_;
print $id;
}
}
}

Your while loop could be written more simply as:

while ( <ID> ) {
chomp;
if ( /^KRWL/ ) {
print;
}
}

This is a prelimanary program so it does not pass the variables to the
subroutine.

There is no subroutine in your example.
I want to pass three variables to the subroutine: 1) the
array resulting from the search;2) the latitude of the site; 3) the
longitude. THe lat and long need to be inserted into a variable on the
subroutine and so does the array. I am not sure on how to do this and
any help wouild be great. Thanks.

Which fields contain the lat and long?




John
 
M

Mumia W. (reading news)

What I want to do is search a text file for the correct station ID. I
hope this is clearer from the code. I run it and it returns the correct
values but I get the warning "Odd number of elements in hash assignment
at search1.pl line 15, <ID> line 6." Im not sure what this means. Any
help?
Also I want to pass a few variables to a subroutine but am not sure how
to do this.

Text file
===================================================================
KLNK 261412Z 04011KT 3/4SM R36/5500VP6000FT BR OVC003 07/06 A2992 RMK
AO2
KCHS 131656Z 15009KT 10SM -RA FEW027 BKN044 24/20 A2994 RMK AO2 RAB44
SLP137 P0000 T02440200
KEMP 101653Z AUTO 33016KT 10SM BKN065 BKN090 07/00 A2993 RMK AO2 PK WND
33027/1559 SLP136 T00670000
KHSI 101653Z AUTO 36015KT 7SM OVC007 03/01 A3013 RMK AO2 SLP214
T00280006
KRSL 101653Z AUTO 35021G27KT 10SM BKN018 OVC030 06/00 A3009 RMK AO2 PK
WND 35027/1649 SLP194 T00560000
KRWL 101653Z AUTO 25018G22KT 10SM CLR M02/M07 A3027 RMK AO2 SLP273
T10171067

My Program
===================================================================
#=========================================================
# This code is used to search a text file for the correct
# ASOS sites are then runs the metarcode subroutine to
# create the placefile that GRLevel3 needs.
#=========================================================
use strict;
use warnings;
$\ = "\n";
# Open the text file containing the stations
open (ID, "stations.txt");
while (my $stations = <ID>) {
my @station = split (/\n/,$stations); # enter each line into an array
foreach (@station) { # Search the array for the correct stations
print $_;
if (my %stid = $_ =~ m/^KRWL/) {

If KRWL is found, this statement assigns (1) to the hash %stid--a hash
that seems to have no reason to exist since it's lexically scoped to the
if statement. Hashes expect to be initialized with even numbers of
initializers, e.g., this is usually an error:

my %stid = (1);

my $id = $_;
print $id;
}
}
}

This is a prelimanary program so it does not pass the variables to the
subroutine. I want to pass three variables to the subroutine: 1) the
array resulting from the search;2) the latitude of the site; 3) the
longitude. THe lat and long need to be inserted into a variable on the
subroutine and so does the array. I am not sure on how to do this and
any help wouild be great. Thanks.

I don't know anything about that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top