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

I thought I posted this successfully, but goofed up. Many thanks for those that answered the last question - quite helpful. What I'm trying to do now (w/ the same script) is split a line, and take the 0th & 9th fields and make them one value in an array. For simplicity sake, the 0th value has a time stamp, and the 9th has the ID. The file I'm digging through isn't in chrnological order, unfortunately, so I want it to look like
ID#TIMESTAMP.
The following...
while ( $line = <IN> ) { push @array, ( split /\s+/, $line )[9, 0]; }
produces
ID# TIMESTAMP
Anyway to put them on the same line?

Replies are listed 'Best First'.
(jeffa) Re: Splitting two columns into an array
by jeffa (Bishop) on Mar 14, 2003 at 03:43 UTC
    Instead of using split, you could capture the parts into $1 and $2 via a regex:
    use strict; use warnings; use Data::Dumper; my @array; while (<DATA>) { my ($time,$id) = /([\d:.]+)\D+(\d+)/; push @array, "$id:$time"; } print Dumper \@array; __DATA__ 00:00:00.20 - blah blah perl him me it they 0223503 blah blah blah 00:00:00.30 - blah blah perl him me it they 0223504 blah blah blah
    UPDATE:
    OK, i see what's going on. You split up the line and get back the parts you want, but when you push them to the array, you are pushing a list, not a single value. Try this:
    while (<IN>) { push @array, join('#',(split)[9, 0]); }
    or more cleverly:
    my @array = map join('#',(split)[9, 0]), <IN>;
    NOTE:
    This reply was originally posted to (DUPE) Splitting two columns into an array. The last two code snippets above joined on a colon (:), which i changed to a pound sign (#) for this question.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Another array thing - split two fields into one
by Zaxo (Archbishop) on Mar 14, 2003 at 03:49 UTC

    You just need to join them before pushing:

    while ( <IN> ) { push @array, join '', (split /\s+/)[9, 0]; }
    I modified that to use the pronoun $_.

    After Compline,
    Zaxo

Re: (DUPE) Splitting two columns into an array
by Cody Pendant (Prior) on Mar 14, 2003 at 06:08 UTC
    push (@array, join('',(split /\s+/,$line)[9, 0]));

    --
    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
Re: Another array thing - split two fields into one
by Coruscate (Sexton) on Mar 14, 2003 at 20:51 UTC

    Note that if you are handling the last field in the row, you might also want to be using chomp() to clear the newline from the line that is being passed to you. Something like while ( chomp(my $line = <IN>) ) { } will do it :)


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

      Something like while ( chomp(my $line = <IN>) ) { } will do it :)

      chomp() returns the number of chopped of chars. So this is not something that's recommended to do.

      ihb