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

Hey Monks, I am writing a script which read a text file and sorts the data in a more clinical way. i will explain in detail. say for ex we have a file called foo.txt. foo.txt contains the following:

The Rock|Cheer|Rock Bottom Triple H|Boo|Pedigree Stone Cold|Cheer|Stone Cold Stunner

Now my code should read the text file and sort the data in this way:

Wrestlername: The Rock Crowdreaction: Cheer Specialmove: RockBottom

i could only get this far. please let me know your ideas on how to do it.

#!/usr/bin/perl -w open(Handle,"D:\\scripts\\read.txt") || die "cannot read the file: $!" +; chomp(@row_data=<Handle>); close(Handle); print join (',',@row_data);
The output is as follows: The Rock|Cheer|Rock Bottom,Triple H|Boo|Pedigree,Stone Cold|Cheer|Ston +e Cold Stunner

Thanks
Irishboy

Replies are listed 'Best First'.
Re: Script issue involves File Handling.
by toolic (Bishop) on Oct 01, 2009 at 20:13 UTC
    One way is to use split:
    use strict; use warnings; while (<DATA>) { chomp; my @tokens = split /\|/; print "Wrestlername: $tokens[0]\n"; print "Crowdreaction: $tokens[1]\n"; print "Specialmove: $tokens[2]\n\n"; } __DATA__ The Rock|Cheer|Rock Bottom Triple H|Boo|Pedigree Stone Cold|Cheer|Stone Cold Stunner

    prints out:

    Wrestlername: The Rock Crowdreaction: Cheer Specialmove: Rock Bottom Wrestlername: Triple H Crowdreaction: Boo Specialmove: Pedigree Wrestlername: Stone Cold Crowdreaction: Cheer Specialmove: Stone Cold Stunner
      awesome! ..thanks toolic.

      hey toolic
      quick question,In the script while reads line by line rite? Hence the array @tokens changes every time the script reads a new line.

      thanks
      Irishboy
        while reads line by line rite?
        Yes.
        the array @tokens changes every time the script reads a new line.
        Correct.
Re: Script issue involves File Handling.
by ww (Archbishop) on Oct 01, 2009 at 20:47 UTC

    Another variant:

    #!/usr/bin/perl use strict; use warnings; # 798719 print "Wrestlername Crowdreaction Specialmove\n"; while ( <DATA> ) { print join("\t", split(/\|/)); } __DATA__ The Rock|Cheer|Rock Bottom Triple H|Boo|Pedigree Stone Cold|Cheer|Stone Cold Stunner

    However, toolic's output is more readable. Tabular output from the above suffers because the field_lengths are so uneven:

    Wrestlername Crowdreaction Specialmove The Rock Cheer Rock Bottom Triple H Boo Pedigree Stone Cold Cheer Stone Cold Stunner
      Tabular output from the above suffers because the field_lengths are so uneven
      I usually reach for Text::Table for nicely formatted tables.
Re: Script issue involves File Handling.
by biohisham (Priest) on Oct 02, 2009 at 09:38 UTC
    How about going surgical instead of clinical? suppose you got 150 wrestlers names, and that you're supposed to sort them alphabetically and you wanted to treat the combination of the wrestler's name, the cheer, the killer move as well as any other additional parameters as a unified data structure that can be expanded to contain other information about the same wrestler, examples of these data can be descriptions about his age, weight in pounds, winning matches, lost games, the association he's member of, a yes/no thingy if he was kicked in the butt by the Undertaker...etc.

    Consider getting an insight into References, they're so powerful. Not belittling the other solutions here by toolic and ww for they are far more readable and constructed and these guys are more experienced than humble me, but I am showing you a TIMTOWTDI thing cuz a surgeon needs to know what there in the guts is :p

    Here we go:

    #!/usr/local/bin/perl use strict; use warnings; my %hash; while(<DATA>){ chomp; my ($name,$crowdReaction,$killerMove)=split /\|/; $hash{$name}=[] unless exists $hash{$name}; #to skip duplicate + wrestlers names just in case. push @{$hash{$name}}, $crowdReaction,$killerMove; } print "WRESTLER\tCrowd Reaction\tKiller Move\n"; print "_" x 50, "\n"; foreach my $name (sort keys %hash){ for(my $i=0;$i< length(@{$hash{$name}});$i++){ print "$name\t@{$hash{$name}}[$i]\t\t@{$hash{$name}}[+ ++$i]\n"; } } __DATA__ The Rock|Cheer|Rock Bottom Triple H|Boo|Pedigree Stone Cold|Cheer|Stone Cold Stunner
    WRESTLER Crowd Reaction Killer Move __________________________________________________ Stone Cold Cheer Stone Cold Stunner The Rock Cheer Rock Bottom Triple H Boo Pedigree
    N.B: The way you used the open function to open a file, you haven't provided arguments specifying if the file has been opened in modes of input, output, append...

    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.