Processing multiple delimiter files

O

ollie

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
 
G

Gunnar Hjalmarsson

ollie said:
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?
 
M

Matt Garrish

ollie said:
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
 
E

Eric Bohlman

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.
 
O

ollie

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
 
A

A. Sinan Unur

@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.
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
 
T

Tad McClellan

ollie said:
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;
 
O

ollie

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 said:
ollie said:
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;
 
B

Brad Baxter

ollie said:
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::parseWords;

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;
}
1,32, ,houston
3,40, ,albany
3,44, ,nyc
1,Texas
3,NY
1,32,Texas,houston
3,40,NY,albany
3,44,NY,nyc


Regards,
 
T

Tad McClellan

ollie said:
Thanks Tad for the suggestions.


You are welcome.

I will accept you having a look at the Posting Guidelines that are
posted here frequently as an easy way for you to pay me back. :)

The sample code was for illustration
and not a real code.


It is disrespectful to post code that is not real.

Lots of people will waste their time fixing syntax errors that
do not even really exist!

Please post only real code (copy/paste it, do not try to re-type it).



[ snip TOFU ]
 
D

DJ Stunks

Brad said:
This sample code is for illustration.

Contents of ./qt:

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

my @f1 = `cat file1`;
my @f2 = `cat file2`;

-- OUCH! --^^^^^^^^^^^

let's not illustrate shelling out if we can avoid it, huh?

-jp
 
O

ollie

If you paid attention to my first posting, the help i seeked for was on
the perl construct. I would have said so if i needed help with the
code.

Sorry, that your focused on the code more than the problem.
 
A

A. Sinan Unur

@j33g2000cwa.googlegroups.com:

[ The fact that you are still not quoting any context as you have been
asked reflects poorly on you ]
If you paid attention to my first posting, the help i seeked
for was on the perl construct. I would have said so if i needed
help with the code.

Sorry, that your focused on the code more than the problem.

Sorry that you insist on being uncooperative.

You are expected to post short, self-contained code that does makes it
easy for others to help you.

Bye!

Sinan
 
B

Brad Baxter

DJ said:
-- OUCH! --^^^^^^^^^^^

let's not illustrate shelling out if we can avoid it, huh?

Given that Perl is a glue language, and that in some
circumstances shelling out to an external utility is a
good solution, and that the above is an illustration of
how one might do that in Perl (though arguably an
incomplete one), I am therefore unapologetic. Sorry.

perl -F','
-lane'if($|){$F[2]=$_{$F[0]};print"@F"}else{$_{$F[0]}=$F[1]}eof&&++$|;$"=","'
file2 file1

There. No shelling out.

Cheers,
 
K

Keith Keller

If you paid attention to my first posting, the help i seeked for was on
the perl construct.

Your first posting did not contain any Perl constructs.
I would have said so if i needed help with the code.

Apparently not, since the code that you did post had syntax errors.
Sorry, that your focused on the code more than the problem.

It sounds like the code is part of the problem; the other part is that
you are still not following the posting guidelines to which you have
been repeatedly referred. If you want free help, you should probably
exercise some common courtesy when posting.

--keith
 
D

DJ Stunks

Brad said:
I am therefore unapologetic. Sorry.

I don't understand -- you're sorry you're not apologetic? Or, like,
unapologetic but sorry nonetheless?

Peter: Chris, everything I say is a lie. Except that. And that. And
that. And that. And that. And that. And that. And that.

-jp
 
T

Tad McClellan

ollie said:
If you paid


If who?

attention to my first posting,


It seems pretty clear to me that I was paying attention
to your first posting.

Rest assured that that will not happen again.

the help i seeked for was on
the perl construct.


What you posted was not a Perl (nor perl) construct.

The perl parser is the definition of Perl, and since it was
not accepted by perl, it was not (yet) Perl.

I would have said so if i needed help with the
code.


It sure did look to me like you needed help with the code,
because the code could not be compiled or executed.

Sorry, that your focused on the code more than the problem.


Sorry you have volunteered to become invisible.

So long!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top