473,473 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

assigning back to an array

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
10 2307
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
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
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
"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
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dariusz | last post by:
I need some help with trying to assign a string to a variable name, whilst the variable name itself is read out of an array. The code below is part of a routine in a database which checks if the...
1
by: Jenny | last post by:
Hi, Can I create an array of tags by assigning same name to these tags? For example, I have two <p> tags with the same name t1. But document.all.b.value=document.all.t.length does not...
14
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0;...
4
by: James | last post by:
Just learning C#. What's the easiest way to assign numbers 1-10 randomly to an array? Basically I just want to take the numbers 1-10 and arrange them randomly in slots 0-9 of an array. Thanks
2
by: ryoung | last post by:
I receive the following error when I attempt to assign values to a web service array. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. ...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
8
by: mast2as | last post by:
I almost apologize to ask this question but I have been starting at this code for a bit of time and don't understand what's wrong with it. I am not arguing about the fact it's good or not coding,...
37
by: miken32 | last post by:
In PHP, if a function returns an array it's fairly common to capture its return values like this: <?php list($foo, $bar, $baz) = some_function_that_return_an_array(); ?> In Javascript, would...
3
by: Chris Saunders | last post by:
My C skills are rather meager so forgive me if I do not express my question clearly enough. Here is a struct that is declared in Windows: typedef struct _REPARSE_GUID_DATA_BUFFER { DWORD...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.