Europa MacDonald
asked on
data failing to pass to arrays
I have this code here.
no information seems to be passed to @match and @nomatch. I have attached the input test file. Could someone help me determine why no information is being passed to these arrays ?
(information passes from @array fine)
open(INPUT, "<C:/Users/mickg/Desktop/L essons/arr ays/input. txt");
my $line = 1;
my @array = 1..6;
while(<INPUT>)
{
my $text = added;
print WRITE "my array: @array \n\n";
print "$line\t $text \t $_ \n";
print WRITE "$line\t $text \t $_ \n";
$line += 1;
my @match = grep { $_ eq $lines[5] } @lines[0..4];
my @nomatch = grep {$_ ne $lines[5] } @lines[0..4];
print WRITE "all matching numbers : @nomatch \n\n";
shift @lines;
}
print WRITE "all matching numbers : @nomatch \n\n";
close (INPUT);
input.txt
no information seems to be passed to @match and @nomatch. I have attached the input test file. Could someone help me determine why no information is being passed to these arrays ?
(information passes from @array fine)
open(INPUT, "<C:/Users/mickg/Desktop/L
my $line = 1;
my @array = 1..6;
while(<INPUT>)
{
my $text = added;
print WRITE "my array: @array \n\n";
print "$line\t $text \t $_ \n";
print WRITE "$line\t $text \t $_ \n";
$line += 1;
my @match = grep { $_ eq $lines[5] } @lines[0..4];
my @nomatch = grep {$_ ne $lines[5] } @lines[0..4];
print WRITE "all matching numbers : @nomatch \n\n";
shift @lines;
}
print WRITE "all matching numbers : @nomatch \n\n";
close (INPUT);
input.txt
ASKER
I have attached the input file Dave. It is just a simple arrangement of numbers while I get this working.
input data:
1 2 3 4 5 6
7 8 9 10 11 12
1 2 3 4 5 6
4 6 3 5 1 6
8 7 9 4 3 8
4 3 2 1 2 1
9 8 2 5 1 4
5 3 6 4 3 8
input data:
1 2 3 4 5 6
7 8 9 10 11 12
1 2 3 4 5 6
4 6 3 5 1 6
8 7 9 4 3 8
4 3 2 1 2 1
9 8 2 5 1 4
5 3 6 4 3 8
I'm slightly confused now. Where does @lines get populated?
ASKER
this was the original code that was provided previously, but I wasnt sure how it worked so I tried to simplify it
also, it gets the error message
Global symbol "%lines" requires explicit package name at 002comparitor.pl line 20.
too
my $fil = shift; # command line argument is the file to read lines from
open (INPUT,"<C:/Users/mickg/De sktop/Less ons/comp/m aster.txt" ) or die "can not open INPUT";
open (MATCH,">C:/Users/mickg/De sktop/Less ons/comp/m atch.txt") or die "can not open MATCH";
#open (NONMATCH,">C:/Users/mickg /Desktop/L essons/com p/nonmatch .txt") or die "can not open NONMATCH";
my @lines = map { chomp; $_ } <INPUT>;
while (@lines > 6) {
# check first 5 lines against the 6th line
my @match = grep { $_ eq $lines[5] } @lines[0..4];
my @nomatch = grep {$_ ne $lines[5] } @lines{0..4};
# do whatever you want with the matches and no-matches
print MATCH @match;
# get rid of first line so next loop will be 2-6 and so on
shift @lines;
}
close (INPUT);
close (MATCH);
also, it gets the error message
Global symbol "%lines" requires explicit package name at 002comparitor.pl line 20.
too
my $fil = shift; # command line argument is the file to read lines from
open (INPUT,"<C:/Users/mickg/De
open (MATCH,">C:/Users/mickg/De
#open (NONMATCH,">C:/Users/mickg
my @lines = map { chomp; $_ } <INPUT>;
while (@lines > 6) {
# check first 5 lines against the 6th line
my @match = grep { $_ eq $lines[5] } @lines[0..4];
my @nomatch = grep {$_ ne $lines[5] } @lines{0..4};
# do whatever you want with the matches and no-matches
print MATCH @match;
# get rid of first line so next loop will be 2-6 and so on
shift @lines;
}
close (INPUT);
close (MATCH);
Well, didn't we fix the "%lines" error a couple of questions ago? :-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thankyou Wilcoxon. I have checked path and file names but there is still nothing being passed to the output.txt file. I am too new learning PERL to be able to problem-solve this on my own, unfortunately.
I have attached the input and output files.
I am using the code as above:
#!/usr/bin/perl
use strict;
use warnings;
# no need for $fil line since you hard-coded the input filename
open (INPUT,"<C:/Users/mickg/De sktop/Less ons/comp/i nput.txt") or die "can not open INPUT";
open (MATCH,">C:/Users/mickg/De sktop/Less ons/comp/o utput.txt" ) or die "can not open MATCH";
my @lines = map { chomp; $_ } <INPUT>;
while (@lines > 6) {
# check first 5 lines against the 6th line
my @match = grep { $_ eq $lines[5] } @lines[0..4];
my @nomatch = grep {$_ ne $lines[5] } @lines[0..4];
# do whatever you want with the matches and no-matches
print MATCH join("\n", @match), "\n";
# get rid of first line so next loop will be 2-6 and so on
shift @lines;
}
close (INPUT);
close (MATCH);
output.txt
input.txt
I have attached the input and output files.
I am using the code as above:
#!/usr/bin/perl
use strict;
use warnings;
# no need for $fil line since you hard-coded the input filename
open (INPUT,"<C:/Users/mickg/De
open (MATCH,">C:/Users/mickg/De
my @lines = map { chomp; $_ } <INPUT>;
while (@lines > 6) {
# check first 5 lines against the 6th line
my @match = grep { $_ eq $lines[5] } @lines[0..4];
my @nomatch = grep {$_ ne $lines[5] } @lines[0..4];
# do whatever you want with the matches and no-matches
print MATCH join("\n", @match), "\n";
# get rid of first line so next loop will be 2-6 and so on
shift @lines;
}
close (INPUT);
close (MATCH);
output.txt
input.txt
Are you getting any errors when you run it? When I run it, I get the same as ozo ("1 2 3 4 5 6" as the only row in output.txt). A single row of "1 2 3 4 5 6" is correct based on what you asked for (only when comparing lines 6-10 against line 11).
ASKER
Now that's interesting. First thankyou to all for bearing with a clueless newbie.
my output.txt file has nothing in it.
I an running strawberry perl on windows 10. Im using Padre as an IDE.
I added print MATCH "test"; outside the while loop and that works
Is there something else I am doing wrong here ?
my output.txt file has nothing in it.
I an running strawberry perl on windows 10. Im using Padre as an IDE.
I added print MATCH "test"; outside the while loop and that works
Is there something else I am doing wrong here ?
Whether or not anything matches, each iteration of the loop should at least print "\n" to MATCH
so that would suggest that you never enter the loop, and @lines > 6 is never true.
Can you confirm that <INPUT> is reading what you think it is reading?
so that would suggest that you never enter the loop, and @lines > 6 is never true.
Can you confirm that <INPUT> is reading what you think it is reading?
ASKER
I can confirm there are two new line characters being passed to the text file
Only 2? That does seem odd. You should have 1 newline in output.txt per line in input.txt minus 5. If you put the below just before the while, what is in output.txt?
In this case, output.txt should exactly match input.txt. If it doesn't there's something wrong with the @lines = <INPUT>. If output.txt does match input.txt then replace the above with "my $line = 6;" and replace the print MATCH line within the loop with the below:
print MATCH join("\n", @lines), "\n";
exit;
In this case, output.txt should exactly match input.txt. If it doesn't there's something wrong with the @lines = <INPUT>. If output.txt does match input.txt then replace the above with "my $line = 6;" and replace the print MATCH line within the loop with the below:
print MATCH $line, "\t", join("\n", @match), "\n";
$line++;
What does output.txt look like now?
ASKER
Apologies for the delay.
I did as wilcoxon requested
the input was
1 2 3 4 5 6
7 8 9 10 11 12
1 2 3 4 5 6
4 6 3 5 1 6
8 7 9 4 3 8
4 3 2 1 2 1
9 8 2 5 1 4
5 3 6 4 3 8
the output became
test
1 2 3 4 5 6
7 8 9 10 11 12
1 2 3 4 5 6
4 6 3 5 1 6
8 7 9 4 3 8
4 3 2 1 2 1
9 8 2 5 1 4
5 3 6 4 3 8
The code that I use was
#!/usr/bin/perl
use strict;
use warnings;
# no need for $fil line since you hard-coded the input filename
open (INPUT,"<C:/Users/mickg/De sktop/Less ons/comp/i nput.txt") or die "can not open INPUT";
open (MATCH,">C:/Users/mickg/De sktop/Less ons/comp/o utput.txt" ) or die "can not open MATCH";
my @lines = map { chomp; $_ } <INPUT>;
print MATCH "test\n\n";
print MATCH join("\n", @lines), "\n";
exit;
while (@lines > 6) {
# check first 5 lines against the 6th line
my @match = grep { $_ eq $lines[5] } @lines[0..4];
my @nomatch = grep {$_ ne $lines[5] } @lines[0..4];
# do whatever you want with the matches and no-matches
print MATCH join("\n", @match), "\n";
# get rid of first line so next loop will be 2-6 and so on
shift @lines;
}
close (INPUT);
close (MATCH);
I did as wilcoxon requested
the input was
1 2 3 4 5 6
7 8 9 10 11 12
1 2 3 4 5 6
4 6 3 5 1 6
8 7 9 4 3 8
4 3 2 1 2 1
9 8 2 5 1 4
5 3 6 4 3 8
the output became
test
1 2 3 4 5 6
7 8 9 10 11 12
1 2 3 4 5 6
4 6 3 5 1 6
8 7 9 4 3 8
4 3 2 1 2 1
9 8 2 5 1 4
5 3 6 4 3 8
The code that I use was
#!/usr/bin/perl
use strict;
use warnings;
# no need for $fil line since you hard-coded the input filename
open (INPUT,"<C:/Users/mickg/De
open (MATCH,">C:/Users/mickg/De
my @lines = map { chomp; $_ } <INPUT>;
print MATCH "test\n\n";
print MATCH join("\n", @lines), "\n";
exit;
while (@lines > 6) {
# check first 5 lines against the 6th line
my @match = grep { $_ eq $lines[5] } @lines[0..4];
my @nomatch = grep {$_ ne $lines[5] } @lines[0..4];
# do whatever you want with the matches and no-matches
print MATCH join("\n", @match), "\n";
# get rid of first line so next loop will be 2-6 and so on
shift @lines;
}
close (INPUT);
close (MATCH);
That looks fine, please try this code now:
#!/usr/bin/perl
use strict;
use warnings;
# no need for $fil line since you hard-coded the input filename
open (INPUT,"<C:/Users/mickg/Desktop/Lessons/comp/input.txt") or die "can not open INPUT";
open (MATCH,">C:/Users/mickg/Desktop/Lessons/comp/output.txt") or die "can not open MATCH";
my @lines = map { chomp; $_ } <INPUT>;
print MATCH "test\n\n";
my $line = 6;
while (@lines > 6) {
# check first 5 lines against the 6th line
my @match = grep { $_ eq $lines[5] } @lines[0..4];
my @nomatch = grep {$_ ne $lines[5] } @lines[0..4];
# do whatever you want with the matches and no-matches
print MATCH $line, "\t", join("\n", @match), "\n";
$line++;
# get rid of first line so next loop will be 2-6 and so on
shift @lines;
}
close (INPUT);
close (MATCH);
ASKER
Thankyou
(I have added a little of my own code which does not affect anything, just so that I can follow and learn)
I tried that code with this input
1 2 3 4 5 6
7 8 9 10 11 12
1 2 3 4 5 6
4 6 3 5 1 6
8 7 9 4 3 8
4 3 2 1 2 1
9 8 2 5 1 4
5 3 6 4 3 8
and got this
test
test
test 1 inside while
6
test 2 inside while
7
then I tried with this input data
1 1 1 1 1 2
1 1 1 1 1 3
1 1 1 1 1 4
1 1 1 1 1 5
1 1 1 1 1 6
1 1 1 1 1 7
1 1 1 1 1 8
1 1 1 1 1 9
and got the same
test
test
test 1 inside while
6
test 2 inside while
7
Does it matter that I am using perl 5.26.0 ?
(I have added a little of my own code which does not affect anything, just so that I can follow and learn)
I tried that code with this input
1 2 3 4 5 6
7 8 9 10 11 12
1 2 3 4 5 6
4 6 3 5 1 6
8 7 9 4 3 8
4 3 2 1 2 1
9 8 2 5 1 4
5 3 6 4 3 8
and got this
test
test
test 1 inside while
6
test 2 inside while
7
then I tried with this input data
1 1 1 1 1 2
1 1 1 1 1 3
1 1 1 1 1 4
1 1 1 1 1 5
1 1 1 1 1 6
1 1 1 1 1 7
1 1 1 1 1 8
1 1 1 1 1 9
and got the same
test
test
test 1 inside while
6
test 2 inside while
7
Does it matter that I am using perl 5.26.0 ?
ASKER
i changed the code to
#!/usr/bin/perl
use strict;
use warnings;
# no need for $fil line since you hard-coded the input filename
open (INPUT,"<C:/Users/mickg/De sktop/Less ons/comp/i nput1.txt" ) or die "can not open INPUT";
open (MATCH,">C:/Users/mickg/De sktop/Less ons/comp/o utput.txt" ) or die "can not open MATCH";
my @lines = map { chomp; $_ } <INPUT>;
print MATCH "test\n" x 2;
my $line = 2;
my $test = 1;
while (@lines > 2) {
print MATCH "test $test inside while\n";
# check first 5 lines against the 6th line
my @match = grep { $_ eq $lines[1] } @lines[0..1];
my @nomatch = grep {$_ ne $lines[1] } @lines[0..1];
# do whatever you want with the matches and no-matches
print MATCH $line, "\t", join("\n", @match), "\n\n";
#no match information
#print MATCH $line, "\t", join("\n", @nomatch), "\n\n";
$line++;
$test++;
# get rid of first line so next loop will be 2-6 and so on
shift @lines;
}
close (INPUT);
close (MATCH);
and using input
1 1 1 1 1 2
1 1 1 1 1 3
1 1 1 1 1 4
1 1 1 1 1 5
1 1 1 1 1 6
1 1 1 1 1 7
1 1 1 1 1 8
1 1 1 1 1 9
got this
test
test
test 1 inside while
2 1 1 1 1 1 3
test 2 inside while
3 1 1 1 1 1 4
test 3 inside while
4 1 1 1 1 1 5
test 4 inside while
5 1 1 1 1 1 6
test 5 inside while
6 1 1 1 1 1 7
test 6 inside while
7 1 1 1 1 1 8
Now I am just not sure what the code is doing ? :)
#!/usr/bin/perl
use strict;
use warnings;
# no need for $fil line since you hard-coded the input filename
open (INPUT,"<C:/Users/mickg/De
open (MATCH,">C:/Users/mickg/De
my @lines = map { chomp; $_ } <INPUT>;
print MATCH "test\n" x 2;
my $line = 2;
my $test = 1;
while (@lines > 2) {
print MATCH "test $test inside while\n";
# check first 5 lines against the 6th line
my @match = grep { $_ eq $lines[1] } @lines[0..1];
my @nomatch = grep {$_ ne $lines[1] } @lines[0..1];
# do whatever you want with the matches and no-matches
print MATCH $line, "\t", join("\n", @match), "\n\n";
#no match information
#print MATCH $line, "\t", join("\n", @nomatch), "\n\n";
$line++;
$test++;
# get rid of first line so next loop will be 2-6 and so on
shift @lines;
}
close (INPUT);
close (MATCH);
and using input
1 1 1 1 1 2
1 1 1 1 1 3
1 1 1 1 1 4
1 1 1 1 1 5
1 1 1 1 1 6
1 1 1 1 1 7
1 1 1 1 1 8
1 1 1 1 1 9
got this
test
test
test 1 inside while
2 1 1 1 1 1 3
test 2 inside while
3 1 1 1 1 1 4
test 3 inside while
4 1 1 1 1 1 5
test 4 inside while
5 1 1 1 1 1 6
test 5 inside while
6 1 1 1 1 1 7
test 6 inside while
7 1 1 1 1 1 8
Now I am just not sure what the code is doing ? :)
Those two input files should not (and don't) return any matches. What do you get when running the code I provided on the original input.txt? Below is what I get (and what you should get).
For reference, I am running Strawberry Perl v5.26.1.
For reference, I am running Strawberry Perl v5.26.1.
test
6
7
8
9
10
11 1 2 3 4 5 6
12
13
14
15
What the above tells you is which line is being tested (6-15) and, if it matches, what matched.
ASKER
1 1 1 1 1 2
1 1 1 1 1 3
1 1 1 1 1 4
1 1 1 1 1 5
1 1 1 1 1 6
1 1 1 1 1 7
1 1 1 1 1 8
1 1 1 1 1 9
if the script compares all the numbers from lines 1 to 5, it should match 1 and not match 7
1 1 1 1 1 3
1 1 1 1 1 4
1 1 1 1 1 5
1 1 1 1 1 6
1 1 1 1 1 7
1 1 1 1 1 8
1 1 1 1 1 9
if the script compares all the numbers from lines 1 to 5, it should match 1 and not match 7
But I bet the problem is that you need to "chomp()" the input that you're reading from the file.
Open in new window