Matching values from array and finding length

S

sly

Hello

I have a list of values held in a .txt file, the image name and value 1
are seperated by a tab.

14img.jpg 1
14img.jpg 1
14img.jpg 1
9img.jpg 1
4img.jpg 1
4img.jpg 1
7img.jpg 1
7img.jpg 1

What I want to do is match the image names and push them onto a new
array and work out the length. So I get 4img.jpg = 2 and 14img.jpg = 3
etc

Thanks all
 
P

Paul Lalli

sly said:
I have a list of values held in a .txt file, the image name and value 1
are seperated by a tab.

14img.jpg 1
14img.jpg 1
14img.jpg 1
9img.jpg 1
4img.jpg 1
4img.jpg 1
7img.jpg 1
7img.jpg 1

What I want to do is match the image names and push them onto a new
array and work out the length. So I get 4img.jpg = 2 and 14img.jpg = 3
etc

I don't understand the correlation between "4img.jpg" and "2". Is it
the number of digits that start the string?

What have you tried so far? What isn't working for you? With what
parts do you require assistance?

splitting the image name from the "value" of 1?
perldoc -f split

Capturing the starting digit(s)?
perldoc perlre
perldoc perlretut
perldoc perlreref

Finding the length of the captured starting digit(s)?
perldoc -f length

Once you've made an attempt, feel free to post what you have if it's
not working, and someone can probably help you fix it.

Paul Lalli
 
A

Ala Qumsieh

Paul said:
I don't understand the correlation between "4img.jpg" and "2". Is it
the number of digits that start the string?

Seems to be the number of times the string '4img.jpg' appears in his txt
file. But, I could be wrong.

--Ala
 
S

Sandman

sly said:
Hello

I have a list of values held in a .txt file, the image name and value 1
are seperated by a tab.

14img.jpg 1
14img.jpg 1
14img.jpg 1
9img.jpg 1
4img.jpg 1
4img.jpg 1
7img.jpg 1
7img.jpg 1

What I want to do is match the image names and push them onto a new
array and work out the length. So I get 4img.jpg = 2 and 14img.jpg = 3
etc

#!/usr/bin/perl
use strict;
use warnings;
my %img_array;
while (<DATA>){
chomp;
my ($img, $nr) = split /\s+/;
$img_array{$img} += $nr;
}
foreach (keys %img_array){
print "$_: $img_array{$_}\n";
}
__DATA__
14img.jpg 1
14img.jpg 1
14img.jpg 1
9img.jpg 1
4img.jpg 1
4img.jpg 1
7img.jpg 1
7img.jpg 1


OUTPUT:

7img.jpg: 2
4img.jpg: 2
9img.jpg: 1
14img.jpg: 3

NOTE:

I split on /\s+/, which is one or more whitespaces (tab, space, etc) which
works better with my example sent through usenet that expands my tabs to
spaces. You could of course change that to /\t/.
 
S

Sandman

Paul Lalli said:
I don't understand the correlation between "4img.jpg" and "2". Is it
the number of digits that start the string?

What have you tried so far? What isn't working for you? With what
parts do you require assistance?

splitting the image name from the "value" of 1?
perldoc -f split

Capturing the starting digit(s)?
perldoc perlre
perldoc perlretut
perldoc perlreref

Finding the length of the captured starting digit(s)?
perldoc -f length

Once you've made an attempt, feel free to post what you have if it's
not working, and someone can probably help you fix it.

I think what the OP wanted to do was make the image list unique to the image
names, with the numbers added up for each image so that:

14img.jpg 1
14img.jpg 1
14img.jpg 1

Became

14img.jpg 3
 
W

William James

sly said:
Hello

I have a list of values held in a .txt file, the image name and value 1
are seperated by a tab.

14img.jpg 1
14img.jpg 1
14img.jpg 1
9img.jpg 1
4img.jpg 1
4img.jpg 1
7img.jpg 1
7img.jpg 1

What I want to do is match the image names and push them onto a new
array and work out the length. So I get 4img.jpg = 2 and 14img.jpg = 3
etc

Thanks all

In Ruby:

h = {}; array = DATA.to_a
array.each{|x|
if !h.key?(x)
puts "#{x.sub(/\s.*/m, "")} #{array.grep(x).size}"
h[x]=1
end
}

__END__
14img.jpg 1
14img.jpg 1
14img.jpg 1
9img.jpg 1
4img.jpg 1
4img.jpg 1
7img.jpg 1
7img.jpg 1
 
W

William James

William said:
In Ruby:

h = {}; array = DATA.to_a
array.each{|x|
if !h.key?(x)
puts "#{x.sub(/\s.*/m, "")} #{array.grep(x).size}"
h[x]=1
end
}

__END__
14img.jpg 1
14img.jpg 1
14img.jpg 1
9img.jpg 1
4img.jpg 1
4img.jpg 1
7img.jpg 1
7img.jpg 1

h = {}; array = DATA.to_a
array.each{|x|
if !h.key?(x)
puts "#{x[/\w+/]} #{array.grep(x).size}"
h[x]=1
end
}
 
J

John W. Krahn

sly said:
I have a list of values held in a .txt file, the image name and value 1
are seperated by a tab.

14img.jpg 1
14img.jpg 1
14img.jpg 1
9img.jpg 1
4img.jpg 1
4img.jpg 1
7img.jpg 1
7img.jpg 1

What I want to do is match the image names and push them onto a new
array and work out the length. So I get 4img.jpg = 2 and 14img.jpg = 3
etc

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

my %data;
while ( <DATA> ) {
my ( $key ) = split or next;
if ( %data && !exists $data{ $key } ) {
print join( "\t", %data ), "\n";
%data = ( $key, 0 );
}
$data{ $key }++;
}
print join( "\t", %data ), "\n";

__DATA__
14img.jpg 1
14img.jpg 1
14img.jpg 1
9img.jpg 1
4img.jpg 1
4img.jpg 1
7img.jpg 1
7img.jpg 1




John
 
S

sly

Thanks to to everyone who replied on this.

Have this now working using the following:

my %img_array;

open(ODB, "<votecount_data.txt") || Error ('open','file');

while (<ODB>){
chomp;
my ($img, $nr) = split /\t/;
$img_array{$img} += $nr;
}

close(ODB);

mime();

foreach (keys %img_array){

print qq ($_: $img_array{$_}<br />\n);

}

I am getting a werid : 0 printed out as well but not too worried about
this.

Can someone explain this line to me please:

$img_array{$img} += $nr;

what is {$img} doing ?

What is the best way to now compare the results and find the higest ? I
normally just use if etc with constants to test conditions but don't
know how to do this for variables:

if ($img_array{$_} > $img_array{$_}) { }; ?????

Thanks again.
 
C

ced

sly said:
Thanks to to everyone who replied on this.

Have this now working using the following:

my %img_array;

open(ODB, "<votecount_data.txt") || Error ('open','file');

while (<ODB>){
chomp;
my ($img, $nr) = split /\t/;
$img_array{$img} += $nr;
}

close(ODB);

mime();

foreach (keys %img_array){

print qq ($_: $img_array{$_}<br />\n);

}

I am getting a werid : 0 printed out as well but not too worried about
this.

That means there's a blank line in your input. With
'use strict;use warnings' you'd see output clues.
Can someone explain this line to me please:

$img_array{$img} += $nr;

what is {$img} doing ?

creates/increments a hash entry with the results of the split.
What is the best way to now compare the results and find the higest ? I
normally just use if etc with constants to test conditions but don't
know how to do this for variables:

if ($img_array{$_} > $img_array{$_}) { }; ?????

I think you may want sort (perldoc -f sort):

foreach ( sort {$img_array{$b} <=> $img_array{$a} } keys %img_array ){

hth,
 
A

Anno Siegel

Sandman said:
#!/usr/bin/perl
use strict;
use warnings;
my %img_array;
while (<DATA>){
chomp;

No need to chomp. split on whitespace removes linefeeds.
my ($img, $nr) = split /\s+/;
$img_array{$img} += $nr;
}
foreach (keys %img_array){
print "$_: $img_array{$_}\n";
}
__DATA__
14img.jpg 1
14img.jpg 1
14img.jpg 1
9img.jpg 1
4img.jpg 1
4img.jpg 1
7img.jpg 1
7img.jpg 1

A more compact way to do the same thing:

++ $img_array{ $_} for map +( split)[ 0], <DATA>;
print "$_: $img_array{ $_}\n" for keys %img_array;

It slurps DATA, so for very long lists the for loop is better.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top