Connecting Tech Pros Worldwide Forums | Help | Site Map

assigning back to an array

rxl124@hehe.com
Guest
 
Posts: n/a
#1: Jul 19 '05
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);



Gunnar Hjalmarsson
Guest
 
Posts: n/a
#2: Jul 19 '05

re: assigning back to an array


rxl124@hehe.com wrote:[color=blue]
> 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).[/color]

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

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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Gunnar Hjalmarsson
Guest
 
Posts: n/a
#3: Jul 19 '05

re: assigning back to an array


Gunnar Hjalmarsson wrote:[color=blue]
> rxl124@hehe.com wrote:[color=green]
>> However --> @real_num = substr($num, 91,10) is only pulling the
>> last line of the file(not the 100 other lines).[/color]
>
> That's because you are assigning @real_num at each iteration. Try:
>
> push @real_num, substr($num, 91,10);[/color]

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;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jürgen Exner
Guest
 
Posts: n/a
#4: Jul 19 '05

re: assigning back to an array


rxl124@hehe.com wrote:[color=blue]
> 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=blue]
> 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=blue]
> @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=blue]
> @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=blue]
> }
> open(NF, ">hanabbs2.log") || die;[/color]

Again, you really should add some text and the actual error reason to the
die() statement
[color=blue]
> 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


rxl124@hehe.com
Guest
 
Posts: n/a
#5: Jul 19 '05

re: assigning back to an array


"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]

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!!
rxl124@hehe.com
Guest
 
Posts: n/a
#6: Jul 19 '05

re: assigning back to an array


"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?
Gunnar Hjalmarsson
Guest
 
Posts: n/a
#7: Jul 19 '05

re: assigning back to an array


rxl124@hehe.com wrote:[color=blue]
> 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.[/color]

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.
[color=blue]
> Also, how come shift do not work in this case?[/color]

What do you mean by that?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

rxl124@hehe.com
Guest
 
Posts: n/a
#8: Jul 19 '05

re: assigning back to an array


Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<oam%b.84160$dP1.230822@newsc.telia.net>...[color=blue]
> rxl124@hehe.com wrote:[color=green]
> > 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.[/color]
>
> 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.
>[color=green]
> > Also, how come shift do not work in this case?[/color]
>
> What do you mean by that?[/color]


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.
Gunnar Hjalmarsson
Guest
 
Posts: n/a
#9: Jul 19 '05

re: assigning back to an array


rxl124@hehe.com wrote:[color=blue]
> Gunnar Hjalmarsson wrote:[color=green]
>> rxl124@hehe.com wrote:[color=darkred]
>>> 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.[/color]
>>
>> 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.[/color]
>
> here is my final program which is NOT working the way i want[/color]

<snip>
[color=blue]
> @yahoo = `sort -u hanabbs2.log`;[/color]

Why are you using a system command when there are suitable Perl
solutions available?
[color=blue]
> 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 ..[/color]

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;
[color=blue]
> 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.[/color]

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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

rxl124@hehe.com
Guest
 
Posts: n/a
#10: Jul 19 '05

re: assigning back to an array


Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<ycF0c.50847$mU6.205615@newsb.telia.net>...[color=blue]
> rxl124@hehe.com wrote:[color=green]
> > Gunnar Hjalmarsson wrote:[color=darkred]
> >> rxl124@hehe.com wrote:
> >>> 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.[/color]
> >
> > here is my final program which is NOT working the way i want[/color]
>
> <snip>
>[color=green]
> > @yahoo = `sort -u hanabbs2.log`;[/color]
>
> Why are you using a system command when there are suitable Perl
> solutions available?
>[color=green]
> > 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 ..[/color]
>
> 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;
>[color=green]
> > 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.[/color]
>
> That's a bad approach. Having strictures enabled is obviously most
> important when the program is being developed.[/color]


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
Gunnar Hjalmarsson
Guest
 
Posts: n/a
#11: Jul 19 '05

re: assigning back to an array


rxl124@hehe.com wrote:[color=blue]
> can you explain
>
> my %bigfile;
> $bigfile{$_} = '' while <PK>;
>
> are you putting key and null value?[/color]

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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Closed Thread