Process each input record as an array of elements

Discussion in 'Perl' started by Dmitry, Oct 13, 2003.

  1. Dmitry

    Dmitry Guest

    Hello everyone,

    I have a really simple question here:

    I have a plain space delimited file that I want to read with WHILE
    loop 1 line at the time and process each input record as an array of
    elements. Then, on the next iteration of the loop, clean up the array
    and populate it with contents of the next input record. Each input
    record consists of 7 fields / elements separated by spaces. The reason
    I need this is because I have to switch FIRST and LAST elements of the
    record by places. For example:

    Before: aaa bbb ccc ddd eee fff ggg

    After: ggg bbb ccc ddd eee fff aaa

    I am having trouble assigning contents of each input record to an
    array.

    If I can do that, then I will be use POP and UNSHIFT Perl functions to
    switch the elements.

    Here is what I have so far:

    my $pop_element = ‘';
    my @print_array = ();

    open (OLD, "< $fname_out") || die "Cannot open OLD file
    $fname_out!";
    open (NEW, "> $temp") || die "Cannot open NEW file $temp!";

    while (<OLD>) {
    @print_array = "$_\n";
    $pop_element = pop ( @print_array );
    unshift ( @print_array, $pop_element );
    $_ = @print_array;
    print NEW "$_\n" || die "Cannot write NEW file $temp!";
    }
    close (OLD) || die "Cannot close OLD file $fname_out!";
    close (NEW) || die "Cannot close NEW file $temp!";
    rename ( $temp, $fname_out ) || die "Cannot rename NEW file $temp
    to $fname_out!";
    }

    Any suggestions would be greatly appreciated.

    Thanks,

    Dmitry.
     
    Dmitry, Oct 13, 2003
    #1
    1. Advertising

  2. Dmitry

    Roy Johnson Guest

    This newsgroup is defunct. Use comp.lang.perl.misc instead.

    This should do more or less what you want (season to taste):

    while (<>) {
    @elems = split; # split on whitespace
    @elems[0,-1] = @elems[-1,0]; # swap first and last elements
    print join(',', @elems), "\n";
    }
     
    Roy Johnson, Oct 13, 2003
    #2
    1. Advertising

  3. Dmitry

    Dmitry Guest

    Thanks Roy, it worked perfectly!

    I did, however post the same question to per.msc group, just curious
    about other possible alternatives.

    Best regards,

    Dmitry.
     
    Dmitry, Oct 14, 2003
    #3
    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. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    441
    Tjerk Wolterink
    Nov 3, 2004
  2. Pat Maddox
    Replies:
    6
    Views:
    162
    Marcin Mielżyński
    Jan 20, 2006
  3. Dmitry
    Replies:
    5
    Views:
    95
    Dmitry
    Oct 15, 2003
  4. PerlFAQ Server
    Replies:
    0
    Views:
    149
    PerlFAQ Server
    Jan 26, 2011
  5. PerlFAQ Server

    FAQ 6.14 How do I process each word on each line?

    PerlFAQ Server, Apr 8, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    162
    PerlFAQ Server
    Apr 8, 2011
Loading...

Share This Page