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

Matching an array line and replacing with new data

Status
Not open for further replies.

madaxe

Technical User
Dec 21, 2004
44
0
0
US
Trying to step through the array until one of the first elements in the array is matched then replace that line with new data.


Madaxe


Code:
open (PLAYERLIST3, "$perpicklist");
@userlist3 = <PLAYERLIST3>;
close (PLAYERLIST3);


foreach $moddata (@userlist3) {
chomp $moddata;
($weekno, $playerpick) = split(/\|/, $moddata);


if  ("$weekno" eq "$week_pick") {

open (PLAYERLIST3, "$perpicklist");	
print FILE "$week_pick\|$player_pick\n";
close (PLAYERLIST3);


	}
}
 
1. Modify your @userlist3 array, instead of trying to write to the file inside the foreach loop.

2. After you've read through your array and modified any elements that need modifying, open PLAYERLIST3 for writing and write the modified @userlist3 back onto the file.

Question: You check to see if $weekno eq $week_pick (no quotes necessary). If so, you replace $weekno with $week_pick. If the two were equal to begin with, how have you changed anything?

 
Hi Mikevh

the array looks like this

$weekno|$playerpick
1|golfer
2|golfer
3|golfer
4|golfer
5|golfer
6|golfer

if the user picks week 4 ie $week_pick from the form then it should find week 4 in the array $weekno and replace the second string $playerpick golfer with the player they picked $player_pick
so it should end up looking like

1|golfer
2|golfer
3|golfer
4|Woods,Tiger
5|golfer
6|golfer

do you have an example of how i can achieve what im after

MadAxe
 
Ok i sat down and had another crack at it this is what i came up with see below. Now it finds line 10 in the array and prints those values as header the values in the array "10 Billy"

The part im struggling with is reopening the file and writting the new line back to line 10 in the array with the correct values which should be "10|Woods,Tiger"

Can anybody Help

Madaxe

Code:
#!/Perl/bin/perl

$perpicklist = "marc jeeves.cgi";
$week_pick = "10";
$player_pick = "Woods,Tiger";


##################################################


open (PLAYERLIST3, "$perpicklist");
@userlist3 = <PLAYERLIST3>;
close (PLAYERLIST3);


foreach $moddata (@userlist3) {
chomp $moddata;
($weekno, $playerpick) = split(/\|/, $moddata);

if  ("$weekno" eq "$week_pick") {

print <<EndStart1;

<h1><center>array $weekno array $playerpick</center></h1>

EndStart1

print $moddata "$week_pick\|$player_pick\n";


	}
}

open (PLAYERLIST3, ">>$perpicklist");
print PLAYERLIST3 @userlist3;
close (PLAYERLIST3);

#####################################################

print <<EndStart;


<head>
<title>Thank You</title>
</head>


<body>
<h4><u>$playerpick array element</u></h4>
<h4><u>$weekno array element</u></h4>
<h4><u>$player_pick</u></h4>
<h4><u>$week_pick</u></h4>



EndStart

Code:
Week|......
1|golfer
2|golfer
3|golfer
4|golfer
5|golfer
6|golfer
7|golfer
8|golfer
9|golfer
10|billy
11|golfer
12|golfer
13|golfer
14|golfer
15|golfer
16|golfer
17|golfer
18|golfer
19|golfer
20|golfer
21|golfer
22|golfer
23|golfer
24|golfer
25|golfer
26|golfer
27|golfer
28|golfer
29|golfer
30|golfer
31|golfer
32|golfer
33|golfer
34|golfer
35|golfer
36|golfer
37|golfer
38|golfer
39|golfer
40|golfer
 
This does what you want. If you're sure there won't be more than one record with a match for $week_pick, you can add a last inside the if block to escape from the foreach loop.
Code:
#!perl
use strict;
use warnings;

my $week_pick = 4;
my $newplayer = "Woods,Tiger";
my $perpicklist = "madaxe.txt";

open (PLAYERLIST3, "<$perpicklist") || 
    die qq(Can't open "$perpicklist" for read!\n);    
my @userlist3 = <PLAYERLIST3>;
close(PLAYERLIST3) ||
    die qq(Can't close "$perpicklist" after read!\n);

foreach my $moddata (@userlist3) {
    chomp $moddata;
    my ($weekno, $playerpick) = split(/\|/, $moddata);
    if ($weekno eq $week_pick) {
        [b]$moddata = "$week_pick\|$newplayer";[/b]
    }
}

open (PLAYERLIST3, ">$perpicklist") || 
    die qq(Can't open "$perpicklist" for write!\n);    
print PLAYERLIST3 join("\n", @userlist3), "\n";
close(PLAYERLIST3) ||
    die qq(Can't close "$perpicklist" after write!\n);
HTH

 
Here's a different example using a hash.

Code:
# This is where you read in the data from the file.
my @input = <DATA>;
chomp @input;

my %userlist3;
foreach (@input) {
    my ($week, $player) = split(/\|/, $_);
    $userlist3{$week} = $player;
}

# Change week 4
my ($change_week, $change_player) = (4, 'Woods, Tiger');
$userlist3{$change_week} = $change_player;

# This is where you would rewrite your updated file.
foreach (sort {$a <=> $b} keys %userlist3) {
    print "$_\|$userlist3{$_}\n";
}

__DATA__
1|golfer
2|golfer
3|golfer
4|golfer
5|golfer
6|golfer
 
rharsh, you never check to see if a record with key 4 exists. If it doesn't, you create a new record. Also records will not necessarily be written back to file in the original order (if that matters).


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top