在 FOR 循环中打印唯一值 [英] Print unique values in FOR loop

查看:51
本文介绍了在 FOR 循环中打印唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件 myresult 和 annotation.两个文件中的数据似乎是范围,但它们不是,这就是为什么我不能将它存储在数组中,我需要使用 split 运算符,以便我可以在 for 循环中使用它并进行比较.现在我需要从 $i(myresult) 和 $j(annotation) 打印所有常见值而不重复(唯一).我没有得到哪个条件以及如何实现它以获得所需的输出.我尝试使用 %hash 但无法实现.

我的结果

0..35112..36324..37536..38748..39960..411..

注释

272..10421649..26293436..47524793..49755408..60226025..6252..

代码:

#!/usr/bin/perlopen( $inp0, "<myresult" ) or die "not found";open( $inp2, "<annotation" ) or die "not found";open( $out, ">output" );我的@arr2 = <$inp0>;我的@arr4 = <$inp2>;我的 $sum1 = 0;foreach 我的 $line1 (@arr2) {我的 ( $from1, $to1 ) = split(/\.\./, $line1 );foreach 我的 $line2 (@arr4) {我的 ( $from2, $to2 ) = split(/\.\./, $line2 );for ( 我的 $i = $from1; $i <= $to1; $i++ ) {for ( 我的 $j = $from2; $j <= $to2; $j++ ) {如果 ( $i == $j ) {打印 $out "$i \n";$sum1++;}}}}}打印唯一值= $sum1";

解决方案

将每个数据集转换成一个值数组.

然后使用哈希计算两个列表中匹配的 uniq 值:

#!/usr/bin/perl -w使用严格;使用警告;使用autodie;使用 List::MoreUtils qw(uniq);我的@result = 做 {#打开我的 $fh, '<', "myresult";打开我的 $fh, '<', \ "0..351\n12..363\n24..375\n36..387\n48..399\n60..411\n";地图 { 我的 ( $min, $max ) =/\d+/g;( $min .. $max ) } <$fh>;};我的@annot = 做 {#打开我的$fh, '<', "myresult";打开我的 $fh, '<', \ "272..1042\n1649..2629\n3436..4752\n4793..4975\n5408..6022\n6025..6252\n";地图 { 我的 ( $min, $max ) =/\d+/g;( $min .. $max ) } <$fh>;};我的 %count;$count{$_}++ for uniq(@result), uniq(@annot);print join( ' ', sort { $a <=> $b } grep { $count{$_} == 2 } keys %count ), "\n";

输出:

<预> <代码> 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 402 401 403 404 405 406 407 408 409 410 411

I have two files myresult and annotation. data in the two files appear to be as range but they are not, that's why i cant store it in an array and i need to use split operator so that i can use it in a for loop and compare. now i need to print all common values from $i(myresult) and $j(annotation) without repetition (unique). i am not getting which condition and how to implement it so as to get desired output. i tried using %hash but could not able to implement.

myresult

0..351 
12..363  
24..375  
36..387  
48..399  
60..411  
.
.

annotation

272..1042
1649..2629
3436..4752
4793..4975
5408..6022
6025..6252
.
.

CODE:

#!/usr/bin/perl
open( $inp0, "<myresult" )   or die "not found";
open( $inp2, "<annotation" ) or die "not found";
open( $out,  ">output" );
my @arr2 = <$inp0>;
my @arr4 = <$inp2>;
my $sum1 = 0;

foreach my $line1 (@arr2) {
    my ( $from1, $to1 ) = split( /\.\./, $line1 );

    foreach my $line2 (@arr4) {
        my ( $from2, $to2 ) = split( /\.\./, $line2 );

        for ( my $i = $from1; $i <= $to1; $i++ ) {
            for ( my $j = $from2; $j <= $to2; $j++ ) {
                if ( $i == $j ) {

                    print $out "$i \n";
                    $sum1++;
                }
            }
        }
    }
}

print "Unique values = $sum1";

解决方案

Translate each data set into an array of values.

Then use a hash to count the matched uniq values from both lists:

#!/usr/bin/perl -w
use strict;
use warnings;
use autodie;

use List::MoreUtils qw(uniq);

my @result = do {
    #open my $fh, '<', "myresult";
    open my $fh, '<', \ "0..351\n12..363\n24..375\n36..387\n48..399\n60..411\n";
    map { my ( $min, $max ) = /\d+/g; ( $min .. $max ) } <$fh>;
};

my @annot = do {
    #open my $fh, '<', "myresult";
    open my $fh, '<', \ "272..1042\n1649..2629\n3436..4752\n4793..4975\n5408..6022\n6025..6252\n";
    map { my ( $min, $max ) = /\d+/g; ( $min .. $max ) } <$fh>;
};

my %count;
$count{$_}++ for uniq(@result), uniq(@annot);

print join( ' ', sort { $a <=> $b } grep { $count{$_} == 2 } keys %count ), "\n";

Outputs:

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

这篇关于在 FOR 循环中打印唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆