Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Processing multiple delimiter files

Reply
Thread Tools

Processing multiple delimiter files

 
 
ollie
Guest
Posts: n/a
 
      03-13-2006
Can someone help with a perl construct for this. I am trying to perform
a SQL type update on a delimiter file. I want to use the value in one
file to update matching row in the other file. Kind of what you'll do
in a relational database system, but I need to do this outside of the
database.

Example
1st file
1,32, ,houston
3,40, ,albany
3,44, ,nyc

2nd file
1,Texas
3,NY

Result:
1,32,Texas,houston
3,40,NY,albany
3,44,NY,albany

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-13-2006
ollie wrote:
> Can someone help with a perl construct for this.


Help? Sure, but first show us what you have tried, and how it fails to
satisfy your needs. Please comply with the posting guidelines for this
group when posting your code.

> I am trying to perform
> a SQL type update on a delimiter file. I want to use the value in one
> file to update matching row in the other file. Kind of what you'll do
> in a relational database system, but I need to do this outside of the
> database.
>
> Example
> 1st file
> 1,32, ,houston
> 3,40, ,albany
> 3,44, ,nyc
>
> 2nd file
> 1,Texas
> 3,NY
>
> Result:
> 1,32,Texas,houston
> 3,40,NY,albany
> 3,44,NY,albany


How did 'nyc' change to 'albany' at the 3:rd line?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
ollie
Guest
Posts: n/a
 
      03-13-2006
Sorry, the output should be

1,32,Texas,houston
3,40,NY,albany
3,44,NY,nyc

 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      03-13-2006

"ollie" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Sorry, the output should be
>
> 1,32,Texas,houston
> 3,40,NY,albany
> 3,44,NY,nyc
>


Please quote context when replying.

Where is your code? How does DBD-CSV or Tie-Handle-CSV not help do what you
want, for example?

Matt


 
Reply With Quote
 
Eric Bohlman
Guest
Posts: n/a
 
      03-13-2006
"ollie" <(E-Mail Removed)> wrote in news:1142286451.557668.38820
@i40g2000cwc.googlegroups.com:

> Can someone help with a perl construct for this. I am trying to perform
> a SQL type update on a delimiter file. I want to use the value in one
> file to update matching row in the other file. Kind of what you'll do
> in a relational database system, but I need to do this outside of the
> database.
>
> Example
> 1st file
> 1,32, ,houston
> 3,40, ,albany
> 3,44, ,nyc
>
> 2nd file
> 1,Texas
> 3,NY
>
> Result:
> 1,32,Texas,houston
> 3,40,NY,albany
> 3,44,NY,albany


Why not use DBD::CSV and actually do it in SQL? If for some reason you
don't want to use SQL, Data::Table and some of its relatives provide join
operations.
 
Reply With Quote
 
ollie
Guest
Posts: n/a
 
      03-14-2006
I would like to use hashmap data structure. I can create the hash as
follows

open(<IN>, "> 2ndfile") || die;
@rows = <IN>;
close(IN);

%data_row = ();
foreach (@rows) (
$data_row{$rows[0] = $rows[1];
}

This is were I get stuck

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-14-2006
"ollie" <(E-Mail Removed)> wrote in news:1142295081.669913.102070
@p10g2000cwp.googlegroups.com:

[ You really ought to read the posting guidelines and start actually
responding to the questions people ask you in their quest to help
you. You should also quote some context when you reply.
See <URL:http://cfaj.freeshell.org/google/>
]

> I would like to use hashmap data structure. I can create the hash as
> follows
>


use strict;
use warnings;

missing.

> open(<IN>, "> 2ndfile") || die;


You have just overwritten '2ndfile'.

> @rows = <IN>;


It does not make sense to try to read from a filehandle which you have
opened for writing only.

Don't rewrite code ... copy and paste it into your post.

The posting guidelines explain how to post code.

> close(IN);
>
> %data_row = ();
> foreach (@rows) (
> $data_row{$rows[0] = $rows[1];
> }
>
> This is were I get stuck


In your case, I suspect that the best solution to your problem involves
using DBD::CSV. The following crufty "solution" should illustrate my
lack of skills:

#!/usr/bin/perl

use strict;
use warnings;

my %states = (
1 => 'Texas',
3 => 'NY'
);

my %cities = (
houston => { state_code => 1, city_code => 32 },
albany => { state_code => 3, city_code => 40 },
nyc => { state_code => 3, city_code => 44 },
);

for my $k ( keys %cities ) {
$cities{$k}->{state_name} = $states{ $cities{$k}->{state_code} };
{
local $" = ', ';
my %record = %{ $cities{$k} };
print "@record{qw(state_code city_code state_name)}, $k\n";
}
}

__END__

D:\Home\asu1\UseNet\clpmisc> h
3, 40, NY, albany
3, 44, NY, nyc
1, 32, Texas, houston


Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-14-2006
ollie <(E-Mail Removed)> wrote:

> I can create the hash as
> follows
>
> open(<IN>, "> 2ndfile") || die;



No you can't.

It does not even compile.

All of the angle bracket characters should be removed from that line.

Why did you put them there?


> @rows = <IN>;
> close(IN);
>
> %data_row = ();
> foreach (@rows) (



There is another syntax error.

You need a curly brace, not a parenthesis.

Is this your real code?


> $data_row{$rows[0] = $rows[1];



There is yet another syntax error, missing a closing curly somewhere.


> }
>
> This is were I get stuck



You got stuck way before here (where you claim that you can
create the hash).

Here is one way to create the hash:

-----------------------
#!/usr/bin/perl
use warnings;
use strict;

open(IN, '2ndfile') or die "could not open '2ndfile' $!";

my($key, %data_row);
while ( <IN> ) {
chomp;
if ( $. % 2 ) # an odd-numbered line
{ $key = $_ }
else # an even-numbered line
{ $data_row{$key} = $_ }
}
close(IN);

print "$_ => $data_row{$_}\n" for sort keys %data_row;
-----------------------


But I'd do it like this:

-----------------------
#!/usr/bin/perl
use warnings;
use strict;

open(IN, '2ndfile') or die "could not open '2ndfile' $!";

my %data_row = map {chomp; $_} <IN>;
close(IN);

print "$_ => $data_row{$_}\n" for sort keys %data_row;
-----------------------


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
ollie
Guest
Posts: n/a
 
      03-14-2006
Thanks Tad for the suggestions. The sample code was for illustration
and not a real code. I'll will look into using DBD::CSV. I though there
might be a way to do achieve the objective using hashmaps all the way

Thanks again.

Tad McClellan wrote:
> ollie <(E-Mail Removed)> wrote:
>
> > I can create the hash as
> > follows
> >
> > open(<IN>, "> 2ndfile") || die;

>
>
> No you can't.
>
> It does not even compile.
>
> All of the angle bracket characters should be removed from that line.
>
> Why did you put them there?
>
>
> > @rows = <IN>;
> > close(IN);
> >
> > %data_row = ();
> > foreach (@rows) (

>
>
> There is another syntax error.
>
> You need a curly brace, not a parenthesis.
>
> Is this your real code?
>
>
> > $data_row{$rows[0] = $rows[1];

>
>
> There is yet another syntax error, missing a closing curly somewhere.
>
>
> > }
> >
> > This is were I get stuck

>
>
> You got stuck way before here (where you claim that you can
> create the hash).
>
> Here is one way to create the hash:
>
> -----------------------
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> open(IN, '2ndfile') or die "could not open '2ndfile' $!";
>
> my($key, %data_row);
> while ( <IN> ) {
> chomp;
> if ( $. % 2 ) # an odd-numbered line
> { $key = $_ }
> else # an even-numbered line
> { $data_row{$key} = $_ }
> }
> close(IN);
>
> print "$_ => $data_row{$_}\n" for sort keys %data_row;
> -----------------------
>
>
> But I'd do it like this:
>
> -----------------------
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> open(IN, '2ndfile') or die "could not open '2ndfile' $!";
>
> my %data_row = map {chomp; $_} <IN>;
> close(IN);
>
> print "$_ => $data_row{$_}\n" for sort keys %data_row;
> -----------------------
>
>
> --
> Tad McClellan SGML consulting
> (E-Mail Removed) Perl programming
> Fort Worth, Texas


 
Reply With Quote
 
Brad Baxter
Guest
Posts: n/a
 
      03-14-2006
ollie wrote:
> Thanks Tad for the suggestions. The sample code was for illustration
> and not a real code. I'll will look into using DBD::CSV. I though there
> might be a way to do achieve the objective using hashmaps all the way
>


This sample code is for illustration.

Contents of ./qt:

#!/usr/local/bin/perl
use warnings;
use strict;
use Text:arseWords;

my @f1 = `cat file1`;
my @f2 = `cat file2`;
die unless @f1 and @f2;

my %h;
for( @f2 ) {
chomp;
my @a = parse_line( ',', '', $_ );
$h{ $a[0] } = $a[1];
}

for( @f1 ) {
my @a = parse_line( ',', '', $_ );
$a[2] = $h{ $a[0] };
print join ',' => @a;
}

>> cat file1

1,32, ,houston
3,40, ,albany
3,44, ,nyc

>> cat file2

1,Texas
3,NY

>> ./qt

1,32,Texas,houston
3,40,NY,albany
3,44,NY,nyc


Regards,

--
Brad

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
processing input from multiple files Christopher Steele Python 2 10-14-2010 07:15 PM
Processing Multiple Large Files friend.05@gmail.com Perl Misc 20 01-02-2009 03:22 PM
Reading files, splitting on a delimiter and newlines. Bruno Desthuilliers Python 8 07-26-2007 06:45 AM
parse with multiple delimiter anitawa@gmail.com Perl Misc 3 04-26-2007 08:12 AM
Text files read multiple files into single file, and then recreate the multiple files googlinggoogler@hotmail.com Python 4 02-13-2005 05:44 PM



Advertisments