ginju75 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Wise Monks,

I am back :-)

I have the following code:

something...; something...; my @X = &values(); sub value{ open (R, "input") || die...; something..; something..; my @Y = ( ); return @Y; }

I need to populate @Y in the values subroutine based on some conditions.

For example, If something is true add an element to the array @Y.

hence Speaking, the array may sound like more of a dynamic array, which should get
loaded based on certain conditions (or based on contents of the input file), which can be provided by the user of the script file.

As you can see, the input file "input" can change. Based on its contents the array needs to be populated.

Help!

Thank you all for your wisdom.

The Learning Monk!

Replies are listed 'Best First'.
Re: How to populate an Array
by Masem (Monsignor) on Dec 06, 2001 at 00:46 UTC
    If you are reading data in line by line, you can add elements to arrays using push, eg push @y, $data;. This way, you can read in a line, determine if the data needs to be stored, and push or let it drop.

    If you are reading the data in en masse as an array to start, you can use the grep function to detemine which elements stay or go, eg @y = grep { $_ =~ /command/ } @data;

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Re: How to populate an Array
by sparkyichi (Deacon) on Dec 06, 2001 at 01:03 UTC
    I think this might be what you want to do:
    while (<R>){ if ($_ eq something...){ push (@Y, $_); } }

    I do not know how much you know, but what this does (I hope) is read the output from your open. Then the current line from the reading of the output is checked to see if it is the same as your variable (something...). It will push it onto the array at the end of the stack.

    I hope this was some help.

    Sparky

      Or, try perldoc grep...

      Perhaps something like:

      @Y = grep { $_ =~ /^magic/ } (<R>);

      That is the  grep EXPR LIST form of grep, which is exactly designed to search a list (here, the list of all records from filehandle <R>, split on the character or fixed length assigned to $/, q.v. in  perldoc perlvar) and return only the elements of the list (here, records in the file; probably lines, if you leave $/ at its default of "\n") that give "true" values for the EXPR.

      Oh, and you really should run through perldoc or at least the Llama...

      Hi Sparky, a quick thought but there are a variety of color schemes in use here at the monastery. Coloring your text based on the color scheme that you use will most likely render it unpleasant to read for those of us who use a different one.

      BTW a cheeky one liner equivelent of your code

      $_ eq 'something' && push @y,$_ while <R>;
      or a grep version
      my @y=grep {$_ eq 'something'}<R>
      Although an added chomp might be a good idea..

      Update
      Spoo. I didnt notice bakus comment to the same effect. One thing to remember though about the grep form is that it loads the whole thing into memory first, whereas the while form does not.

      :-)

      Yves / DeMerphq
      --
      This space for rent.

Re: How to populate an Array
by dragonchild (Archbishop) on Dec 06, 2001 at 00:56 UTC
    1. Turn your computer off.
    2. Get into your car.
    3. Go to your nearest Borders or Barnes & Nobles.
    4. Go to the Information Desk.
    5. Ask them where a copy of Learning Perl may be found.
    6. Go get a copy from the shelves.
    7. Purchase said copy.
    8. Get back into your car.
    9. Go home.
    10. Read that book.
      • Do not skip any pages.
      • Do not skip any words.
      • Take notes.
    11. After reading it, put your old program aside.
    12. Write new program.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        my $/50
      1. Never switch off *nix computers, even after installing software ;0).
      2. Save time and the planet, try Safari
      3. We all begin with questions like this; self-sufficiency is the real goal.

      --

      Brother Frankus.