By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,659 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,659 IT Pros & Developers. It's quick & easy.

assigning back to an array

P: n/a
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);
Jul 19 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
rx****@hehe.com wrote:
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);

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

Jul 19 '05 #2

P: n/a
Gunnar Hjalmarsson wrote:
rx****@hehe.com wrote:
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);


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

Jul 19 '05 #3

P: n/a
rx****@hehe.com wrote:
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 (<FH>) {
@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
Jul 19 '05 #4

P: n/a
"Jürgen Exner" <ju******@hotmail.com> wrote in message news:<xQ*****************@nwrddc02.gnilink.net>...
rx****@hehe.com wrote:
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 (<FH>) {
@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


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!!
Jul 19 '05 #5

P: n/a
"Jürgen Exner" <ju******@hotmail.com> wrote in message news:<xQ*****************@nwrddc02.gnilink.net>...
rx****@hehe.com wrote:
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 (<FH>) {
@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

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?
Jul 19 '05 #6

P: n/a
rx****@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.
Also, how come shift do not work in this case?


What do you mean by that?

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

Jul 19 '05 #7

P: n/a
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<oa********************@newsc.telia.net>...
rx****@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.
Also, how come shift do not work in this case?


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.
Jul 19 '05 #8

P: n/a
rx****@hehe.com wrote:
Gunnar Hjalmarsson wrote:
rx****@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.


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


<snip>
@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.

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

Jul 19 '05 #9

P: n/a
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<yc********************@newsb.telia.net>...
rx****@hehe.com wrote:
Gunnar Hjalmarsson wrote:
rx****@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.


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


<snip>
@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.

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
Jul 19 '05 #10

P: n/a
rx****@hehe.com wrote:
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.

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

Jul 19 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.