assigning back to an array

Discussion in 'Perl' started by rxl124@hehe.com, Feb 25, 2004.

  1. Guest

    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);
     
    , Feb 25, 2004
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Feb 25, 2004
    #2
    1. Advertising

  3. Gunnar Hjalmarsson wrote:
    > 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
     
    Gunnar Hjalmarsson, Feb 25, 2004
    #3
  4. 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
     
    Jürgen Exner, Feb 25, 2004
    #4
  5. Guest

    "Jürgen Exner" <> wrote in message news:<xQ2%b.6703$>...
    > 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!!
     
    , Feb 26, 2004
    #5
  6. Guest

    "Jürgen Exner" <> wrote in message news:<xQ2%b.6703$>...
    > 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?
     
    , Feb 26, 2004
    #6
  7. 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
     
    Gunnar Hjalmarsson, Feb 26, 2004
    #7
  8. Guest

    Gunnar Hjalmarsson <> wrote in message news:<oam%b.84160$>...
    > 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.
     
    , Mar 1, 2004
    #8
  9. wrote:
    > Gunnar Hjalmarsson wrote:
    >> 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
     
    Gunnar Hjalmarsson, Mar 1, 2004
    #9
  10. Guest

    Gunnar Hjalmarsson <> wrote in message news:<ycF0c.50847$>...
    > wrote:
    > > Gunnar Hjalmarsson wrote:
    > >> 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
     
    , Mar 2, 2004
    #10
  11. 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
     
    Gunnar Hjalmarsson, Mar 2, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Lionheart
    Replies:
    27
    Views:
    405
    Jean-Hugues ROBERT
    May 4, 2004
  2. weston
    Replies:
    1
    Views:
    253
    Richard Cornford
    Sep 22, 2006
  3. bintom
    Replies:
    11
    Views:
    657
    Luca Risolia
    Oct 15, 2012
  4. Myth__Buster
    Replies:
    13
    Views:
    377
    Anand Hariharan
    Feb 26, 2013
  5. Myth__Buster
    Replies:
    0
    Views:
    253
    Myth__Buster
    Feb 1, 2013
Loading...

Share This Page