"Jürgen Exner" <jurgenex@hotmail.com> wrote in message news:<xQ2%b.6703$921.3436@nwrddc02.gnilink.net>...[color=blue]
>
rxl124@hehe.com wrote:[color=green]
> > 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[/color]
> The more idiomatic way nowadays is to
> use warnings;
>
> Also, strictures are missing
> use strict;
>[color=green]
> > open(FH, "files.txt") || die;[/color]
>
> 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";
>[color=green]
> > @yahoo = <FH>;
> > foreach $num (@yahoo){[/color]
>
> 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 (<FH>) {
>[color=green]
> > @real_num = substr($num ,91, 10);[/color]
>
> 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);
>[color=green]
> > }
> > open(NF, ">hanabbs2.log") || die;[/color]
>
> Again, you really should add some text and the actual error reason to the
> die() statement
>[color=green]
> > foreach $num (@real_num){
> > print NF "$num\n";[/color]
>
> 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[/color]
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?