Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How do I select first string of a two dimensional array to compare to other values?

by joi1369 (Initiate)
on Aug 30, 2015 at 23:07 UTC ( #1140475=perlquestion: print w/replies, xml ) Need Help??

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

I have an array after splitting a text document into three different columns. I'm trying to retrieve the first value in the first column to compare against other values in that column and then print out that initial value but, when I use the following code, it prints all of the first column. I've tried several different methods of doing this but I think I'm just not understanding something. The code is below and any help is greatly appreciated. Thanks in advance! 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 1424622060, 2015-02-22 16:21:00, 4300.300

Script-

#!/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_value = @splits[0]; print "$first_value\n"; }

Wanted output-

1424621700

Replies are listed 'Best First'.
Re: How do I select first string of a two dimensional array to compare to other values?
by BrowserUk (Pope) on Aug 30, 2015 at 23:19 UTC
      That was completely my fault. I was trying to clarify once I had put it on the site and then ended up forgetting to change the other one.
Re: How do I select first string of a two dimensional array to compare to other values?
by choroba (Archbishop) on Aug 30, 2015 at 23:17 UTC
    Hi joi1369, welcome to the Monastery!

    I don't understand your question. It's usually better to include sample input data (within <code> tags) and expected output.

    If you want to work with the first line only, don't use while. If you want to compare two string values, you should use eq, ne, or index, or maybe a regex: $value =~ /\Q$important/.

    Update: example script added:

    #!/usr/bin/perl use warnings; use strict; my $first_line = <DATA>; my $important = (split ' ', $first_line)[0]; while (<DATA>) { # Process the remaining lines. my $value = (split)[0]; print if $value eq $important; } __DATA__ john chevrolet usa hans bmw germany ivan lada russia john talbot uk
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      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.

        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:  <%-{-{-{-<

Re: How do I select first string of a two dimensional array to compare to other values?
by AnomalousMonk (Bishop) on Aug 30, 2015 at 23:30 UTC
    I have a two dimensional array after splitting a text document into three different columns.

    You split with
        my @splits = split (',', $line);
    to the array  @splits which has one dimension. On what do you base your assertion that you have a two-dimensional array?

    You assign the first element of  @splits to  $first_value and then print  $first (which is never defined) immediately afterward. How does | can that work with strict enabled?

    Can you supply a short, representative example or two of your input data? We could then use this for testing various alternatives.

    Also, please see the Short, Self Contained, Correct (Compilable), Example discussion.


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

      I said this above as well but, the non-defined "$first" was completely my fault. I was trying to clarify the code a bit once I had put it on the site and then ended up forgetting to change the other one. The input looks like this-

      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

      And I'm trying to get the value "1424621700" returned after the file is split at the commas.

        Please see above.


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

Re: How do I select first string of a two dimensional array to compare to other values?
by 1nickt (Abbot) on Aug 31, 2015 at 05:41 UTC

    #!/usr/bin/perl use strict; use warnings; use feature qw/ say /; my $first = ( split ',', <DATA> )[0]; say $first; __DATA__ 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 1424622060, 2015-02-22 16:21:00, 4300.300
    $ perl 1140499.pl 1424621700 $

    ++james28909 for making me see it more simply.

    The way forward always starts with a minimal test.
Re: How do I select first string of a two dimensional array to compare to other values?
by 1nickt (Abbot) on Aug 31, 2015 at 01:42 UTC

    Hi there, it looks like you want to do this:

    #!/usr/bin/perl use strict; use warnings; my $file = '1140475.txt';; open my $raw, '<', $file or die "open: $!"; open my $out, '>', "out_$file" or die "close: $!"; while ( my $line = <$raw> ) { chomp( $line ); my @splits = split ( ',', $line ); print $out "First line, first field: $splits[0]\n"; last; } close $out or die "close: $!"; __END__
    Output:
    $ cat out_1140475.txt First line, first field: 1424621700

    But you should use a module if you can. A lot of people like Path::Tiny because it does the file opening, reading, and chomping, for you. In the example below I use split() on the first element of the array of lines returned by Path::Tiny::path() (there's actually only one line since I set the 'count' attribute), and then I assign the first element of the result of split() to $first.

    #!/usr/bin/perl use strict; use warnings; use Path::Tiny qw/ path /; my $file = '1140475.txt'; my $first = ( split ',', (path($file)->lines( {chomp => 1, count => 1} + ))[0] )[0]; print "$first\n"; __END__
    Output:
    $ perl 1140475-2.pl 1424621700 $
    Hope this helps!

    The way forward always starts with a minimal test.
Re: How do I select first string of a two dimensional array to compare to other values?
by AnomalousMonk (Bishop) on Aug 31, 2015 at 01:38 UTC

    BTW: Your OP was changed to add some example data. The conventional practice of this site is to also add a note to any altered node to indicate what was changed or deleted. Please see How do I change/delete my post?.


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

Re: How do I select first string of a two dimensional array to compare to other values?
by james28909 (Deacon) on Aug 31, 2015 at 05:13 UTC
    use strict; use warnings; print $_ for my ($data) = split ',', <DATA>; __DATA__ 123,456 234,567 345,456 #outputs: '123'

      hi james28909,
      if split operator can take a regex, why not just:

      use strict; use warnings; print split /,.+/, <DATA>; __DATA__ 123,456 234,567 345,456
      Just a flash of thought! :)

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me
Re: How do I select first string of a two dimensional array to compare to other values?
by poj (Abbot) on Aug 31, 2015 at 09:12 UTC

    Here is an example using the first line of data compared to the other values. Note, it skips lines containing ! which may precede the first data line

    #!/usr/bin/perl use strict; use warnings; my $file = $ARGV[0]; open (RAW, '<', "./$file") or die "Can't open $file for read: $!"; my @first; my $line_count = 0; while (<RAW>){ chomp; next if /!/; my @col = split /,/,$_; if (++$line_count == 1){ @first = @col; print "\n--------------------------------\n"; print "Start $first[1]\n"; print sprintf "%10s %10s %10s\n",'+secs','value','change'; print sprintf "%10s %10s %10s\n",'-'x10,'-'x10,'-'x10; } print sprintf "%10d %10.3f %10.3f\n", ($col[0] - $first[0]), $col[2], ($col[2] - $first[2]); }

    Outputs

    -------------------------------- Start 2015-02-22 16:15:00 +secs value change ---------- ---------- ---------- 0 4294.700 0.000 60 4289.700 -5.000 120 4299.800 5.100 180 4302.800 8.100 240 4296.900 2.200 300 4301.000 6.300 360 4300.300 5.600
    poj
Re: How do I select first string of a two dimensional array to compare to other values?
by sundialsvc4 (Abbot) on Aug 31, 2015 at 01:34 UTC

    It certainly looks to me that the print statement is within the while loop, such that it will print every value retrieved from the file-handle.   You do need the loop, presumably, because you have a next if test to exclude certain lines.   But, once you reach the point where the print statement is now located, you probably (instead ...) want to use a last statement to force a premature end to the while-loop:   the first value is now in $first_value, and you do not wish to continue reading.   After the } symbol which ends the while-loop, you can, if you like, put the print statement to see it.

    If, later on in the program, you start another while loop using the same file-handle, it should resume reading the file where it left off.   (Unless you use fseek() to reposition it.)

    - - -

    (In the future, it will help us considerably if you’ll routinely tell us more information:   a snippet of what the file looks like, and a brief description of what output you are seeing and why you feel that the output is wrong.)   Meanwhile, Welcome to the Monastery!!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1140475]
Front-paged by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2021-05-15 11:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (150 votes). Check out past polls.

    Notices?