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

dear friends, I tryied to print my array using map and it works like this:
map { print "$_\n"; } @peolocorg_tagged;
output:
w1 w2 w3 [t4 w4] w5 . . .
I want to print like this :
w1 w2 w3 w4 t4 w5 . . .
it means i want to print w4 and then the mapped value. is there any suggestion? I do really appreciate your ideas.

Replies are listed 'Best First'.
Re: printing array using map
by oko1 (Deacon) on Apr 27, 2008 at 17:14 UTC

    Is there a reason you insist on using 'map'? An explicit 'for' loop would be more legible. Not that I dislike 'map', but once past a certain degree of complexity, you really should break it out.

    #!/usr/bin/perl -wl use strict; print for map { y/[]//d; join " ", reverse split } <DATA>; __DATA__ w1 w2 w3 [t4 w4] w5

    Update: My assumption is the opposite of netwallah's, since 'map' would print an arrayref as 'ARRAY(0xDEADBEEF)' rather than '[foo bar]'.

    
    -- 
    Human history becomes more and more a race between education and catastrophe. -- HG Wells
    
      Actually lets make this more clear, let's say I have an text file like this :
      w1 w2 w3 [t4 w4] w5 w6
      and I want to process it to have something like this in output :
      w1 w2 w3 w4 t4 w5 w6
      each w is a token and t is a lable for some tokens. I've tried this but it dosnt work and I get the same output.
      use strict; my $file = $ARGV[0]; open IN, $file or die "Can't open"; my @file =<IN>; close IN; m/\[(\S+)\s(\S+)\]/ ? print qq[$2 $1\n] :print qq[$_] for @file;
      while my file is a text with my data as below.
      w1 w2 w3 [t4 w4] w5 w6
        Firstly, using the readline operator with no filehandle defaults to opening and reading the files supplied as command-line arguments so you don't need to open the filehandle yourself. Secondly, you can use just one capture in the regex by using a negated character class, in other words capture zero or more non closing square brackets.

        use strict; use warnings; print map { chomp; s{^\[([^\]]*)\]}{$1}; qq{$_\n}; } <>;

        Running it gives

        J:\johngg\Scripts\Monks>type spw683152.dat w1 w2 w3 [t4 w4] w5 w6 J:\johngg\Scripts\Monks>perl spw683152 spw683152.dat w1 w2 w3 t4 w4 w5 w6

        I hope this is useful.

        Cheers,

        JohnGG

        In what way does oko1's solution not work for you?

        Using your input of:

        w1 w2 w3 [t4 w4] w5

        produces:

        w1 w2 w3 w4 t4 w5

        using an elegance I'd be proud of. Is that not what you're after?


        Unless I state otherwise, my code all runs with strict and warnings
Re: printing array using map
by NetWallah (Canon) on Apr 27, 2008 at 17:06 UTC
    It is not clear whether "[t4 w4]" is an array reference, or plain text. Assuming the latter :
    my @a=(qw(w1 w2 w3),"[t4 w4]" ,qw(w5 w6)); m/\[(\S+)\s(\S+)\]/ ? print qq[$2 $1\n] :print qq[$_\n] for @a
    FYI - it is pointless using "map" in a void context - "for" is preferable.

    If you have an array ref, the code is:

    my @a=(qw(w1 w2 w3),["t4","w4"],qw(w5 w6)); ref $_ ? print "$_->[1] $_->[0]\n" :print qq[$_\n] for @a
    Of course, for production-level code, you would do a lot more error-checking.

         "How many times do I have to tell you again and again .. not to be repetitive?"