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
Re: How do I select first string of a two dimensional array to compare to other values?
by BrowserUk (Patriarch) on Aug 30, 2015 at 23:19 UTC
|
This: my $first_value is not the same variable as this: print "$first\n";
If you had actually used strict and warnings in your code; they would have told you that.
| [reply] [d/l] [select] |
|
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.
| [reply] |
Re: How do I select first string of a two dimensional array to compare to other values?
by choroba (Cardinal) 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
| [reply] [d/l] [select] |
|
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. | [reply] [d/l] [select] |
|
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: <%-{-{-{-<
| [reply] [d/l] [select] |
|
|
|
Re: How do I select first string of a two dimensional array to compare to other values?
by AnomalousMonk (Archbishop) 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: <%-{-{-{-<
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] |
|
Please see above.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] |
Re: How do I select first string of a two dimensional array to compare to other values?
by 1nickt (Canon) 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.
| [reply] [d/l] [select] |
Re: How do I select first string of a two dimensional array to compare to other values?
by 1nickt (Canon) 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.
| [reply] [d/l] [select] |
Re: 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: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: <%-{-{-{-<
| [reply] [d/l] |
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'
| [reply] [d/l] |
|
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
| [reply] [d/l] |
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
| [reply] [d/l] [select] |
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!!
| |
|
|