in reply to Re: How do I select first string of a two dimensional array to compare to other values?
in thread How do I select first string of a two dimensional array to compare to other values?

Thank you for the welcome! Let me try to clarify things a bit. This is the input-

1424621700, 2015-02-22 16:15:00, 4294.700 1424621760, 2015-02-22 16:16:00, 4289.700 1424621820, 2015-02-22 16:17:00, 4299.800 1424621880, 2015-02-22 16:18:00, 4302.800 1424621940, 2015-02-22 16:19:00, 4296.900 1424622000, 2015-02-22 16:20:00, 4301.000

I'm trying to run it through this script (fixed from above)-

#!/usr/bin/perl use strict; use warnings; my $file = $ARGV[0]; open (RAW, "./$file") or die "Can't open $file for read: $!"; open (OUT, ">./OUTPUT_$file") or die "Can't open Output_$file: $!"; while(<RAW>) { my $line = $_; chop($line); next if ($line =~ /!/); my @splits = split (',', $line); my $first_line = $splits; print "$first_line\n"; }

I am trying to get the string "1424621700" assigned to "$first_line" and then printed. Sorry about the disorganization. I'm new to Perl and haven't quite gotten the hang of it yet.

  • Comment on Re^2: How do I select first string of a two dimensional array to compare to other values?
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: How do I select first string of a two dimensional array to compare to other values?
by AnomalousMonk (Archbishop) on Aug 31, 2015 at 01:11 UTC

    So how does your current code fail to serve your purposes? An alternative:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $line = qq{1424621700, 2015-02-22 16:15:00, 4294.700\n}; chomp $line; print qq{'$line'}; ;; my ($first) = split m{ , \s* }xms, $line; print qq{'$first'}; " '1424621700, 2015-02-22 16:15:00, 4294.700' '1424621700'

    Update: Ah, I see it now:
        my $first_line = $splits;
    and again, how can this compile with strict enabled? You probably want something like the
        my $first_line = $splits[0];
    statement you had before.


    Give a man a fish:  <%-{-{-{-<

      Rather than printing out only 1424621700, it prints out the entire first column like so-

      1424621700 1424621760 1424621820 1424621880 1424621940 1424622000 1424622060 1424622120 1424622180 1424622240 1424622300

      Again, thank you so much for the help

        Or perhaps you mean that you want to compare the first field of the first line in the file against the first fields of all subsequent lines? In that case, perhaps something like (also untested):

        ... open file handles, etc. ... defined(my $first_line = <RAW>) or die "reading raw input: $!"; chomp $first_line; my ($first_line_first_field) = split m{ , \s* }xms, $first_line; while (defined(my $following_line = <RAW>)) { chomp $following_line; my ($following_line_first_field) = split m{ , \s* }xms, $following +_line; compare($first_line_first_field, $following_line_first_field); } close RAW or die "closing raw handle: $!";
        (Of course, you have to write the  compare() function!)

        Update: Added error check to original
            my $first_line = <RAW>;
        statement above to make it
            defined(my $first_line = <RAW>) or die "reading raw input: $!";


        Give a man a fish:  <%-{-{-{-<

        But your code above loops through the entire file of the  RAW input handle (assuming the syntax error is fixed), processing and printing every line. (BTW: Nothing is ever written to the  OUT handle.)

        If you want to process only the first line of RAW, maybe something like (untested):

        my $first_line = <RAW>; close RAW or die "closing raw input: $!"; chomp $first_line; my $first_field = split m{ , \s* }xms, $first_line; print "$first_field\n";


        Give a man a fish:  <%-{-{-{-<