assigning back to an array

R

rxl124

I have files that I only need one field that I need to grep it out and
I am trying to assign that to another file.

(It happens that one field that I am looking for has some other character
that I dont need so I am pulling it out w/ substr commands).

However --> @real_num = substr($num, 91,10) is only pulling the last line
of the file(not the 100 other lines).

Can someone please tell me what I am doing wrong here?

Please Please help as I am driving myself mad on this one.


#!/usr/bin/perl -w

open(FH, "files.txt") || die;
@yahoo = <FH>;
foreach $num (@yahoo){
@real_num = substr($num ,91, 10);
}
open(NF, ">hanabbs2.log") || die;
foreach $num (@real_num){
print NF "$num\n";
}
close(NF);
 
G

Gunnar Hjalmarsson

I have files that I only need one field that I need to grep it out
and I am trying to assign that to another file.

(It happens that one field that I am looking for has some other
character that I dont need so I am pulling it out w/ substr
commands).

However --> @real_num = substr($num, 91,10) is only pulling the
last line of the file(not the 100 other lines).

That's because you are assigning @real_num at each iteration. Try:

push @real_num, substr($num, 91,10);
 
G

Gunnar Hjalmarsson

Gunnar said:
That's because you are assigning @real_num at each iteration. Try:

push @real_num, substr($num, 91,10);

Btw, you don't need the variables. And why didn't you enable strictures?

#!/usr/bin/perl -w
use strict;

open FH, '< files.txt' or die $!;
open NF, '> hanabbs2.log' or die $!;
print NF (substr $_, 91, 10), "\n" while <FH>;
close NF;
close FH;
 
J

Jürgen Exner

I have files that I only need one field that I need to grep it out and
I am trying to assign that to another file.

(It happens that one field that I am looking for has some other
character that I dont need so I am pulling it out w/ substr commands).

However --> @real_num = substr($num, 91,10) is only pulling the last
line of the file(not the 100 other lines).

Can someone please tell me what I am doing wrong here?

Please Please help as I am driving myself mad on this one.


#!/usr/bin/perl -w
The more idiomatic way nowadays is to
use warnings;

Also, strictures are missing
use strict;
open(FH, "files.txt") || die;

You may want to add a message to your die() statement with an explanation of
the error:
open(FH, "files.txt") or die "Cannot open files.txt because $!\n";
@yahoo = <FH>;
foreach $num (@yahoo){

That doesn't make sense. Why are you reading the whole file into an array
when in the very next statement you are looping through that array (and
don't use the array anywhere else). Better use the idiomatic loop

while ( said:
@real_num = substr($num ,91, 10);

In each iteration you are re-assigning @real_num, throwing away whatever
data was in there before. Probably you meant

push @real_num, substr($_, 91, 10);
}
open(NF, ">hanabbs2.log") || die;

Again, you really should add some text and the actual error reason to the
die() statement
foreach $num (@real_num){
print NF "$num\n";

Oh, that's all you do with @real_num?
Then why not open both file handles up front and process the file line by
line, printing each line as you process the line?

open(FH, "files.txt") or die "Cannot open files.txt because $!\n";
open(NF, ">hanabbs2.log") or die "Cannot open hanabbs2.log because $!\n";

while (<FH>) {
print NF substr($_, 91, 10);
}
close FH;
close NF;

jue
 
R

rxl124

Jürgen Exner said:
The more idiomatic way nowadays is to
use warnings;

Also, strictures are missing
use strict;


You may want to add a message to your die() statement with an explanation of
the error:
open(FH, "files.txt") or die "Cannot open files.txt because $!\n";


That doesn't make sense. Why are you reading the whole file into an array
when in the very next statement you are looping through that array (and
don't use the array anywhere else). Better use the idiomatic loop



In each iteration you are re-assigning @real_num, throwing away whatever
data was in there before. Probably you meant

push @real_num, substr($_, 91, 10);


Again, you really should add some text and the actual error reason to the
die() statement


Oh, that's all you do with @real_num?
Then why not open both file handles up front and process the file line by
line, printing each line as you process the line?

open(FH, "files.txt") or die "Cannot open files.txt because $!\n";
open(NF, ">hanabbs2.log") or die "Cannot open hanabbs2.log because $!\n";

while (<FH>) {
print NF substr($_, 91, 10);
}
close FH;
close NF;

jue

You guys are the best. Last night, I was up until wee hours trying to
figure this out. I am going to give these a try and let you know
sometime during the course of night.

Thank you!!
 
R

rxl124

Jürgen Exner said:
The more idiomatic way nowadays is to
use warnings;

Also, strictures are missing
use strict;


You may want to add a message to your die() statement with an explanation of
the error:
open(FH, "files.txt") or die "Cannot open files.txt because $!\n";


That doesn't make sense. Why are you reading the whole file into an array
when in the very next statement you are looping through that array (and
don't use the array anywhere else). Better use the idiomatic loop



In each iteration you are re-assigning @real_num, throwing away whatever
data was in there before. Probably you meant

push @real_num, substr($_, 91, 10);


Again, you really should add some text and the actual error reason to the
die() statement


Oh, that's all you do with @real_num?
Then why not open both file handles up front and process the file line by
line, printing each line as you process the line?

open(FH, "files.txt") or die "Cannot open files.txt because $!\n";
open(NF, ">hanabbs2.log") or die "Cannot open hanabbs2.log because $!\n";

while (<FH>) {
print NF substr($_, 91, 10);
}
close FH;
close NF;

jue


Thank you again for both of your kind help..
I been studying for perl for about month now and I been reading and
coding, but when it comes to actually applying what you know is
totally different story.

At any rate, below is my final 2

#!/usr/bin/perl -w
use strict;
open(FH, "<files.txt") || die;
open(NF, ">hanabbs2.log") || die;
while (<FH>) {
print NF substr($_, 91, 10), "\n";
}
close FH;
close NF;

and (for learning purpose)

#!/usr/bin/perl -w

open(FH, "alarm.log") || die;
while (<FH>) {
push @real_num , substr($_ ,91, 10);
}
open(NF, ">hanabbs2.log") || die;
foreach $num (@real_num){
print NF "$num\n";
}
close(NF);

Of course this is just half part of my complete program(I will run
into next headache of having to actually compare this
file(hanabbs2.log) and compare to another file(complete.log) and print
out what do not grep from complete.log
is there syntax as grep ! hanabbs2.log complete.log
I will have to further study on that.

Also, how come shift do not work in this case?
 
G

Gunnar Hjalmarsson

I will run into next headache of having to actually compare this
file(hanabbs2.log) and compare to another file(complete.log) and
print out what do not grep from complete.log
is there syntax as grep ! hanabbs2.log complete.log
I will have to further study on that.

One approach you may want to consider is storing the strings in
complete.log as keys in a hash, and then test whether respective hash
key exists while looping through hanabbs2.log.
Also, how come shift do not work in this case?

What do you mean by that?
 
R

rxl124

Gunnar Hjalmarsson said:
One approach you may want to consider is storing the strings in
complete.log as keys in a hash, and then test whether respective hash
key exists while looping through hanabbs2.log.


What do you mean by that?


here is my final program which is NOT working the way i want

#!/usr/bin/perl -w

#use strict;

open(FH, "< $ARGV[0]") || die;
open(NF, ">hanabbs2.log") || die;
open(PK, "<page.num") || die;
open(EF, "+>existfile") || die;
open(KF, "+>nexistfile") || die;

while (<FH>) {
print NF substr($_, 91, 10), "\n";
}
close(NF);

@yahoo = `sort -u hanabbs2.log`;
my @bigfile = <PK>;
close PK;

WID: foreach $yahoos (@yahoo) {
YAH: foreach $big_file (@bigfile) {
if ($yahoos =~ /$big_file/) {
print EF "$yahoos";
next WID;
} else {
next YAH;
}
print KF "$yahoos";
}
}

above program appears to work..... but it's not..
and i think there is grammar problems. or logic problem in last
foreach ..
i am not able to pin point at this point..
I think where i fail is that if $yahoos doesn't match $big_file, I
want that to
loop through each word in @bigfile array and if it runs out without
matching, i want that $yahoos to be writtent KF and then go back to
WID.

Please comment on it as you see fit
PS:for now, I commented back strict so that I can just get the program
running and then once it works, I will go back to turn back on strict
and add more necessary lexical variable.

Thanks again in advance.
 
G

Gunnar Hjalmarsson

here is my final program which is NOT working the way i want

@yahoo = `sort -u hanabbs2.log`;

Why are you using a system command when there are suitable Perl
solutions available?
my @bigfile = <PK>;
close PK;

WID: foreach $yahoos (@yahoo) {
YAH: foreach $big_file (@bigfile) {
if ($yahoos =~ /$big_file/) {
print EF "$yahoos";
next WID;
} else {
next YAH;
}
print KF "$yahoos";
}
}

above program appears to work..... but it's not.. and i think there
is grammar problems. or logic problem in last foreach ..

Well, I suppose that nothing ever gets printed to the KF filehandle,
right?

Didn't you like my hash idea? Applied to the latest code, I meant that
you could do something like this instead:

my %bigfile;
$bigfile{$_} = '' while <PK>;
close PK;
open NF, '< hanabbs2.log' or die $!;
while (<NF>) {
if (exists $bigfile{$_}) {
print EF;
} else {
print KF;
}
}
close NF;
close EF;
close KF;
PS:for now, I commented back strict so that I can just get the
program running and then once it works, I will go back to turn back
on strict and add more necessary lexical variable.

That's a bad approach. Having strictures enabled is obviously most
important when the program is being developed.
 
R

rxl124

Gunnar Hjalmarsson said:
Why are you using a system command when there are suitable Perl
solutions available?


Well, I suppose that nothing ever gets printed to the KF filehandle,
right?

Didn't you like my hash idea? Applied to the latest code, I meant that
you could do something like this instead:

my %bigfile;
$bigfile{$_} = '' while <PK>;
close PK;
open NF, '< hanabbs2.log' or die $!;
while (<NF>) {
if (exists $bigfile{$_}) {
print EF;
} else {
print KF;
}
}
close NF;
close EF;
close KF;


That's a bad approach. Having strictures enabled is obviously most
important when the program is being developed.


You are right..... below file does exactly what i need to do

#!/usr/bin/perl -w

#use strict;

open(FH, "< $ARGV[0]") || die;
open(NF, ">hanabbs2.log") || die;
open(PK, "<patrice.us") || die;
open(EF, "+>existfile") || die;
open(KF, "+>nexistfile") || die;

while (<FH>) {
print NF substr($_, 91, 10), "\n";
}
close(NF);

@yahoo = `sort -u hanabbs2.log`;
sleep(3);

my %bigfile;
$bigfile{$_} = '' while <PK>;
close PK;
for (@yahoo) {
if (exists $bigfile{$_}) {
print EF;
} else {
print KF;
}
}
close NF;
close EF;
close KF;

I am not too comfortable w/ hashes as of yet and I am gonna go back
and try to make my program work as well. But thank you as it's working
out great.

actually,

can you explain

my %bigfile;
$bigfile{$_} = '' while <PK>;

are you putting key and null value?

I will post my program as soon as it works......

I am using my sort unix command cause perldoc -q uniq solution didn't
work for me for some reason
 
G

Gunnar Hjalmarsson

can you explain

my %bigfile;
$bigfile{$_} = '' while <PK>;

are you putting key and null value?

Yes. The values are not important, but we use the keys to take
advantage of the ability to look up keys in a hash.
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top