Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

key and values help

by Anonymous Monk
on Aug 07, 2004 at 03:55 UTC ( [id://380839]=perlquestion: print w/replies, xml ) Need Help??

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

Hello everyone, I have an output of a command in this format,
log1 123 log1 233 log1 223 log2 465 log2 231 log2 456
I need help to put the above output like this
log1 123 233 223 log2 465 231 456

Edited by davido: Used code tags so that Anonymous Monk's post would show the crucial tabular format which is lost without the use of proper HTML formatting tags.

Replies are listed 'Best First'.
Re: key and values help
by davido (Cardinal) on Aug 07, 2004 at 04:04 UTC

    Well, is the input in the form of one big string, or is it in some other form? I'll assume it's a string.

    my $input = "log1 123 log1 233 log1 223 log2 465 log2 231 log2 456"; my @items = split /\s+/, $input; my %groups; while ( @items ) { push @{$groups{ +shift( @items ) }}, shift( @items ); } print "$_ @{$groups{$_}}\n" foreach sort keys %groups;

    Dave

      I like your solution, but I'm not sure what + is doing in this line:
      push @{$groups{ +shift( @items ) }}, shift( @items );
      I found this in perlop

      Unary "+" has no effect whatsoever, even on strings. It is useful syntactically for separating a function name from a parenthesized expression that would otherwise be interpreted as the complete list of function arguments.

      Is that what you were doing there? Making sure Perl knew that the return value from shift( @items ) was not part of an argument list?


      dsb
      This @ISA my cool %SIG

        You are close. Usually Perl sees any bareword placed in the position of a hash key as literal text, the actual key. The plus forces Perl to see it as an expression; in this case, a function, and evaluate it as a function using its return value as the hash's key.


        Dave

      hello Davido, that was a quick reply but the input is in tabular form like the one I pasted log1 123 log1 233 log1 223 log2 465 log2 231 log2 456

        With a little ambition you might have taken my example and run with it, adapting it to your needs. But here's the updated version that more closely accomplishes what you're asking, now that the details are known. Honestly, the only significant change was to how I formatted the output of the sample script. The script was already capable of handling data formatted in just about any way, so long as the formatting is accomplished with what Perl thinks of as space (\n\t\r and ' ')

        use strict; use warnings; my $input = <<HERE; log1 123 log1 233 log1 223 log2 465 log2 231 log2 456 HERE my %groups; { my @items = split /\s+/, $input; while ( @items ) { push @{$groups{ +shift( @items ) }}, shift( @items ); } } { local $" = "\t"; print "$_\n@{$groups{$_}}\n\n" foreach sort keys %groups; } __OUTPUT__ log1 123 233 223 log2 465 231 456

        My second example uses a HERE doc to illustrate input in the form of a tabular list. Also, I played around with scoping a bit to keep variables confined to narrower scopes... not really necessary, but you've got to let me have a little fun with it too. ;)

        If this is your first post to the Monastery, let me point out that when you post nodes, your input needs HTML-like markup, or else it will lose all formatting. Prior to your initial post being edited by a janitor (me), it was impossible to tell that your input data was in a tabular format. After adding simple code tags, it became apparent that you were presenting tabular format data. Please do read the PerlMonks FAQ for details on formatting your posts with PerlMonks HTML tags.


        Dave

Re: key and values help
by murugu (Curate) on Aug 07, 2004 at 04:48 UTC

    Hi,

    The code is,

    while (<DATA>) { chomp; my @a=split/\s+/; push @{$h{$a[0]}},$a[1]; } $"="\t"; print "$_\n@{$h{$_}}\n\n" for sort keys %h; __DATA__ log1 123 log1 233 log1 223 log2 465 log2 231 log2 456

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-23 22:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found