Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load arrays from a file 1

Status
Not open for further replies.

netman4u

Technical User
Mar 16, 2005
176
US
Hello all,

Been ages since I programmed. Appologize for my rustyness but I have the following text file:

Code:
Group            Type       Peers     Established    Active/Received/Damped
BRIB_PEERS   Internal   2         1

What I need to do is extract the "Peers" and the number below associated with it (in this case 2 under it) and the "Established" and the number associated with that (in this case 1).

So the "Peers" and "Established" are on the first line of the file and the numbers that correspond with them are on the line below as in one is the column heading and the other is the value.

Seemed straight forward to me just load each line in an array and them extract the corresponding array element into a hash.

Not working.

Here is my code:

Code:
use strict;
use warnings;
my @array1;
my @array2;
my %hash;
my %hash1;
my $logfile = "D:\\Stage\\out.log";
my $out_file = "D:\\Stage\\test5.txt";
open (IN, "$out_file") or die ("Error opening cli file $out_file: $1");
my $i = 0;
while (<IN>) {
	$i++;
	chomp;
	@array$i = $_;
}
$hash{$array1[2]} = $array2[2];
$hash1{$array1[3]} = $array2[3];
my $k;
my $v;
while ( ($k,$v) = each %hash ) {
    print "$k => $v\n";
}


Output:

Code:
D:\Stage>bpBellSouthCoyote_3.pl
Scalar found where operator expected at D:\Stage\bpBellSouthCoyote_3.pl line 42,
 near "@array$i"
        (Missing operator before $i?)
Global symbol "@array" requires explicit package name at D:\Stage\bpBellSouthCoy
ote_3.pl line 42.
syntax error at D:\Stage\bpBellSouthCoyote_3.pl line 42, near "@array$i "
Execution of D:\Stage\bpBellSouthCoyote_3.pl aborted due to compilation errors.

tried:

@array . "$i" = $_;
"@array" . "$i" = $_;

blah blah...no go.

Gotta be a better way!

Any help is appriciated (as always)

Nick



If at first you don't succeed, don't try skydiving.
 
Assuming there are only two lines in the file, this may be what you want:

REPLACE THIS:
my $i = 0;
while (<IN>) {
$i++;
chomp;
@array$i = $_;
}
$hash{$array1[2]} = $array2[2];
$hash1{$array1[3]} = $array2[3];

WITH THIS:
my @header = split(/\s+/, <INF>);
my @data = split(/\s+/, <INF>);

@hash{@h[2,3]} = @d[2,3];
 
D'oh!

In the last line, "@h" should be "@header" and "@d" should be "@data"

Sorry.
 
That works cfmartin!

Question. Why does this line:

my @data = split(/\s+/, <INF>);


Not just read the first line in the file again?

If at first you don't succeed, don't try skydiving.
 
D'oh!

I see! It does not! I did not know you could assign elements to a hash like that.

If at first you don't succeed, don't try skydiving.
 
Each time you read from a file using <FILE>, you read 1 record from a file. By default, the record is delimited by a carriage return("\n"). However, this can be changed using the $/ Perl variable. Glad I could help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top