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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning Nth value to every record in file 1

Status
Not open for further replies.

bigbalbossa

Programmer
Mar 21, 2002
87
US
All,

I have a script that calculates an nth value. I need to use this value to assign a number to every record in my file.

For example, if my datafile contains 2000 records and i want to keep 100 nth'd records, my value will be 16 (based on a formula in my script). Now, i need to print each record while incrementing a counter 1 to 16, then back to 1 to 16...and so on, until each rec has a value.

Make sense?
 
a general idea:

Code:
my $flag = 1;
for (a..z,a..z) {
   print $flag++, ". $_\n";
   $flag = 1 if $flag > 16;
}


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Another way:

my $num = 1;
while (<>) {
print "$num $_";
$num += 1;
if ($num > 16) {
$num = 1;
}
}
 
Yes, they are really essentially the same. They would both work equally well (or unwell) as the case may be.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
*grin*

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@flags[/blue] = [fuchsia]1..1[/fuchsia][red]<<4[/red][red];[/red]
[purple]for (a..z,a..z) {[/purple]
[purple]	push [blue]@flags[/blue], my [blue]$flag[/blue] = shift [blue]@flags[/blue];[/purple]
[purple]	print [blue]$flag[/blue], ". [blue]$_[/blue][purple][b]\n[/b][/purple]";[/purple]
[purple]}[/purple]

- Miller
 
that should be: "*evil grin*" [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
*twisted grin*

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$flag[/blue][red];[/red]
[olive][b]for[/b][/olive] [red]([/red]a..z,a..z[red])[/red] [red]{[/red]
	[red]([/red][blue]$flag[/blue]&=[fuchsia]0xf[/fuchsia][red])[/red]++[red];[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$flag[/blue], [red]"[/red][purple]. [blue]$_[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

- M
 
hehehe... did the Doctor take you off your meds or put you back on? [spineyes]

[smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top