Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Can't / won't print pipe delimiter?

by punch_card_don (Curate)
on May 18, 2009 at 16:34 UTC ( [id://764667]=perlquestion: print w/replies, xml ) Need Help??

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

Memographic Monks,

I have

@my_list = (3, 5, 2, 9); for $i (0 ..$#my_list) { print $my_list[$i]."|"; }
outputs
3529
If I change the print to
print $my_list[$i]."XX";
it outputs
3XX5XX2XX9XX
If I change it to
print $my_list[$i]."|n";
it outputs
3|n5|n2|n9|n
But it will not output
3|5|2|9|
as I want. Not even if I try
print $my_list[$i]."\|";

I know, pipe, perl control character....what's the trick to get it to print what I want?

Thanks.

UPDATE

Aie - holiday Monday programming - ok, time to take my lumps -

the code I provided was a short form for the real code which was

@my_list = (3, 5, 2, 9); for $i (0 ..$#my_list) { ... a bunch of stuff... $my_id_list .= $my_list[$i]."|"; ... a bunch more stuff... } print $my_id_list;
where more scanning the code revealed this helpful little line
#trim trailing delimiter $my_id_list =~ s/\|$//;
in ...a bunch more stuff..., which of course was the wrong place for it. Should'a been after the lop and before the print.

So, lessons #3,673,592 & #3,673,593: don't code half asleep on a holiday Monday; post real code or at least a sanitized version of it, not shortcuts.

Let the flagellation begin.




Time flies like an arrow. Fruit flies like a banana.

Replies are listed 'Best First'.
Re: Can't / won't print pipe delimiter?
by Old_Gray_Bear (Bishop) on May 18, 2009 at 16:42 UTC
    Try
    print join('|',@my_list);

    ----
    I Go Back to Sleep, Now.

    OGB

      Almost perfect!
      just need one final | to get output requested.
      #!/usr/bin/perl -w use strict; my @list = (3, 5, 2, 9); print join('|',@list),'|'; #prints: 3|5|2|9|
      print join "\x7c", @my_list; # :D print join chr 124, @my_list; # :D
Re: Can't / won't print pipe delimiter?
by talexb (Chancellor) on May 18, 2009 at 17:45 UTC

    I would just have used map to make that a lot more simple:

    print map { "$_|" } @my_list

    And a design pattern that you should start to recognize is that as soon as you see the concatenation operator, you should be thinking about changing that to a list. So

    $my_id_list .= $my_list[$i]."|";
    should change to
    push ( @my_id, "$my_list[$i]|" );
    Later, after you're done with the loop, you can join all of the list elements together.

    And if your code also removes the last "|" from the strong that you're building, then you can do

    push ( @my_id, $my_list[$i] );
    during the loop and
    join('|',@my_id);
    at the end of the loop to get the same result.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      Create an array, multiple pushes, then a join, instead of multiple appends and a regex - - - ya, it certainly codes cleaner.

      Aren't you supposed to be in the backyard barbecuing?

          Aren't you supposed to be in the backyard barbecuing?

        Just came back from a nice long bike ride. The steaks are defrosting on the counter. Red wine is open. Grill at 6pm, then .. hockey's on at 730pm. Happy May 24!!

        Alex / talexb / Toronto

        "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Can't / won't print pipe delimiter?
by BrowserUk (Patriarch) on May 18, 2009 at 16:40 UTC
Re: Can't / won't print pipe delimiter?
by Anonymous Monk on May 18, 2009 at 16:38 UTC
    How are you running this program? Your shell is probably interfering, because | is not a control character
Re: Can't / won't print pipe delimiter?
by artist (Parson) on May 18, 2009 at 16:39 UTC
    It prints perfectly fine on my machine with "|". I am not sure, why you don't get that. Try '|' rather than "|". Also mention which version of perl you are using.
    --Artist
      Try '|' rather than "|".

      What would you expect that to do? Are you thinking of the pattern to split? Even in that case, it wouldn't matter -- the pipe character has no special interpolation properties in single- versus double-quoted strings.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-25 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found