Tie::Handle::CSV Help...

Tie::Handle::CSV Help...

am 23.07.2006 20:00:36 von lancerset

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'};
}

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

am 23.07.2006 23:16:43 von mumia.w.18.spam+nospam.usenet

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;

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

am 24.07.2006 04:44:06 von lancerset

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;

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

am 24.07.2006 16:39:51 von lancerset

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;

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

am 24.07.2006 18:53:35 von mumia.w.18.spam+nospam.usenet

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.

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

am 25.07.2006 00:09:59 von Jim Gibson

In article <1153751991.227975.112520@p79g2000cwp.googlegroups.com>,
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";
>
> 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 **
----------------------------------------------------------
http://www.usenet.com