Click to See Complete Forum and Search --> : Array Woes


jumper
10-11-2005, 01:48 PM
#!/usr/local/bin/perl -w

use strict;

my @fields = ();
my $line = 1;
my $pattern = "259775";
my $index = 0;

while (<>) {
@fields = $_;
# @fields = split/:/, $_;
}

#print @fields;
#print "@fields\n";
#print @fields."";

foreach $index (@fields) {
# print $fields[1], "\n";

print "element number $line is $index\n";

# if (/$pattern/) {
# print $line, " ", $_;
# }
$line = $line + 1;

# print "$_\n";
}

print $line, "\n";
this has gone a bit pete tong...

it was supposed to read a file [via a parameter at the command line] loop through the records, split the record into fields, search field(s) for string and print successful result or error message

I cannot even populate the array properly !

The latest output I have is

#output begin
element number 1 is

2
#output end

Is it overwriting itself ?

Is it just ignoring the array assignment ?

Are the print statements naff ?


tia,

Nedals
10-11-2005, 08:41 PM
use strict;
my $pattern = "259775";

while (<>) {
my @fields = split(/\t/, $_);
my $result = ($fields[1] =~ /$pattern/) ? 'sucess' : 'error';
print "$result\n";
}
exit;

jumper
10-12-2005, 05:18 AM
So I do not need separate loops :(

thanks Nedals

jumper
10-12-2005, 07:38 AM
#!/usr/local/bin/perl -w
# pmc_myapp.pl
# read file
# loop through records
# split record into fields
# search field(s) for string
# print successful result or error message

use strict;

# check for parameters and produce a message
unless (@ARGV)
{
print "Usage: $0 filename\n\n";
print "Example: $0 records.dat\n\n";
exit;
}

my $pattern = "259775";
my @fields = ();
my $result = "don't know";
my $fields;

while (<>) {
LINE: @fields = split(/:/, $_);
last LINE if $_ eq "\n";
# print @fields. "\n";
$result = ($fields[4] =~ /$pattern/) ? 'sucess' : 'error';

if ($result eq 'sucess') {
print "@fields\n";
}
}

exit;just a minor niggle :(

I'm, receiving this error

Label not found for "last LINE" at ./pmc_myapp.pl line 26, <> chunk 48.

The only way I have found to use labels is by creating a string followed by a colon.

Is this a scope issue ?


tia,

jumper
10-12-2005, 08:18 AM
LINE: while (<>) {
@fields = split(/:/, $_);
last LINE if /^$/;

$result = ($fields[3] =~ /$pattern/) ? 'sucess' : 'error';

if ($result eq 'sucess') {
print "@fields\n";
}
else {
print "record not found!\n";
}
}

aaah I should have labelled the whole block :)

Nedals
10-12-2005, 12:16 PM
while (<>) {
next if (/^$/); ## Skip blank lines
my @fields = split(/:/, $_);
....
Don't use lables unless you have to.
It makes your code messy. Reminds me of the old BASIC spaggetti code

Keep your variable scope as short as posssible. To that end, since @fields is only used within the 'while' loop, declare it there. (not outside). I don't know why you are declaring 'my $fields'. It's not used. (unless you are thinking the $fields[3] needs to be declared? It does not. It's part of the @fields array that you have already declared).


if ($fields[3] =~ /$pattern/) {
print "@fields\n";
} else {
print "record not found!\n";
}
Remove.... $result = ($fields[3] =~ /$pattern/) ? 'sucess' : 'error';
It's not needed. You also don't need the $result variable.

Actually you probably don't need the @fields array or the split

while (<>) {
next if (/^$/); ## skip blank line
chomp; ## remove any '\n'
# s/(\n|\r)//g; ## if working with Micro$ use this instead
if (/:$pattern:/) { ## match $pattern bounded by colons
print "$_\n";
} else {
print "record not found!\n";
}
}

jumper
10-14-2005, 05:20 AM
thanks for that Nedals,

have a good weekend :)


Paul