Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Move to next iteration?

Status
Not open for further replies.

max1x

Programmer
Jan 12, 2005
366
US
Hey all,

I am reading in from a file with the following fields:

Name|Flag|Function|ID
P_1|0|LC|0|
P_1|1|LC|1|
P_2|1|LC|2|
P_2|0|LC|3|
P_2|0|LC|4|
P_12|0|LC|0|
P_12|0|LC|1|
P_12|0|LC|2|
P_12|0|LC|3|

I am trying to read only those names where the flag is set to "0" and NO "1's" are assocatied to that name. So for the above file, I want to grab all records for P_12 and ignore P_1 and P_2.


#!/usr/bin/perl
use strict;
open IN,"< ./file.xls" || die "Cannot open file.xls:($!)"; while (my $line = <IN>) {
if ($line =~ /^Name/i) {
next;
}
my ($Name,$Flag,$Function,$ID) = split (\|/,$line);
my @name = $Name;
foreach my $name (sort @name) {
if ($Flag == 1) { next; }
print "$name, $Flag, $Function, $ID\n";
}
} close IN;

This gives me the next record but for same Name where the Flag=0.
I am trying another approach, using hashes, but wanted to check if there is an easier way.

Thank you.
 
Is this what you need?

If so, you will need to alter the __DATA__ section to read in a file (i can help if required)

Code:
[b]#!/usr/bin/perl[/b]

while (<DATA>) {
  @cols = split(/\|/);
  $name{$cols[0]} += $cols[1];
  $values{$cols[0]} .= $_;
}

while (($key, $value) = each %name) {
  if ($value == 0) {
    print "$values{$key}\n\n";
  }
}

[blue]__DATA__
P_1|0|LC|0|
P_1|1|LC|1|
P_2|1|LC|2|
P_2|0|LC|3|
P_2|0|LC|4|
P_12|0|LC|0|
P_12|0|LC|1|
P_12|0|LC|2|
P_12|0|LC|3|[/blue]


Kind Regards
Duncan
 
Thank you Duncan. I couldn't figure out on the logic, but you made it very simple. Thanks again.
 
no problem! glad to help!

... when i get a moment i will explain the code


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top