Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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 ( [id://1140476]=note: print w/replies, xml ) Need Help??


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

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
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
  • Comment on Re: 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^2: How do I select first string of a two dimensional array to compare to other values?
by joi1369 (Initiate) on Aug 31, 2015 at 01:05 UTC

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

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1140476]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-16 03:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found