Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

print datafields separated by tab

by bandya (Initiate)
on Aug 23, 2003 at 08:09 UTC ( [id://286025]=perlquestion: print w/replies, xml ) Need Help??

bandya has asked for the wisdom of the Perl Monks concerning the following question:

Hi!
I am trying to print datafields as
field1    field2   field3
etc. by:
for($count=1;$count<=$nfields;$count++) { print $fieldname[$count]; print "\t"; }
but it gets printed like
field1
          field2
          field3
can someonle please tell me my mistake?
thanks

edited by ybiC: <code> tags and formatting

Replies are listed 'Best First'.
Re: print datafields separated by tab
by thinker (Parson) on Aug 23, 2003 at 09:22 UTC

    Hi bandya,

    It looks as though your fieldnames have newlines embedded at the end.

    If this is the case,what you want to do is

    for (@fieldname){ chomp; # see perldoc -f chomp print "$_\t"; }

    though it would be better to chomp fieldname when you put it into @fieldname.

    Alternatively, combine the chomp with allolex's join method, and use

    print join "\t", map { chomp; $_ }  @fieldname;

    hope this helps

    thinker

Re: print datafields separated by tab
by allolex (Curate) on Aug 23, 2003 at 08:11 UTC

    Assuming an array @fieldname,

    print join("\t",@fieldname);

    Update: I should probably add that your C-style for loop is very ugly in Perl. Have a look at the functions for, foreach, while in perldoc. :)

    --
    Allolex

Re: print datafields separated by tab
by davido (Cardinal) on Aug 23, 2003 at 16:02 UTC
    Your data has newline characters at the end of each element. For example, you think you are looking at...

    ABCD EFGH GHIJ

    But what you've really got in your array is:

    ABCD\n EFGH\n GHIJ\n

    The first print statement prints one element, with a newline. The second statement prints a single tab on the new line. On the next iteration, the next field is printed on that same new line, and another newline is output. Then another tab, from the next new line, and so on. So you're always starting over with a new line, and always tabbing from the beginning of that new line.

    You should be chomping the data. If you don't really care or need for it to have newline characters in it, chomp them as the data is slurped into the array.

    Try this:

    my @array; while ( my $line = <STDIN> ) { chomp $line; push @array, $line; } #.... later on... foreach ( @array ) { print "$_\t"; }

    A slightly more obfuscated way of printing the list, while avoiding using a foreach (or for) loop could be:

    { local $, = "\t"; print @array, "\n"; }

    The preceeding example sets the output field separator, contained in $, to the tab character. Just don't forget to switch it back or you'll have a mess in other places in the program.

    Hope this helps...

    UPDATE: Egads, too early in the morning. Code has been fixed. Thanks for the head's up. Duh!

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

      Hi davido,

      This is wrong in (at least) 2 regards. Firstly, the syntax is wrong in the while loop. What you needed was

      while (my $line = <STDIN> ) {

      Secondly, chomp does not return what you think it does. It returns the number of characters it removes, in your case 1 for each iteration. So your final result will be like

      1    1    1

      Hope this helps

      thinker

        Wiping sleepy sand out of eyes..... Thanks for the head's up. I've fixed what should have been simple code. That will teach me to cut the 'test it first' corner. ;)

        By the way, you can see my use of chomp's seldom-used return value here: Chomped JAPH.

        Thanks again.

        Dave

        "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Re: print datafields separated by tab
by bart (Canon) on Aug 23, 2003 at 22:51 UTC
    The simplest way to achieve what you want is to use the special variables $\ and $,:
    @field = qw(look at me now); @field2 = qw(this is so neat); local($\, $,) = ("\n", "\t"); print @field; # first row print @field2; # second row
    Resulting in:
    look at me now this is so neat

    About your original problem: I think your fields data ends in newlines.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-04-16 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found