[prev in list] [next in list] [prev in thread] [next in thread] 

List:       perl-win32-users
Subject:    RE: how do you replace elements of a list ?
From:       "Corcoran, DavidX" <davidx.corcoran () intel ! com>
Date:       1999-07-30 19:01:47
[Download RAW message or body]

To change the index in an array you can also just modify $_ thus:

	@x = (1..40);

	for (@x) {
		$_ = func($_) if /.5/;
	}

	for (@x) {
		print "$_\n";
	}

sub func
{
	return "($_[0])";
}

> -----Original Message-----
> From:	Igor "Lepi" [SMTP:igor@attractive.com]
> Sent:	Tuesday, July 27, 1999 2:13 PM
> To:	Perl-Win32-Users Mailing List
> Subject:	Re: how do you replace elements of a list ?
> 
> You could always open two files and rename the new over the old like this
> if
> you are using a list in a file  :
> 
> open (INFILE,"file1.txt") || die $!;
> #open backup file
> open (OUTFILE,"file2.txt") || die $!;
> while (<INFILE>)
> {
>     chomp;
>     $return = &validate_trans($_);
>     print OUTFILE $return;
>     print "\n";
> }
> 
> close (INFILE);
> close(OUTFILE);
> rename "file2.txt","file1.txt";
> 
> or if you just want to change an array :
> 
> $i = 0;
> while (@list)
> {
>         my $ret;
>         if ($ret = &validate_trans($_))
>         {
>             $list[$i] = $ret;
>         }
>     $i++;
> }
> 
> Btw i tried to modify your sub but I'm not quite sure i did the right
> thing
> but I think you'll grasp it :))
> 
>  sub validate_trans(
>  {
> 
> my $line =  shift;
>    # check each line of the trans for potential errors
>      # check to see we have a policy number
>      #
>      if ("DataHeader" eq substr($_,1,10))
>      {
>         # break up the line into it's smaller fields
>        @fields = split(/,/,$line);
>         # capture the policy number and check it
>        $policyid = $fields[1];
>        if (! check_for_policy_id($policyid))
>        {
>           # set the error level indicator
>          $error_level = 2;
> 
>          # set the error flag for bad policy number
>          $e_policyid = 1;
> 
>          # set policy number to a default value
>          $fields[1] = "FK0000000";
> 
>          # join all of fields back into a single trans line again
>          $_ = join(",",@fields);
> 
>           # now i need to replace the current element of @trans with it's
>  corrected version
>          return $_;
>        }
>        else
>        {
>          # skip to the next line in the list
>          next;
>        }
>      } # end if
> 
> 
> Igor;
> ----- Original Message -----
> From: Mark Meyer <geeky2@gte.net>
> To: Perl-Win32-Users Mailing List
> <perl-win32-users@listserv.activestate.com>
> Sent: Tuesday, July 27, 1999 18:48
> Subject: how do you replace elements of a list ?
> 
> 
> > hi everyone..
> >
> > how do you "replace" an element  of a list ?
> >
> > the below code loops through a list "@trans" of "lines" pulled from a
> text
> > file.  each "line" element of the list is broken into it's constituent
> parts
> > using the split() function.  once i have determined that a particular
> part
> > of the line is missing or incorrect i replace it with it's default value
> > like FK0000001.  then i join all of the fields of the line back together
> > again.
> >
> > finally i need to be able to put that "corrected" line back into my list
> >
> >
> > thanks for any help
> > mark
> >
> > sub validate_trans(
> > {
> >
> >   my @trans;
> >
> >   # assign the trans array to the in comming parm
> >   @trans = @_;
> >
> >   # check each line of the trans for potential errors
> >   foreach $_ (@trans)
> >   {
> >     # check to see we have a policy number
> >     #
> >     if ("DataHeader" eq substr($_,1,10))
> >     {
> >        # break up the line into it's smaller fields
> >       @fields = split(/,/,$_);
> >        # capture the policy number and check it
> >       $policyid = $fields[1];
> >       if (! check_for_policy_id($policyid))
> >       {
> >          # set the error level indicator
> >         $error_level = 2;
> >
> >         # set the error flag for bad policy number
> >         $e_policyid = 1;
> >
> >         # set policy number to a default value
> >         $fields[1] = "FK0000000";
> >
> >         # join all of fields back into a single trans line again
> >         $_ = join(",",@fields);
> >
> >          # now i need to replace the current element of @trans with it's
> > corrected version
> >
> >       }
> >       else
> >       {
> >         # skip to the next line in the list
> >         next;
> >       }
> >     } # end if
> >
> >
> >
> > ---
> > You are currently subscribed to perl-win32-users as: igor@attractive.com
> > To unsubscribe, forward this message to
> >          leave-perl-win32-users-90392J@lyris.activestate.com
> > For non-automated Mailing List support, send email to
> >          ListHelp@ActiveState.com
> 
> 
> 
> ---
> You are currently subscribed to perl-win32-users as:
> davidx.corcoran@intel.com
> To unsubscribe, forward this message to
>          leave-perl-win32-users-90392J@lyris.activestate.com
> For non-automated Mailing List support, send email to  
>          ListHelp@ActiveState.com

---
You are currently subscribed to perl-win32-users as: [perl-win32-users@progressive-comp.com]
To unsubscribe, forward this message to
         leave-perl-win32-users-90392J@lyris.activestate.com
For non-automated Mailing List support, send email to  
         ListHelp@ActiveState.com

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic