Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Separate column results?!

by perlnoobster (Sexton)
on Aug 29, 2012 at 07:36 UTC ( #990399=perlquestion: print w/ replies, xml ) Need Help??
perlnoobster has asked for the wisdom of the Perl Monks concerning the following question:

Hi Gurus, I have the following coding which loops to assign a phone model to a colour:
my $output = "phones_out.txt"; open(OUT,"+>$output"); my $PHONE_FILE = $ARGV[0] || 'phones.txt'; my @COLOURS = <DATA>; my $GET_MODEL = qr{\A ( [^\n]+ ) }x; open my $PHONES, '<', $PHONE_FILE or die "Cannot open $PHONE_FILE\n"; Case Cover"; while (my ($phn) = <$PHONES> =~ $GET_MODEL) { Do something (matches phone models to colours) }
in the phones.txt file there is a list of phones such as:
iphone samsung s2 motoroalla atrix
etc And the code runs perfectly fine, the colours in the __DATA__ section asign perfectly with the phone models. with all the above in mind I need help in regards to adding a second column into the phones.txt file, i added a column with values (seperated by tab) into the text file however that is also getting printed in the output file aswell as the first column? I want to differentiate between the columns at the beginning of the loop, is this possible? (so that i can call upon the variable at a later stage e.g $results[0] = phone model (column 1) $results1 = phone style (column 2) I hope this makes sense? please can someone help me? Thank you :)

Comment on Separate column results?!
Select or Download Code
Re: Separate column results?!
by choroba (Abbot) on Aug 29, 2012 at 07:50 UTC
    What is this Case Cover"; on line 8 of your code?

    You should really read some books on Perl. We are not going to do all your work for you.

    To parse the tab separated columns, you might profit from

    split /\t/, $line;
    See split.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Hi Choroba, Thank you for replying, please ignore that line, I've taken the split suggestion on board and have come up with the following :) :
      #!/usr/bin/perl use warnings; use strict; my $PHONE_FILE = 'phones.txt'; open(PHONE_FILE, $PHONE_FILE) or die "Can't open file: $!\n"; my @lines = <PHONE_FILE>; foreach $_ (@lines) { my @columns = split('\t', $_); my $col1 = $columns[0]; my $col2 = $columns[1]; chomp $col1; print "\$columns[0]\t$columns[1]\n"; }
      The data used in the text file is the following:
      Apple iPhone 4 test Apple iPhone 4s test1 Apple iPhone 3G test2 Apple iPhone 3GS test3
      The output is:
      $columns[0] test $columns[0] test1 $columns[0] test2 $columns[0] test3
      Please can you help me fix this? the output should be the phone name and not $columns[0]?? Thank you :)
        update: I've changed it to the following:
        print "\n$columns[0]\t$columns[1]";
        works fine now :) however there is a blank space between the lines the chomp function doesn't seem to work?
        #!/usr/bin/perl use warnings; use strict; my $PHONE_FILE = 'phones.txt'; open(PHONE_FILE, $PHONE_FILE) or die "Can't open file: $!\n"; my @lines = <PHONE_FILE>; foreach $_ (@lines) { my @columns = split('\t', $_); my $col1 = $columns[0]; my $col2 = $columns[1]; chomp $col1; chomp $col2; print "\n$columns[0]\t$columns[1]"; }
        ??
Re: Separate column results?!
by pvaldes (Chaplain) on Aug 29, 2012 at 17:28 UTC

    And the (not shown) code runs perfectly fine

    great

    the colours in the __DATA__ section asign perfectly with the phone models.

    can't see this section in the post. How is this data section structured?

    ...to adding a second column into the phones.txt file

    Already with two columns... but only in some rows

    iphone samsung s2 Apple iPhone 4s test1

    (Well in fact, with a number of columns between one and four, maybe more depending on the name of the phone, who knows?)

    I added a column with values (seperated by tab) into the text file however that is also getting printed in the output file aswell as the first column? I want to differentiate between the columns at the beginning of the loop... I hope this makes sense? please can someone help me?

    I read it three times and still don't understand your post, sorry, what is the question?. You can not use split like this if you have a variable number of columns in each row. Can we see a sample of your data?

    Let see:

    #!/usr/bin/perl use warnings; use strict; open (my $PHONE_FILE, '<', 'phones.txt') or die "Can't open file: $!\n +"; while(<$PHONE_FILE>) { chomp; my ($col1,$col2,@columns) = split /\t/, $_; print "$col1\t$col2\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://990399]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (14)
As of 2014-08-28 15:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (263 votes), past polls