in reply to Re^2: perl print to csv
in thread perl print to csv

That’s because two pairs of square brackets were lost due to the absence of <code> tags. I believe the two statements within the loop were originally as follows:

@$_ = split /(?:,|\s)+/, $row; $#$_ != 2 ? next : push @{$data{$_->[2]}}, @$_[1, 0];

The first line treats $_ as an array reference and autovivifies the array @$_, populating it with the comma-and-whitespace-separated fields in $row. In the second line, $#$_ is the index of the last element in @$_, so if it is not 2 (corresponding to 3 fields) then this data line is skipped. If exactly 3 fields were extracted from $row into @$_, the second and first fields, in that order, are pushed onto the anonymous array $data{$_->[2]}, where $_->[2] is the name field. So after running the loop with the data given, the hash %data (formatted by Data::Dump) looks like this:

{ bill => [1427766556, 5], bob => [1427766557, 12, 1427766555, 10] }

To the OP: What makes this code look strange — apart from the missing square brackets — is:

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^4: perl print to csv
by bishop2001 (Initiate) on Apr 21, 2015 at 12:16 UTC

    Greetings all, thanks for the reponses. What im trying to accomplish is this. i would like the file im working on which contains lines such as this
    52,1427766557, bob
    12,1427766556, bob
    5,1427766555, bill
    10,1427766554, joe
    23,1427766553, bob

    and turn into something like this where i can open it in excel and it would have a grid type of format where bob is the heading of the column, 12 is the value and 1427766557 is the time. 
                            bob         joe      bill

    1427766557   52

    1427766556   12

    1427766555                               5

    1427766554                   10

    1427766553   23

    @$_ = split /(?:,|\s)+/, $row;
    $#$_!=2 ? next : push @{$data{$_->2}}, @$_1,0

    with these 2 lines im splitting the row then assigning each part to an array and taking the 3rd part which is the name and setting it as the hash key and setting the other two elelents, time and value as the keys of the hash.

    thanks,

      How about

      use strict; use warnings; my %cols; my $ctr=0; my @data = map {/(.*),(.*), (.*)/; $2."\t"x($cols{"\t$3"}//=++$ctr)."$ +1\n" } <DATA>; print sort {$cols{$a}<=>$cols{$b}} keys %cols; print "\n", @data; __DATA__ 52,1427766557, bob 12,1427766556, bob 5,1427766555, bill 10,1427766554, joe 23,1427766553, bob

      Comment: headers look slightly misaligned due to usage of tab stopps. This could be resolved by monitoring the length of $2.

        that's pretty cool. what if my file looked like this? where i just want the time to appear once in the left column and update the values per each uniq name which is the 3rd part of the each string.

        0, 1429283609, 0137a653-fa74-4a52-ba4c-50f562762de1 0, 1429283609, 051512ec-e7b1-4c3d-8061-21b7fbf08d0e 0, 1429283609, 0650d772-9447-49eb-9aa3-f27b2540ce8d 0, 1429283609, 157e1b11-7e80-4547-ae18-3139fd408340 0, 1429283609, 1810844c-15f8-436a-a895-54f3277e1d49 0, 1429283609, 21d67ba6-122d-43e7-8f1b-a95fe348dfef 0, 1429283609, 4246bf2b-2847-457a-9b0e-6032b991ce0e 0, 1429283609, 54e1c264-8dbd-4ab1-a78e-be806c9913e6 0, 1429283609, 60ac1e84-abde-40db-b9c0-b1a28c0f908b 0, 1429283609, 61992ab9-5bb5-4d2f-8193-e70152099fa6 0, 1429283609, 7e35918e-a3c2-4309-8029-ae7dacb5ca0b 0, 1429283609, 89d0ae4a-c11d-4e9a-9576-5c214a8e32f6 0, 1429283609, 92426dba-d599-49a0-8c49-c3aabf670ef4 0, 1429283609, b8f74bdc-df0b-4651-9acd-fbd4a3773663 0, 1429283609, bd468bcf-9298-4e61-8994-12a0219d6c79 0, 1429283609, d89545cc-ad88-46cb-a16f-27e75521d23d 0, 1429283609, da5da8b8-2df4-4594-9c7f-6906cbf8feb8 0, 1429283609, e363ea60-c89c-44ba-a750-fce6bde688d2 0, 1429283609, f0966c0f-47cf-4da5-aacf-83c665926f3a 2, 1429283609, 063cbfbe-3414-40df-9a16-96db9006c15d 2, 1429283609, ce744bcf-c359-4835-b15f-54cfd8a046d4 8, 1429283609, 3fa53f3c-f3e8-4058-b5d2-6e62aa65591a 8, 1429283609, 5254254f-2072-42e7-aa5e-e0b44a66679c 8, 1429283609, 790995af-ab95-4ece-8e81-35a89d130cb4 8, 1429283609, 8df495c7-51dd-4d59-851c-f7bc58ba40fd 8, 1429283609, c1d57fcd-a15d-4e91-b731-6130f02f1e8e 8, 1429283609, c679e25e-3536-4714-a831-91d91da8c0f8 8, 1429283609, f94ce85f-ad95-430e-b541-697b278fa316 9, 1429283609, 0f0e377c-5cad-46a5-8801-bd21420c1948 36, 1429283609, bd9a2931-8c94-4294-b648-5d2ce28ab086 40, 1429283609, 6c8fafbe-08d9-44c9-b218-ea6663f2f0f0 11, 1429283609, 9cd96bac-d243-42e3-9851-613a42530106 3, 1429283609, 7087ccf1-0894-419f-8da3-d474542cea45 3, 1429283609, 980eea25-6b86-46fc-a27f-c11d493bac38 3, 1429283609, b04e56d6-5f9e-45c2-9189-caa610ec909c 12, 1429283609, 1c0f9560-f6ea-4f18-9771-4813fb3e0581 14, 1429283609, 956334fd-c47c-4778-bbcc-f62360ff7aa3 15, 1429283609, 31ec36aa-b648-41f7-94bf-82df49fe6074 4, 1429283609, 4013876b-118a-4051-8f4f-cd94f1efa4cd 4, 1429283609, 48510b68-5a31-485d-b9b4-3d3c0c337de5 16, 1429283609, 5d2a3ac0-e72f-403d-88ea-65ce0612dd2b 18, 1429283609, e567169e-0ac4-44df-88b1-2352f9f4d008 19, 1429283609, 1a9d2ed0-f62e-43a1-9eb2-9de39120e6cd 19, 1429283609, 1c7bc703-ecab-40ef-b340-e28051244e6c 19, 1429283609, 1f100db5-0ce8-4c1e-a2f7-3dd4763064d5 19, 1429283609, 29aa5b94-e114-4d5f-b998-5952da6f8f24 21, 1429283609, 2c33d55f-0229-48b4-b7b9-6e0fb74d9a83 21, 1429283609, 3873999a-30a6-4af0-ab38-d6babbf651ae 21, 1429283609, 41faac7d-05c5-493e-bea7-8bb15c6d1298 21, 1429283609, 4caf9c83-aeb6-44d2-ae80-b5308b51387a 21, 1429283609, 4e40c295-496d-4988-8e19-2aea3f89bf5f 21, 1429283609, 4ed1fadf-1b88-4a7d-b196-46e290bdec04 21, 1429283609, 4fc03c18-1807-45f2-99c5-f25435fc1d23 21, 1429283609, 5436ce9c-691d-495d-b1b8-68a7949371b7 21, 1429283609, 5c17ff05-a7d6-4bc1-b5e7-e6854037d806 21, 1429283609, 5c498654-f0e6-463e-85e8-6a8b845cfd5c 21, 1429283609, 60c6f333-3053-47c5-902e-11ff038e4ac5 21, 1429283609, 6f760b86-60fe-456c-8f03-3f2e77c3e46e 21, 1429283609, 7773a7c9-ab84-4518-80b0-8d82bdaa4c0f 21, 1429283609, 795a2629-ff5c-41cf-a97b-ce02e83ce2f9 21, 1429283609, 7abc187b-dcfe-4f3a-970b-ef2fc36249b9 21, 1429283609, 925bf18c-3148-46f9-a6ff-55d6be1ce613 21, 1429283609, 95d202b0-f55f-4a34-94c1-7e07494507bf 21, 1429283609, 9c8aded7-b583-4b34-ac2f-f5d473dd3bed 21, 1429283609, a74b0f6f-ac6b-4c23-bdf5-7f1c59ede4e5 21, 1429283609, aa6201c0-a37c-4ac3-a7e6-4660e0ac1f96 21, 1429283609, afdde595-6dec-497a-8e0c-2889735e0bd9 21, 1429283609, b7b9092f-3970-40a7-b160-81be342c9c7b 21, 1429283609, b926df1c-6e9d-43d7-81b7-f487e320ea06 21, 1429283609, bb168dd4-9eb5-4abe-a458-057b5fb598e3 21, 1429283609, c85a4b9d-8009-4ab7-88c0-fdb1c410140d 21, 1429283609, d6da0425-5b39-45ee-98fc-7b84051fb358 21, 1429283609, dbccfe72-a5f7-43e9-b580-f66f18bb08f5 21, 1429283609, df64c887-df52-4f41-9234-6905e2c32674 21, 1429283609, e769697d-fc12-4041-b33c-43dfb39039f3 21, 1429283609, eb6b5e28-9221-4d3d-b970-0c71b7184843 21, 1429283609, f6898def-7686-4dfb-9e01-9bce0b80bbe6 21, 1429283609, f759c2e7-ecfc-4455-a799-9281db8881f8 22, 1429283609, c8352cde-b808-46da-85c6-3ca9df652bb7 6, 1429283609, 40f32061-77d4-4446-9557-b622ac799704 6, 1429283609, 59c47b71-96fd-4d07-bb65-366fa9b7a4d7 24, 1429283609, 82d5cdc6-956e-42e9-bcd4-c0b9ba204b49 25, 1429283609, 95f8413c-a8e0-4d70-9aab-4b0f7be64178 7, 1429283609, 01c3edd3-14f0-4bf1-b29b-34547f770cb1 7, 1429283609, 7cc75412-4a93-4ee8-98ab-70b79a95d8af 7, 1429283609, acf656f0-9180-43fe-a7bf-ce34679f391f 0, 1429283608, 0137a653-fa74-4a52-ba4c-50f562762de1 0, 1429283608, 051512ec-e7b1-4c3d-8061-21b7fbf08d0e 0, 1429283608, 0650d772-9447-49eb-9aa3-f27b2540ce8d 0, 1429283608, 157e1b11-7e80-4547-ae18-3139fd408340 0, 1429283608, 1810844c-15f8-436a-a895-54f3277e1d49 0, 1429283608, 21d67ba6-122d-43e7-8f1b-a95fe348dfef 0, 1429283608, 4246bf2b-2847-457a-9b0e-6032b991ce0e 0, 1429283608, 54e1c264-8dbd-4ab1-a78e-be806c9913e6 0, 1429283608, 60ac1e84-abde-40db-b9c0-b1a28c0f908b 0, 1429283608, 61992ab9-5bb5-4d2f-8193-e70152099fa6 0, 1429283608, 7e35918e-a3c2-4309-8029-ae7dacb5ca0b 0, 1429283608, 89d0ae4a-c11d-4e9a-9576-5c214a8e32f6 0, 1429283608, 92426dba-d599-49a0-8c49-c3aabf670ef4 0, 1429283608, b8f74bdc-df0b-4651-9acd-fbd4a3773663