in reply to Re: String comparison in an array
in thread String comparison in an array

thanks, but bare $_ also did not work printing them gives this : $elements[0] = "computer" $_ = computer

Replies are listed 'Best First'.
Re^3: String comparison in an array
by Eily (Monsignor) on Aug 08, 2018 at 13:03 UTC

    Ah right, it really looks like you are trying to read a csv file. You should consider using Text::CSV as it already handle special cases (values with quotes among values without, multiline values etc...). Beyond that you can follow hippo's advice

      here is the compilable code
      use strict; use warnings; use Data::Dump; use Text::CSV; use List::MoreUtils qw{ any }; my $first = 'computer'; my $last = 'printer'; my $in = 0; my @keys; open DATA, "types.txt" or die $!; while (<DATA>) { if (defined(my $key = (split)[-5])) { if ($key eq $first .. $key eq $last) { $in = 1; push @keys, $key; } elsif ($in && $key eq $last) { push @keys, $key; } else { $in = 0; } } } dd @keys; my $filename = "orders.csv"; my $csv = Text::CSV->new ( { binary => 1 } ); open my $fh, "<:encoding(Latin-1)", "orders.csv" or die "Cannot open $ +filename"; my $do_skip_test = 1; my %keyhash = map {($_=>1)} @keys; while(my $line = <$fh>) { if ($do_skip_test) { my (@fillarr1) = split ';',$line; #print $fillarr1[14]; #if ((grep { print $fillarr1[14], $_,"\n";$fillarr1[14] eq $_ +}@keys)) #need to change this loop to compare all array variables #if (($fillarr1[14] =~ /tm_C40_ANA_v1.12.0/) || ($fillarr1[14] + =~ /tm_C40_ANA_v1.14.0/)) #if (exists $fillarr1{$fillarr1[14]}) #if ($keyhash{$fillarr1[14]}) if (($fillarr1[14] =~ /$_/)@keys) { #$do_skip_test = 0; my (@fillarr) = split ';',$line; print "\n$fillarr[0]\t$fillarr[1]\t$fillarr[3]\t$fillarr[4 +]\t$fillarr[5]\t$fillarr[14]\t"; } else { next; } } } close($fh);

        There are quite a lot of things to correct here... I'll only point out some of thems because otherwise I'll just end up rewriting everything, and I'd rather leave you with your own code, but feel free to post the cleaned up version again if you want more advice.

        First, you might notice that you open two files using two different methods. The second one - where you use a variable with a $, and indicate that the file is for reading with < - is the one to use. The first one is especially bad because DATA is already used by perl to mean something else, so you should really avoid it.

        Second, a short and working example is supposed to be short. Remove all the commented lines that make it really hard to read your code (you should keep the useful comments, like # need to change this loop)

        Third, this:

        print "\n$fillarr[0]\t$fillarr[1]\t$fillarr[3]\t$fillarr[4]\t$fillarr[ +5]\t$fillarr[14]\t";
        does mostly the same as:
        print "\n", join "\t", @fillarr[0, 1, 3, 4, 5, 14];
        (Still not perfect, but better). Or Text::CSV might also help you write as a tab separated file.

        Fourth: you can remove the else { next; } because next basically means "stop working on that element", but there's no work to be done at that point (nothing after the else).

        Fifth: just including a module doesn't magically solve your problems. You can create your reader like this: my $csv = Text::CSV->new ({ binary => 1, sep_char => ";" }); and then use the parse() and fields() method to get your arrays. Or if you don't want to use Text::CSV, don't include it.