Tie::Handle::CSV Help...

Ask a Question related to PERL Modules, Design and Development.

  1. #1

    Default Tie::Handle::CSV Help...

    Hello All,,

    I need some assistance please, i've been fussing with this for a while
    but i'm stuck.

    What i need here is, while my Style column in my .csv file continues to
    be the same number, take the corresponding number in the Size column,
    and sort it ,separated by a pipe. for as long as the number in the
    Style column continues to be the same. So the Style column contains
    numbers like 123456, and the Size column are numbers like 8, 9.5, 7
    etc...Before and After should look like this:

    Before:
    123456 8
    123456 10
    123456 8.5
    838493 12
    838493 10
    838494 9

    After:
    123456 8|8.5|10|11
    838493 9|10|12

    etc etc....

    I have the following so far which isn't much, I've
    only put the values from the 2 columns into separate variables.
    Any suggestions please, i'm still pretty at this kind of stuff,,.
    Thanks again,

    $csv_fh = Tie::Handle::CSV->new('myfile.csv');

    while (<$csv_fh>){
    $StyleField = ( scalar <$csv_fh> )->{'Style'};
    $SizeField = ( scalar <$csv_fh> )->{'Size'};
    }

    onlineviewer Guest

  2. Similar Questions and Discussions

    1. Can I Handle Error 404 JUST Using ASP ?
      understand that through IIS you can implement custom error messages, but since we are in hosted environment, we need to implement custom error...
    2. handle the IE.
      Hello, I need to show full screen, when IE is shown. Can I handle the appearence of IE by some flash commands ? Also, can I hanlde the html,...
    3. [PHP] Not sure of the best way to handle a problem
      Actually it does have to do with php.One of my main questions was also about whether it was best to use the script from the command-line or from...
    4. I cannot handle Tables!
      I get very surprized when I set everything accurately and corrently in Dremweaver and I get a bad result in the browser! How is this possible to set...
    5. handle file > 2GB
      Hi there, Anyone know how to handle file size > 2GB in SUN? I called stat but it fail with EOVERFLOW if the file > 2GB. TIA
  3. #2

    Default Re: Tie::Handle::CSV Help...

    On 07/23/2006 01:00 PM, onlineviewer wrote:
    > Hello All,,
    >
    > I need some assistance please, i've been fussing with this for a while
    > but i'm stuck.
    >
    > What i need here is, while my Style column in my .csv file continues to
    > be the same number, take the corresponding number in the Size column,
    > and sort it ,separated by a pipe. for as long as the number in the
    > Style column continues to be the same. So the Style column contains
    > numbers like 123456, and the Size column are numbers like 8, 9.5, 7
    > etc...Before and After should look like this:
    >
    > Before:
    > 123456 8
    > 123456 10
    > 123456 8.5
    > 838493 12
    > 838493 10
    > 838494 9
    >
    > After:
    > 123456 8|8.5|10|11
    > 838493 9|10|12
    >
    > etc etc....
    >
    > I have the following so far which isn't much, I've
    > only put the values from the 2 columns into separate variables.
    > Any suggestions please, i'm still pretty at this kind of stuff,,.
    > Thanks again,
    >
    > $csv_fh = Tie::Handle::CSV->new('myfile.csv');
    >
    > while (<$csv_fh>){
    > $StyleField = ( scalar <$csv_fh> )->{'Style'};
    > $SizeField = ( scalar <$csv_fh> )->{'Size'};
    > }
    >
    You could create a hash to store the styles as keys and the
    sizes as values; then you'd push each size into the hash
    (using autovivication) as it's found:

    my %hash;
    ...
    push @{$hash{$StyleField}}, $SizeField;
    Mumia W. Guest

  4. #3

    Default Re: Tie::Handle::CSV Help...

    Thanks for the sponse,

    I'm not sure where in my script this push function would go, i'm
    confused.
    I see what needs to happens, but i'm not sure how to get
    there,,.Suggestions
    please,,.

    $csv_fh = Tie::Handle::CSV->new('csvfile.csv');
    $\ = "|";

    while(<$csv_fh>){
    $StyleField = ( scalar <$csv_fh> )->{'Style'};
    $SizeField = ( scalar <$csv_fh> )->{'Size'};
    push @{$hash{$StyleField}}, $SizeField;
    }


    Mumia W. wrote:
    > On 07/23/2006 01:00 PM, onlineviewer wrote:
    > > Hello All,,
    > >
    > > I need some assistance please, i've been fussing with this for a while
    > > but i'm stuck.
    > >
    > > What i need here is, while my Style column in my .csv file continues to
    > > be the same number, take the corresponding number in the Size column,
    > > and sort it ,separated by a pipe. for as long as the number in the
    > > Style column continues to be the same. So the Style column contains
    > > numbers like 123456, and the Size column are numbers like 8, 9.5, 7
    > > etc...Before and After should look like this:
    > >
    > > Before:
    > > 123456 8
    > > 123456 10
    > > 123456 8.5
    > > 838493 12
    > > 838493 10
    > > 838494 9
    > >
    > > After:
    > > 123456 8|8.5|10|11
    > > 838493 9|10|12
    > >
    > > etc etc....
    > >
    > > I have the following so far which isn't much, I've
    > > only put the values from the 2 columns into separate variables.
    > > Any suggestions please, i'm still pretty at this kind of stuff,,.
    > > Thanks again,
    > >
    > > $csv_fh = Tie::Handle::CSV->new('myfile.csv');
    > >
    > > while (<$csv_fh>){
    > > $StyleField = ( scalar <$csv_fh> )->{'Style'};
    > > $SizeField = ( scalar <$csv_fh> )->{'Size'};
    > > }
    > >
    >
    > You could create a hash to store the styles as keys and the
    > sizes as values; then you'd push each size into the hash
    > (using autovivication) as it's found:
    >
    > my %hash;
    > ...
    > push @{$hash{$StyleField}}, $SizeField;
    onlineviewer Guest

  5. #4

    Default Re: Tie::Handle::CSV Help...

    Hello Again,

    If anyone has any suggestions for me please,,.
    I'm at this point:

    while(<$csv_fh>){
    $SizeField = ( scalar <$csv_fh> )->{'Size'};
    $StyleField = ( scalar <$csv_fh> )->{'Style'};
    push @{$MYHASH{$StyleField}}, $SizeField;
    print "$StyleField => $SizeField\n";

    which produces this output:

    123345 => 8.5
    123345 => 10.5
    555443 => 12
    555443 => 7
    555443 => 8.5

    I need the output to look like this:
    123345 => 8.5|10.5
    555443 => 7|8.5|12






    onlineviewer wrote:
    > Thanks for the sponse,
    >
    > I'm not sure where in my script this push function would go, i'm
    > confused.
    > I see what needs to happens, but i'm not sure how to get
    > there,,.Suggestions
    > please,,.
    >
    > $csv_fh = Tie::Handle::CSV->new('csvfile.csv');
    > $\ = "|";
    >
    > while(<$csv_fh>){
    > $StyleField = ( scalar <$csv_fh> )->{'Style'};
    > $SizeField = ( scalar <$csv_fh> )->{'Size'};
    > push @{$hash{$StyleField}}, $SizeField;
    > }
    >
    >
    > Mumia W. wrote:
    > > On 07/23/2006 01:00 PM, onlineviewer wrote:
    > > > Hello All,,
    > > >
    > > > I need some assistance please, i've been fussing with this for a while
    > > > but i'm stuck.
    > > >
    > > > What i need here is, while my Style column in my .csv file continues to
    > > > be the same number, take the corresponding number in the Size column,
    > > > and sort it ,separated by a pipe. for as long as the number in the
    > > > Style column continues to be the same. So the Style column contains
    > > > numbers like 123456, and the Size column are numbers like 8, 9.5, 7
    > > > etc...Before and After should look like this:
    > > >
    > > > Before:
    > > > 123456 8
    > > > 123456 10
    > > > 123456 8.5
    > > > 838493 12
    > > > 838493 10
    > > > 838494 9
    > > >
    > > > After:
    > > > 123456 8|8.5|10|11
    > > > 838493 9|10|12
    > > >
    > > > etc etc....
    > > >
    > > > I have the following so far which isn't much, I've
    > > > only put the values from the 2 columns into separate variables.
    > > > Any suggestions please, i'm still pretty at this kind of stuff,,.
    > > > Thanks again,
    > > >
    > > > $csv_fh = Tie::Handle::CSV->new('myfile.csv');
    > > >
    > > > while (<$csv_fh>){
    > > > $StyleField = ( scalar <$csv_fh> )->{'Style'};
    > > > $SizeField = ( scalar <$csv_fh> )->{'Size'};
    > > > }
    > > >
    > >
    > > You could create a hash to store the styles as keys and the
    > > sizes as values; then you'd push each size into the hash
    > > (using autovivication) as it's found:
    > >
    > > my %hash;
    > > ...
    > > push @{$hash{$StyleField}}, $SizeField;
    onlineviewer Guest

  6. #5

    Default Re: Tie::Handle::CSV Help...

    On 07/24/2006 09:39 AM, onlineviewer wrote:
    > Hello Again,
    >
    > If anyone has any suggestions for me please,,.
    > I'm at this point:
    >
    > while(<$csv_fh>){
    > $SizeField = ( scalar <$csv_fh> )->{'Size'};
    > $StyleField = ( scalar <$csv_fh> )->{'Style'};
    > push @{$MYHASH{$StyleField}}, $SizeField;
    > print "$StyleField => $SizeField\n";
    > [...]
    Your well-written while loop puts some pretty interesting
    things into %MYHASH. Have you taken a look into %MYHASH lately?

    Typically what you do when programming is to write some code
    that fills a data structure with data. Then, because there are
    many opportunities to write bugs that mess up the data, you
    print out the data structure to make sure that it's right.

    So naturally, after putting "push @{$MYHASH..." into your
    while loop, you'd print the contents of %MYHASH (after the
    while loop) to make sure the data is all there. I happen to
    think that it is.

    The lazy person's (my) way of printing a complex data
    structure is to use the Data::Dumper module. Read the doc on
    Data::Dumper: "perldoc Data::Dumper". Use Data::Dumper to
    display the contents of %MYHASH.


    Mumia W. Guest

  7. #6

    Default Re: Tie::Handle::CSV Help...

    In article <1153751991.227975.112520@p79g2000cwp.googlegroups .com>,
    onlineviewer <lancerset@gmail.com> wrote:
    > Hello Again,
    >
    > If anyone has any suggestions for me please,,.
    > I'm at this point:
    >
    > while(<$csv_fh>){
    > $SizeField = ( scalar <$csv_fh> )->{'Size'};
    > $StyleField = ( scalar <$csv_fh> )->{'Style'};
    > push @{$MYHASH{$StyleField}}, $SizeField;
    > print "$StyleField => $SizeField\n";
    >
    > which produces this output:
    >
    > 123345 => 8.5
    > 123345 => 10.5
    > 555443 => 12
    > 555443 => 7
    > 555443 => 8.5
    >
    > I need the output to look like this:
    > 123345 => 8.5|10.5
    > 555443 => 7|8.5|12

    #!/usr/local/bin/perl
    use strict;
    use warnings;

    my %myhash = (
    '123345' => [ 8.5, 10.5 ],
    '555443' => [ 12, 7, 8.5 ]
    );

    for my $key ( sort keys %myhash ) {
    print "$key => ", join('|',sort {$a<=>$b} @{$myhash{$key}} ),"\n";
    }

    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    [url]http://www.usenet.com[/url]
    Jim Gibson Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139