storing values from file into an array.-newbie

Discussion in 'Perl Misc' started by Go Perl, Oct 20, 2003.

  1. Go Perl

    Go Perl Guest

    I am having difficulties accessing these values outside this while
    loop. I am able to get only the last elements of the array outside.
    what should i do to store each of these elements c_array[1],...in an
    array and how to access them later ?

    while ($c_line=<CONTROL_FILE>) {
    chop($c_line);
    @c_array = split(/\t/, $c_line);
    if ($c_array[4] != "flag" && $c_array[4] == 1) {
    $bit_length = blength($c_array[3],$c_array[2]);
    $number_Par = $number_Par + 1;
    $BIT = $BIT + $bit_length;
    printf OUT_CONTROL ("%d\t%d\t%d\t%d\n", $c_array[1], $c_array[2],
    $c_array[3], $bit_length);

    }

    }
    Go Perl, Oct 20, 2003
    #1
    1. Advertising

  2. Go Perl

    Bob Walton Guest

    Go Perl wrote:

    > I am having difficulties accessing these values outside this while
    > loop. I am able to get only the last elements of the array outside.
    > what should i do to store each of these elements c_array[1],...in an
    > array and how to access them later ?
    >
    > while ($c_line=<CONTROL_FILE>) {
    > chop($c_line);
    > @c_array = split(/\t/, $c_line);
    > if ($c_array[4] != "flag" && $c_array[4] == 1) {
    > $bit_length = blength($c_array[3],$c_array[2]);
    > $number_Par = $number_Par + 1;
    > $BIT = $BIT + $bit_length;
    > printf OUT_CONTROL ("%d\t%d\t%d\t%d\n", $c_array[1], $c_array[2],
    > $c_array[3], $bit_length);
    >
    > }
    >
    > }
    >


    Well, every time through the loop you are reassigning @c_array, so of
    course only the last line's worth is in it when you are done. If you
    want to save that array in another array, add something like:

    push @output_array,[@c_array];

    in the vicinity of your printf statement.

    And BTW, you have an improper use of a relational operator.

    use warnings;

    would have told you that. Always always always use warnings; and use
    strict; during development! Something like:

    use warnings;
    use strict;
    my $c_line;
    my @c_array;
    my $bit_length;
    my $number_Par;
    my $BIT;
    my @output_array;
    while ($c_line=<DATA>) {
    chop($c_line);
    @c_array = split(/\t/, $c_line);
    if ($c_array[4] != "flag" && $c_array[4] == 1) {
    $bit_length = blength($c_array[3],$c_array[2]);
    $number_Par = $number_Par + 1;
    $BIT = $BIT + $bit_length;
    printf ("%d\t%d\t%d\t%d\n", $c_array[1], $c_array[2],
    $c_array[3], $bit_length);
    push @output_array,[@c_array];
    }
    }
    use Data::Dumper;
    print Dumper(\@output_array);
    sub blength{
    return 42; #or something
    }
    __END__
    9
    2
    3
    2
    1
    1
    2
    3
    4
    1
    3
    4
    5
    6
    1

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Oct 21, 2003
    #2
    1. Advertising

  3. Go Perl

    Tore Aursand Guest

    On Mon, 20 Oct 2003 15:51:02 -0700, Go Perl wrote:
    > I am having difficulties accessing these values outside this while
    > loop.


    Have you tried declaring '@c_value' outside the loop?


    --
    Tore Aursand <>
    Tore Aursand, Oct 21, 2003
    #3
  4. Go Perl <> wrote:
    > I am having difficulties accessing these values outside this while
    > loop. I am able to get only the last elements of the array outside.



    That's because you overwrite the old values each time through
    the loop.


    > what should i do to store each of these elements c_array[1],...in an
    > array and how to access them later ?



    Don't overwrite the old values each time through the loop. :)


    > while ($c_line=<CONTROL_FILE>) {
    > chop($c_line);



    That's how it was done over 7 years ago.

    Where are you learning your Perl from?

    chomp($c_line);


    > @c_array = split(/\t/, $c_line);



    Assignment overwrites whatever used to be there.

    You haven't said what exactly you want in @c_array.

    If you want _all_ of the values in @c_array:

    push @c_array, split(/\t/, $c_line);

    If you want a LoL in @c_array:

    push @c_array, [ split(/\t/, $c_line) ];


    > if ($c_array[4] != "flag" && $c_array[4] == 1) {

    ^^
    ^^

    You should always enable warnings when developing Perl code!

    You are using the wrong operator there.


    > $number_Par = $number_Par + 1;


    $number_Par++;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 21, 2003
    #4
    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. rajus
    Replies:
    12
    Views:
    564
    Al Balmer
    Oct 21, 2006
  2. Replies:
    3
    Views:
    345
    John Harrison
    Nov 9, 2005
  3. toton
    Replies:
    11
    Views:
    709
    toton
    Oct 13, 2006
  4. Jonathan Wood
    Replies:
    1
    Views:
    508
    Jonathan Wood
    Jun 2, 2008
  5. Jen
    Replies:
    3
    Views:
    223
    Robert Klemme
    Mar 23, 2011
Loading...

Share This Page