Changing output

E

Euro Millions

Hi there

What I would like to do is simple (that is if you know it)

I have a CSV text file (comma separated file) which contains the following
info:

Language, Greeting,
English, Hello,
German, Hallo,
Spanish, Hola,
Japanese, Konnichiwa,

I would like Perl to create an output file which looks as follows:

Language,Language2, Language3, Langauge4, Greeting,Greeting2, Greeting3,
Greeting4
English, German, Spanish, Japanese, Hello, Hallo, Hola, Konnichiwa

How can I get Perl to read this text file?
What is the easiest way of going about it?
Which Perl Modules would you recommend?

Thank you in advance for your assistance.

Thanks

Mike
 
E

Eric Schwartz

Euro Millions said:
What I would like to do is simple (that is if you know it)

I have a CSV text file (comma separated file) which contains the following
info:

Language, Greeting,
English, Hello,
German, Hallo,
Spanish, Hola,
Japanese, Konnichiwa,

I would like Perl to create an output file which looks as follows:

Language,Language2, Language3, Langauge4, Greeting,Greeting2, Greeting3,
Greeting4
English, German, Spanish, Japanese, Hello, Hallo, Hola, Konnichiwa

How can I get Perl to read this text file?

perldoc -f open
perldoc perlsyn (look for the ' said:
What is the easiest way of going about it?

I'd use a hash.
Which Perl Modules would you recommend?

You don't need any. You could get ultra-fancy and try a
matrix-oriented module or some such, but there's no point at all to
doing so.

FYI, the way to get the best response out of this group is to try it
yourself, and post the code. That way, you may well figure it out on
your own, and if you don't, we'll have a starting point to work with.

Posters on clpm don't take kindly to someone saying, essentially,
"please write my program for me." That may not have been what you
meant, but it's what it'll look like to many of the best contributors
here. When you post code, you're saying, "Here, I've made an effort,
but I got stuck. What's wrong with this?" It also gives people the
opportunity to suggest other improvements in your code you may not
have thought about yet.

The best example is a 10-20 line program that exhibits your problem
clearly.

-=Eric
 
G

gnari

[snip problem]

it is usually better if you post what you have tried, and tell us
why it is not good enough

but here a few pointers anyways:

you might want to open the file with open()
dont forget the check if the open failed, then call die()
use a while loop to read the lines in succession
while (<FILEHANDLE>) {
use split() on each line to get the individual columns
and store the values into an array
when all lines read close() the file

now loop over the array elements, printing them out

if you dont know all these functions, use perldoc:
perldoc -f open
perldoc -f close
perldoc -f split
perldoc perlsyn
perldoc perlop

there are some modules on CPAN that deal with CSV
(search.cpan.org)

although usually the values are enclosed in quotes in CSV
"bla","foo","bar"

good luck

gnari
 
W

Walter Roberson

:What I would like to do is simple (that is if you know it)

:I have a CSV text file (comma separated file) which contains the following
:info:

:Language, Greeting,
:English, Hello,
:German, Hallo,
:Spanish, Hola,
:Japanese, Konnichiwa,

:I would like Perl to create an output file which looks as follows:

:Language,Language2, Language3, Langauge4, Greeting,Greeting2, Greeting3,
:Greeting4
:English, German, Spanish, Japanese, Hello, Hallo, Hola, Konnichiwa


If the exact order of the outputs does not matter (as long as the pairs
are correct):


my %phrases;

while (<>) {
my ($lang, $phrase) = split /,/;
$phrases{$lang} = $phrase;
}

my $numphrase = scalar(keys %phrases);

{
my $heading = 'Language,';
$heading .= "Language$_, " for (2..$numphrase);
$heading .= "Greeting";
$heading .= ",Greeting2" if $numphrase >= 2;
$heading .= ", Greeting$_" for (3..$numphrase);
$heading .= "\n";
print $heading;
}

{
my $body = join ', ', keys %phrases;
$body .= ", $phrases{$_}" for keys %phrases;
$body .= "\n";
print $body;
}



But your program would be easier if you could get rid of the
strange requirement for not having a space betweeen Language,Language2
and Greeting,Greeting2 ...
 
B

Brad Baxter

What I would like to do is simple (that is if you know it)

I have a CSV text file (comma separated file) which contains the following
info:

Language, Greeting,
English, Hello,
German, Hallo,
Spanish, Hola,
Japanese, Konnichiwa,

I would like Perl to create an output file which looks as follows:

Language,Language2, Language3, Langauge4, Greeting,Greeting2, Greeting3,
Greeting4
English, German, Spanish, Japanese, Hello, Hallo, Hola, Konnichiwa

Okay, now tell us what you REALLY want to do. :)

Obviously, you don't really have that file, because writing a program for
that purpose just doesn't make sense. So, do you really have a file that
contains dozens of languages? Is "Greeting" always "Hello", not "Good
morning", "G'day", "'Sup"? I assume your list contains other things, like
"Thank you", "Please", etc.

The question as it stands is so trivial that I'm compelled to think you're
leaving out the parts that matter.

Regards,

Brad
 

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

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top