Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Counting objects in document - need help

    Hi,
    I need to create a simple script that looks into a document and counts how many times every png, gif, tiff, etc. has been used, and then this count is memorized and stored into a log.txt that the script would spit out.

    Could anyone point me in the right direction?
    I'm hoping this is the right part of the forums to post this question, if not, please relocate this post. :-)

    Simply put:
    Script looks into document
    Script finds 1.png
    1.png is memorized and count for 1.png is set to one.
    Script finds 1.png again.
    1.png is memorized again and count for 1.png is set to two.
    Script finds 2.png
    2.png is memorized and count for 2.png is set to one.
    Script can't find any more images.
    End of script, command.log ejects counts to log.txt

    THANK YOU ANYBODY WHO HELPS! I'm very new to scripting.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,436
    Thanks
    13
    Thanked 360 Times in 356 Posts
    if you’re talking about an HTML document, you start by getting all images. then you can use various filter and/or map operations to get the count of each image.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    if you’re talking about an HTML document, you start by getting all images. then you can use various filter and/or map operations to get the count of each image.
    how would I go about 'getting all images'? Do you mean to compile them into an array?
    This html document is roughly 800mb and contains 200,000 jpg names

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,436
    Thanks
    13
    Thanked 360 Times in 356 Posts
    Quote Originally Posted by ukri View Post
    Do you mean to compile them into an array?
    how else do you want to process them in JS?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    This should give you a head start.
    Code:
    (function() {
    
    	function getFilename(url) {
    		var a = document.createElement('a');
    		a.href = url;
    		return a.pathname.replace(/\/?.*\//g, '');
    	}
    
    	var imgs = document.getElementsByTagName('img'),
    		count = imgs.length,
    		images = {},
    		filename,
    		i;
    
    	for (i; i < count; i++) {
    		filename = getFilename(imgs[i].src);
    		if (images[filename]) {
    			images[filename]++;
    		}
    		else {
    			images[filename] = 1;
    		}
    	}
    
    	//test
    	console.log(images)
    
    })();
    Not tested but should give you an idea.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Tags for this Thread

    Posting Permissions

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