Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

How to compare elements within an array

by doubleqq (Acolyte)
on Feb 20, 2014 at 18:45 UTC ( #1075621=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I have a list of files like so:

lab1_set1.txt lab1_set2.txt lab2_set1.txt lab2_set2.txt lab3_set2.txt ..... labn.set1.txt labn_set2.txt

for each pair I want to notify the user that the set was paired and notify the user if a set is unpaired.

I currently have the files read into an array. What I don't know is how to set up the regex and use that compare the filenames. My code so far seems off track... the mental block for me is how do I traverse the array and match on a regex condition? My conceptual code below hopefully reveals the deficiency in my thinking...

for (@labsets) { if ($_ ~= /^lab\d{1}/) = [some condition] { print "$_ matched\n!" } else { print "print $_ did not match\n"; }
Thank you all for any pointers and/or insight.

Replies are listed 'Best First'.
Re: How to compare elements within an array
by Kenosis (Priest) on Feb 20, 2014 at 19:03 UTC

    One option is to use a hash to track the lab count (using the lab numbers as keys, so they can be numerically sorted later). If the count is 2, then the lab is paired, else it's unpaired:

    use strict; use warnings; my %hash; while (<DATA>) { $hash{$1}++ if /^lab(\d+)_/; } for my $lab ( sort { $a <=> $b } keys %hash ) { print "lab$lab is ", ( $hash{$lab} == 2 ? 'paired' : 'unpaired' ), + "\n"; } __DATA__ lab1_set1.txt lab1_set2.txt lab2_set1.txt lab3_set1.txt lab3_set2.txt


    lab1 is paired lab2 is unpaired lab3 is paired

    Hope this helps!

      Thank you! This is such an elegant solution. You make hashes awesome. I did not realize you could use regex bare. I always believe you needed a {$variable =~} syntax.

        You're most welcome, doubleqq!

        I did not realize you could use regex bare.

        As I'm sure you've surmised, the regex implicitly operates on Perl's default scalar $_. As you examine more Perl code, you'll note regexes often being used in this context.

Re: How to compare elements within an array
by davido (Cardinal) on Feb 20, 2014 at 19:43 UTC

    Just for fun, this version detects which of the two files is actually missing:

    use strict; use warnings; my %file; while(<DATA>) { chomp; if( !/^lab(\d+)_set(\d+)\.txt/ ) { warn "$_ doesn't belong here!"; next; } $file{$1} += $2; } for( keys %file ) { print "lab$_ ", $file{$_} == 3 ? 'matched' : "is missing lab${_}_set" . ( 3 - $file{$_} ) . ".txt", "\n"; } __DATA__ lab1_set1.txt lab1_set2.txt lab2_set1.txt lab2_set2.txt lab3_set2.txt lab100_set1.txt lab100_set2.txt


    lab1 matched lab3 is missing lab3_set1.txt lab100 matched lab2 matched


Re: How to compare elements within an array
by kcott (Archbishop) on Feb 23, 2014 at 08:33 UTC

    G'day doubleqq,

    Here's another way to do it. This solution only uses a single loop and pulls two elements at a time from the array (using splice). If a pair wasn't found, the second element (if it exists) is put back on the array for retesting on the next iteration.

    #!/usr/bin/env perl -l use strict; use warnings; my @labsets = qw{ lab1_set1.txt lab1_set2.txt oops___should_not_be_in_this_list.txt lab2_set1.txt lab2_set2.txt lab3_set2.txt lab4_set1.txt lab99_set1.txt lab99_set2.txt lab100_set1.txts lab100_set2.txt lab101_set1.txt lab101_set2.txts }; while (@labsets) { my ($set1, $set2) = splice @labsets, 0, 2; if ($set1 =~ /^(lab\d+_set)([12])\.txt$/) { if (! defined $set2) { print 'NOT PAIRED: ', $set1; } elsif ($2 == 1 && $set2 eq $1 . '2.txt') { print "PAIRED: $set1 and $set2"; } else { print 'NOT PAIRED: ', $set1; unshift @labsets, $set2; } } else { print 'BAD FILENAME: ', $set1; unshift @labsets, $set2 if defined $set2; } }


    PAIRED: lab1_set1.txt and lab1_set2.txt BAD FILENAME: oops___should_not_be_in_this_list.txt PAIRED: lab2_set1.txt and lab2_set2.txt NOT PAIRED: lab3_set2.txt NOT PAIRED: lab4_set1.txt PAIRED: lab99_set1.txt and lab99_set2.txt BAD FILENAME: lab100_set1.txts NOT PAIRED: lab100_set2.txt NOT PAIRED: lab101_set1.txt BAD FILENAME: lab101_set2.txts

    Update: Made some changes to the code and added some additional tests. Old code (and old output) in spoiler below.

    -- Ken

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1075621]
Approved by Kenosis
Front-paged by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2023-03-27 20:16 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (65 votes). Check out past polls.