[Novalug] perl - can't wrap my head around a loop

Miles Oliver miles.d.oliver@gmail.com
Wed Jan 21 12:51:30 EST 2009


Thx to all who assisted.

Been slammed with other things, just now getting back to replying.

my %dump_lookup;    # create a lookup hash

This was exactly what I needed to create a lookup hash.

This made it work.

Many thanks.

On 1/14/09, William Sutton <william@trilug.org> wrote:
> did it help?
>
> William Sutton
>
>
> On Wed, 14 Jan 2009, William Sutton wrote:
>
> > Not sure if this is what you're after, but give it a spin:
> >
> > #!/usr/bin/perl
> > use strict;
> >
> > $| = 1; # turn on autoflush so writes are seen immediately
> >
> > my %dump_lookup;    # create a lookup hash
> >
> > open(INDUMP, "<$csvfilename");
> > while (my $line = <INDUMP>)
> > {
> >    $line =~ s/[\r\n]+$//;
> >    my ($id, $company) = split(/,/, $line, 2);
> >    $dump_lookup{$id} = $company;   # assumes id to be unique
> > }
> > close (INDUMP);
> >
> > # at this point, %dump_lookup is a map of ids to companies...
> > open(INGROUP, "<$csvgroupname");
> > open(DUMP, ">>$csvfilenamefinal");
> > while (my $line = <INDGROUP>)
> > {
> >    $line =~ s/[\r\n]+$//;
> >
> >    # I don't know what column 2 is, so it's "$unknown" for now
> >    my ($id, $unknown, $name, $phone) = split(/,/, $line, 4);
> >
> >    # join the results of the line, including an id lookup
> >    print DUMP join(",", $id, $dump_lookup{$id}, $name,$phone) ."\r\n";
> > }
> >
> > # close and quit
> > close(DUMP);
> > close(INGROUP);
> >
> >
> > William Sutton
> >
> > On Wed, 14 Jan 2009, Miles Oliver wrote:
> >
> >
> > > Im fighting with some search and replace functions in perl. I've got
> > > some explicit lin The answer is probably very to do but I just cant
> > > seem to wrap my head around the answer.
> > >
> > > I've written very little perl and am finding that I need to do more
> > > and this is a problem I need to solve.
> > >
> > > Some of you can probably explain very easily to me.
> > >
> > > I want to loop through file A line by line and substitue a string
> > > globally in file B based upon the second element of the array in file
> > > A
> > >
> > > File A is a .csv file like
> > >
> > > 1,company 123
> > > 2,company 456
> > >
> > > I want to replace in file B, witing a new file C with the changes.
> > >
> > > ID,1,NAME,PHONE
> > >
> > > with
> > >
> > > ID,company 123,NAME,PHONE
> > > ID,company 456,NAME,PHONE
> > > ID,company 123,NAME,PHONE
> > >
> > >
> > > Probably something very simple but I've looked at this for a long time
> > > and could use a fresh pair of eyes.
> > >
> > > Be kind....
> > >
> > >
> > > open(INDUMP, "<$csvfilename");
> > > open(INGROUP, "<$csvgroupname");
> > > open(DUMP, ">>$csvfilenamefinal");
> > >
> > > @lines2 = <INGROUP>; # read file into list
> > >
> > > foreach $inline2(@lines2) # loop thru list
> > > {
> > > @data=split(',',$inline2);
> > > my $dumpname = $data[0];
> > > my $dumpstring = $data[1];
> > > while (<INDUMP>){
> > >       $_ =~ s/,$dumpname,/,$dumpstring,/g;
> > > #        print $_;
> > >       print DUMP $_;
> > > }
> > > }
> > > close(INDUMP);
> > > close(INGROUP);
> > > close(DUMP);
> > >
> > >
> > > --
> > > doveryai, no proveryai
> > > http://en.wikipedia.org/wiki/Trust,_but_Verify
> > > _______________________________________________
> > > Novalug mailing list
> > > Novalug@calypso.tux.org
> > > http://calypso.tux.org/cgi-bin/mailman/listinfo/novalug
> > >
> > >
> > >
> > _______________________________________________
> > Novalug mailing list
> > Novalug@calypso.tux.org
> > http://calypso.tux.org/cgi-bin/mailman/listinfo/novalug
> >
> >
> >
>


-- 
"The nine most terrifying words in the English language are, 'I'm from
the government and I'm here to help.'"
Ronald Reagan



More information about the Novalug mailing list